您好我正在尝试将多个数据包中的一个大缓冲区发送到客户端计算机但是在4-5次写入调用后,我收到错误“通过对等套接字写入错误连接重置”。以下是我的代码示例
server.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;
}
client.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");
}
请帮我解决这个问题..
答案 0 :(得分:0)
这通常是由写入已经被对等方关闭的连接引起的。
在这种情况下,可能是因为在关闭之前没有读取所有输入。你忽略了read()
返回的结果,除了检查-1,所以你假设它填充了缓冲区。它没有义务这样做,或者一次传输多个字节。你需要循环,直到你收到了你期望的所有数据。这反过来可能意味着您需要在数据之前发送正在写入的数据的长度。
答案 1 :(得分:0)
您没有检查write()
的返回值。您是否假设所有数据都已写入,即n = 51200?情况可能并非如此。同样适用于read()
。