套接字编程connect()在第二次运行循环时失败(socket programming connect() fails the second time I run the loop)

编程入门 行业动态 更新时间:2024-10-26 12:26:14
套接字编程connect()在第二次运行循环时失败(socket programming connect() fails the second time I run the loop)

好吧,我试图在for循环中运行服务器端和客户端。 我第一次运行它,它运行正常,但第二次连接失败或它被卡在accept()。 这是我的代码:

客户代码:

for(i=0;i<2;i++) { if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("\nError : Connect Failed\n"); return 1; } //***************Writing_I*************** memset(recvBuff, '0',sizeof(recvBuff)); ticks = time(NULL); snprintf(recvBuff, sizeof(recvBuff), "%.24s\r\n", ctime(&ticks)); if((n = send(sockfd, recvBuff, strlen(recvBuff), 0))<0) { printf("Error : send operation failed\n"); } //***************Reading_I*************** memset(recvBuff, '0', sizeof(recvBuff)); n= recv(sockfd, recvBuff, sizeof(recvBuff)-1, 0); { printf("bytes read: %d\n", n); recvBuff[n] = '\0'; printf("Data: %s\n",recvBuff); } if(n < 0) { printf("\n Read error \n"); } } close(sockfd);

服务器代码:

if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL))>=0) { for(i=0;i<2;i++) { fd= open("/home/t/Desktop/CS II/A4/test.txt", O_RDONLY); //***************Reading*************** memset(sendBuff, '0', sizeof(sendBuff)); count = recv(connfd, sendBuff, sizeof(sendBuff)-1, 0); { printf("bytes read: %d\n", count); sendBuff[count] = '\0'; printf("Data: %s\n",sendBuff); } if(count < 0) { printf("Read error \n"); } //***************Writing*************** ticks = time(NULL); memset(sendBuff, '0', sizeof(sendBuff)); printf("Reading from file\n"); if((noOfBytesRd= read(fd, sendBuff, sizeof(sendBuff)-1))<0) printf("\n Error : read operation failed\n"); sendBuff[noOfBytesRd]='\0'; if((count = send(connfd, sendBuff, strlen(sendBuff), 0))<0) printf("\n Error : wtite operation failed\n"); } close(connfd); }

Well i am trying to run the server side and the client side in a for loop. The first time I run it, it runs fine but the second time either the connect fails or it gets stuck at accept(). Here's my code:

Client code:

for(i=0;i<2;i++) { if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("\nError : Connect Failed\n"); return 1; } //***************Writing_I*************** memset(recvBuff, '0',sizeof(recvBuff)); ticks = time(NULL); snprintf(recvBuff, sizeof(recvBuff), "%.24s\r\n", ctime(&ticks)); if((n = send(sockfd, recvBuff, strlen(recvBuff), 0))<0) { printf("Error : send operation failed\n"); } //***************Reading_I*************** memset(recvBuff, '0', sizeof(recvBuff)); n= recv(sockfd, recvBuff, sizeof(recvBuff)-1, 0); { printf("bytes read: %d\n", n); recvBuff[n] = '\0'; printf("Data: %s\n",recvBuff); } if(n < 0) { printf("\n Read error \n"); } } close(sockfd);

Server code:

if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL))>=0) { for(i=0;i<2;i++) { fd= open("/home/t/Desktop/CS II/A4/test.txt", O_RDONLY); //***************Reading*************** memset(sendBuff, '0', sizeof(sendBuff)); count = recv(connfd, sendBuff, sizeof(sendBuff)-1, 0); { printf("bytes read: %d\n", count); sendBuff[count] = '\0'; printf("Data: %s\n",sendBuff); } if(count < 0) { printf("Read error \n"); } //***************Writing*************** ticks = time(NULL); memset(sendBuff, '0', sizeof(sendBuff)); printf("Reading from file\n"); if((noOfBytesRd= read(fd, sendBuff, sizeof(sendBuff)-1))<0) printf("\n Error : read operation failed\n"); sendBuff[noOfBytesRd]='\0'; if((count = send(connfd, sendBuff, strlen(sendBuff), 0))<0) printf("\n Error : wtite operation failed\n"); } close(connfd); }

最满意答案

套接字在其生命周期中只能connect一次。 您应该为要创建的每个连接创建一个单独的套接字,并在完成连接后将其关闭。

A socket can be connect'ed only once in its life cycle. You should create a separate socket for each connection you intend to make, and close it when you're done with the connection.

更多推荐

sizeof,printf,sendBuff,recvBuff,电脑培训,计算机培训,IT培训"/> <meta name=&qu

本文发布于:2023-08-06 15:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:programming   socket   connect   fails   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!