linux socket tcp传输问题

yanranyaya 2013-05-16 09:29:08
我在本机上写了一个客户端和一个服务端,客户端在循环里面做1、连接服务端 2、发送数据 3、关闭套接字 ,一直重复这样操作。 发现发送一段时间后客户端与服务端发送和接受数据不一致,再就是一直阻塞不动!

服务端代码如下:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
int fd;
struct sockaddr_in serverAddr;

if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("open socket error!\n");
exit(-1);
}
printf("%d\n",fd);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(30000);
if(inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr.s_addr) != 1)
{
printf("trans addr error!\n");
exit(-1);
}

if(bind(fd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1)
{
printf("bind error!\n");
exit(-1);
}

if(listen(fd, 20) == -1)
{
printf("listen error!\n");
exit(-1);
}
int countl = 0;
char recv_buf[20*1024];
int fd_client;
while(true)
{
if((fd_client = accept(fd, (struct sockaddr*)NULL, NULL)) == -1)
{
printf("accept error!\n");
continue;
}
if(recv(fd_client, recv_buf, 20*1024, 0) < 0)
{
printf("recv error!\n");
close(fd_client);
continue;
}
printf("%llu, %llu\n", countl, strlen(recv_buf));
countl++;
//printf("%d\n", fd_client);
close(fd_client);
}
return 0;
}

客户端代码如下:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

int main(int argc, char *argv[])
{
char *need_write1;
char *need_write2;
unsigned int fileLen;
int fd, offset;
fd = open("data1.txt", O_RDONLY);
fileLen = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
need_write1 = (char*)malloc(fileLen + 1);
need_write1[fileLen] = '\0';
read(fd, need_write1, fileLen);
close(fd);

fd = open("data2.txt", O_RDONLY);
fileLen = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
need_write2 = (char*)malloc(fileLen + 1);
need_write2[fileLen] = '\0';
read(fd, need_write2, fileLen);
close(fd);

struct sockaddr_in serverAddr;

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(30000);
if(inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr.s_addr) != 1)
{
printf("trans addr error!\n");
exit(-1);
}

int countl = 0;
char *send_buf;
while(countl < 100000)
{
if(countl%3 == 0)
send_buf = need_write1;
else if(countl%3 == 1)
send_buf = need_write2;
else
send_buf = need_write1 + 1;
if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("open socket error!\n");
exit(-1);
}
//printf("%d\n", fd);
if(connect(fd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1)
{
printf("connect error! %s\n", strerror(errno));
close(fd);
continue;
}
if(send(fd, send_buf, strlen(send_buf) + 1, 0) == -1)
{
printf("send error!\n");
close(fd);
continue;
}
close(fd);
printf("%d\n",countl++);
}

return 0;
}


哪位大神指导原因的指导下,感激不尽!QQ1072721076
...全文
105 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧