gpt4 book ai didi

连接因对等套接字写入错误而重置

转载 作者:行者123 更新时间:2023-11-30 17:29:15 25 4
gpt4 key购买 nike

您好,我正在尝试将多个数据包中的一个大缓冲区发送到客户端计算机,但在 4-5 次写入调用后,我收到错误“连接由对等套接字写入错误重置”。下面是我的代码示例

服务器.c

int main()
{
int fd,i;
image_info info;
bool quit = false;
struct sockaddr_in serv_addr,clint_addr;
int sock_fd,clint_fd,n;//clen;
socklen_t clen = 512;
char buff[100];
sock_fd=socket(AF_INET,SOCK_STREAM,0);
if(sock_fd<0)
{
perror("server socket");
exit(1);
}
puts("server socket is created successfully\n");
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=65000;
serv_addr.sin_addr.s_addr= INADDR_ANY;
if(bind(sock_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
perror("bind");
exit(1);
}
puts("bind successful\n");
puts("listening\n");
if(listen(sock_fd,0)<0)
{
perror("listen");
exit(1);
}

if((fd = open("/dev/video0",O_RDWR | O_NONBLOCK)) == -1)
{
perror("cam_info: Can't open device");
return 1;
}

//printf("FD=%d\n",fd);
init_device(fd);
map_device(fd);
start_capturing(fd);
update_cam(fd);
update_cam(fd);

puts("waiting to accept a connection from cient\n");
clint_fd=accept(sock_fd,(struct sockaddr *)&clint_addr,&clen);
if(clint_fd<0)
{
int errsv = errno;
printf("Errorno:%d",errsv);
perror("client server");
exit(1);
}

info.width = 640;
info.hight = 480;
info.buff_size = 2*640*480;
//uint8_t image_buff[buff_length];

bzero(buff,100);
n=read(clint_fd,buff,100);
if(n<0)
{
perror("read");
exit(1);
}
puts(buff);
n= write(clint_fd,&info,sizeof(info));
if(n<0)
{
perror("write");
exit(1);
}

/*for(i=0;FRAME_BUFFERS[buff_index].start[i];i++)
{
image_buff[i] = FRAME_BUFFERS[buff_index].start[i];
}*/

//printf("length = %d\n",i);
update_cam(fd);
for(int i=0; i<51200*12; i=i+51200)
{
printf("in loop=%d\n",i);
n= write(clint_fd,FRAME_BUFFERS[buff_index].start+i,51200);
if(n<0)
{
perror("write");
exit(1);
}

}

n=read(clint_fd,buff,100);
if(n<0)
{
perror("read");
exit(1);
}
puts(buff);


stop_capturing(fd);

close(fd);
return 0;
}

客户端.c

int main()
{
struct sockaddr_in serv_addr;
FILE *fp;
int sock_fd,n;
unsigned char* rgb;
image_info info;
char buff[100];
fp = fopen("sample.jpg","w+");
sock_fd=socket(AF_INET,SOCK_STREAM,0);
if(sock_fd<0)
{
perror("server socket");
exit(1);
}
puts("client socket is created successfully\n");
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=65000;
serv_addr.sin_addr.s_addr=inet_addr("172.20.115.126");
if(connect(sock_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
perror("connect");
exit(1);
}
puts("connect successful\n");
bzero(buff,100);
n= write(sock_fd,"connected",11);
if(n<0)
{
perror("write");
exit(1);
}
n=read(sock_fd,&info,sizeof(info));
if(n<0)
{
perror("read");
exit(1);
}
printf("size=%d\n",info.buff_size);
n= write(sock_fd,"data received",14);
if(n<0)
{
perror("write");
exit(1);
}
uint8_t image_buff[info.buff_size];
int i=0;
for(i=0; i<51200*12; i=i+51200)
{
printf("in loop=%d\n",i);
n=read(sock_fd,image_buff+i,51200);
if(n<0)
{
perror("read");
exit(1);
}

}

/* else
{
printf("read successfull\n");
}*/
printf("successfull\n");
rgb = (uint8_t*)yuyv2rgb(image_buff, 640, 480);
jpeg(fp,rgb, 640, 480, 100);
printf("successfull\n");
fclose(fp);
return 0;
//printf("size=%d\n",info.buff_size);
//printf("end");
}

请帮我解决这个问题..

最佳答案

您没有检查write()的返回值。您是否假设所有数据都已写入,即 n=51200?情况可能并非如此。同样适用于 read()

关于连接因对等套接字写入错误而重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25707670/

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