gpt4 book ai didi

c - 关于 C (Linux) 套接字服务器客户端编程中 accept() 的问题

转载 作者:太空狗 更新时间:2023-10-29 11:43:37 25 4
gpt4 key购买 nike

我在 Linux 中使用 C 语言编写了一个服务器和客户端程序。

代码如下:

服务器代码:

#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write

int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
close(client_sock);
puts("I am at While loop");
while(1)
{
}
return 0;
}

客户端代码:

#include<stdio.h> //printf
#include<string.h> //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr

int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");

server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}

puts("Connected\n");
close(sock);
while(1);
return 0;
}

当我运行服务器代码然后运行客户端代码时它工作正常但是当我终止客户端代码再次运行它时(服务器代码只执行一次)它会自动连接到服务器而不接受服务器调用。

在服务器调用 accept() 连接客户端之前,我不希望客户端连接。

我只是想让客户端等待或给一些超时,直到服务器调用 accept() 来建立它们之间的连接。

我得到的输出如下:

服务器输出:

root@beaglebone:/home/Saad/test# ./server_test
Socket created
bind done
Waiting for incoming connections...
Connection accepted
I am at While loop

客户端输出:

root@beaglebone:/home/Saad/test# ./client_test     
Socket created
Connected

^C
root@beaglebone:/home/Saad/test# ./client_test
Socket created
Connected

我不知道为什么会这样,也不知道如何解决。

更新:

在对客户端代码进行一些编辑后,我尝试了以下答案,但我遇到了同样的问题。

客户端代码:

#include<stdio.h> //printf
#include<string.h> //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr

int main(int argc , char *argv[])
{
char buf[1];
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
int ret;

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");

server.sin_addr.s_addr = inet_addr("192.168.1.2");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server
ret = connect(sock , (struct sockaddr *)&server , sizeof(server));
if (ret < 0)
{
perror("connect failed. Error");
return 1;
}
recv(sock, buf, 1, MSG_WAITFORONE);
printf("%s",buf);
puts("Connected\n");
close(sock);
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
ret = connect(sock , (struct sockaddr *)&server , sizeof(server));
if (ret < 0)
{
perror("connect failed. Error");
return 1;
}
else
{
puts("Connected\n");
}
while(1);
return 0;
}

客户端输出:

root@beaglebone:/home/Saad/test# ./client_test_1
Socket created
1Connected

Socket created
Connected

最佳答案

@EJP 解释的很好,如果你想解决你的问题,使用这个代码:

客户:

#include<stdio.h> //printf
#include<string.h> //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr

int main(int argc , char *argv[])
{
char buf[1];
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
int ret;

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");

server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server
ret = connect(sock , (struct sockaddr *)&server , sizeof(server));
if (ret < 0)
{
perror("connect failed. Error");
return 1;
}
recv(sock, buf, 1, MSG_WAITFORONE);

puts("Connected\n");
close(sock);
while(1);
return 0;
}

服务器:

#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);

send(client_sock, "1", 1, 0);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
close(client_sock);
puts("I am at While loop");
while(1)
{
}
return 0;
}

在客户端:
我在客户端添加了一个 recv 调用,它会阻塞直到服务器实际发送一些东西。

在服务器端:
我在接受调用后立即添加了一个 send 调用。这确保只有已被接受的客户端才能继续其代码路径。

关于c - 关于 C (Linux) 套接字服务器客户端编程中 accept() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30160485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com