gpt4 book ai didi

客户端未与服务器 C 连接(套接字编程)

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:56 24 4
gpt4 key购买 nike

我有一个非常简单的客户端和服务器代码:

客户

int sockfd = 0;
int bytesReceived = 0;
char recvBuff[BUFSIZE];
memset(recvBuff, '0', sizeof(recvBuff));
struct sockaddr_in serv_addr;
printf("here i am\n");
/* Create a socket first */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0){
printf("\n Error : Could not create socket \n");
return 1;
}
fprintf(stderr, "connection stats\n");
fprintf(stderr, "connecting to %d - %s \n",port,server);
/* Initialize sockaddr_in data structure */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port); // port
serv_addr.sin_addr.s_addr = server;
printf(" attempting connection %s:%d\n", server, port);
/* Attempt a connection */
int con = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
printf("Error %d\n", con);
exit(-1);

和服务器

   int listenfd = 0;
int connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[BUFSIZE];
int numrv;

listenfd = socket(AF_INET, SOCK_STREAM, 0);

printf("Socket retrieve success\n");

memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
printf("here 2\n");
serv_addr.sin_family = AF_INET;
printf("here 3\n");
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("here 4\n");
serv_addr.sin_port = htons(port);
printf("here 5\n");
int optval = 1;
printf(" opening port %d\n", port);
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const void *) &optval, sizeof(int));
bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
printf("binded\n");


while (1) {
connfd = accept(listenfd, (struct sockaddr *) NULL, NULL);
printf("connfd %d", connfd);


close(connfd);
sleep(1);


}

客户端输出:

here i am
connecting to 8888 - localhost
attempting connection localhost:8888

服务器输出

Socket retrieve success
here 2
here 3
here 4
here 5
opening port 8888
binded

经过几分钟的等待……我观察到以下情况:客户:

here i am
connecting to 8888 - localhost
attempting connection localhost:8888
Error -1

服务器:

Socket retrieve success
here 2
here 3
here 4
here 5
opening port 8888
binded
connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd

根据-1,我可以猜到连接失败了。我不确定我错过了什么?

最佳答案

服务器在调用 accept() 之前未在绑定(bind)套接字上调用 listen()


还有这个

serv_addr.sin_addr.s_addr = server;

编译器应该发出警告。您不能将 C-“字符串”(我假设 server 是)分配给 in_addr_t(serv_addr.sin_addr.s_addr 是什么)。

解决这个问题:

serv_addr.sin_addr.s_addr = inet_addr(server);

或者更好地为此添加错误检查,如下所示:

{
in_addr_t in_addr = inet_addr(server);
if (INADDR_NONE == in_addr)
{
perror("inet_addr() failed");
abort(); /* or whatever error handling you choose. */
}

serv_addr.sin_addr.s_addr = in_addr;
}

关于客户端未与服务器 C 连接(套接字编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30453112/

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