user-0 提问: C语言socket编程的问题

回复:
user: user-0
content: 引用 5 楼 Rewen 的回复:不是数据问题，是recv 端的问题。 没有配置listening socket吧  服务端   #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h>  int main(int age, char *args[]) { 	struct sockaddr_in addr; 	struct sockaddr_in client_addr;  	int sockfd = 0;  //套接字 	int port = 0;    //端口 	int on = 1; 	int choice = 0; 	socklen_t client_addrlen = 0;  	char buff[1024*10]; 	char pathname[1024];  	FILE *fpjpg = NULL;  	memset(&addr, 0 , sizeof(addr)); 	memset(&client_addr, 0 , sizeof(client_addr)); 	memset(buff, 0 , sizeof(buff)); 	memset(pathname, 0 , sizeof(pathname));  	if ( age < 2 ) 	{ 		printf("port not bei null\n"); 		return -1; 	}  	port = atoi(args[1]); 	if ( port == 0 ) 	{ 		printf("atoi fail\n"); 		return -1; 	}  	sockfd = socket(AF_INET, SOCK_STREAM , 0);  	if ( sockfd == 0 ) 	{ 		printf("socket fail\n"); 		return -1; 	}  	if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on , sizeof(on)) == -1 ) 	{ 		printf("setsockope fail\n"); 		return -1; 	}  	addr.sin_family = AF_INET; 	addr.sin_port = htons(port); 	addr.sin_addr.s_addr = htonl(INADDR_ANY);  	if (bind(sockfd, (struct sockaddr *)&addr , sizeof(addr)) == -1 ) 	{ 		printf("bind fail\n"); 		return -1; 	}  	if ( listen(sockfd , 20) == -1 ) 	{ 		printf("listen fail\n"); 		return -1; 	}  	client_addrlen = sizeof(client_addrlen); 	if ( accept(sockfd, (struct sockaddr *)&client_addr, &client_addrlen) == -1 ) 	{ 		printf("accept fail\n"); 		return -1; 	}  	while (1) 	{ 		printf("[1] is jpg [2] is text :");  		scanf("%d",&choice);  		switch(choice) 		{ 		case 1:  	//		printf("input jpg pathname:"); 	//		while (getchar() != '\n');  	//		fgets(pathname, sizeof(pathname), stdin);  			fpjpg = fopen("/home/x/code/1.jpg", "rb");  	//		if (pathname[strlen(pathname) - 1] == '\n') 	//		{ 	//			pathname[strlen(pathname) - 1] = 0; 	//		}   			if ( fpjpg == NULL ) 			{ 				printf("jpg open fail : %s\n",strerror(errno)); 				continue; 			}  			while ( !(feof(fpjpg)) ) 			{  				memset(buff, 0 , sizeof(buff));  				if (fread(buff, 1 , sizeof(buff), fpjpg) == 0) 				{ 					printf("jpg open fail : %s\n",strerror(errno)); 				}  				if (send(sockfd, buff, sizeof(buff), 0) == -1) 				{ 					printf("send fail\n"); 					break; 				}  			} 			break; 		} 		fclose(fpjpg); 		close(sockfd); 	}  	return EXIT_SUCCESS; }   客户端  #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>  int main(int age, char *args[]) { 	struct sockaddr_in addr;  	int sockfd = 0;   //套接字 	int port = 0;	  //端口 	int ret = 0;  	char buff[1024*10];    //数据buff 	char pathname[1024];   //创建新文件的路径 	char ipbuf[128];       //ip 地址  	FILE *fpjpg = NULL;  	memset(&addr, 0 , sizeof(addr)); 	memset(buff, 0 , sizeof(buff)); 	memset(ipbuf, 0 , sizeof(ipbuf));  	if ( age < 3 ) 	{ 		printf("ip and port not bei null\n"); 		return -1; 	}  	port = atoi(args[2]); 	if ( port == 0 ) 	{ 		printf("atoi fail\n"); 		return -1; 	}  	if (strlen(args[1]) > 16) 	{ 		printf("Invalid IP address\n"); 	} 	strncpy(ipbuf, args[1], strlen(args[1]));   	sockfd = socket(AF_INET, SOCK_STREAM , 0);  	if ( sockfd == 0 ) 	{ 		printf("socket fail\n"); 		return -1; 	}   	addr.sin_family = AF_INET; 	addr.sin_port = htons(port); 	addr.sin_addr.s_addr = inet_addr(ipbuf);   	if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) 	{ 		printf("connect fail\n"); 		return -1; 	}  	fpjpg = fopen("/home/x/code/project/sock/2.jpg","wb");  	while (1) 	{ 		memset(buff, 0 , sizeof(buff)); 		ret = recv(sockfd, buff, sizeof(buff), 0);  // 接收数据  每次到这里直接就返回0了  		if (ret == 0) 		{ 			printf("recv is ok\n"); 			break; 		} 		fwrite(buff, 1 , sizeof(buff), fpjpg);  	} 	fclose(fpjpg); 	close(sockfd);   	return EXIT_SUCCESS; }

user: user-1
content: 不知道有多少前人掉在TCP Socket send(人多)send(病少)send(财富) recv(人多病)recv(少财富) 陷阱里面啊！ http://bbs.csdn.net/topics/380167545

user: user-2
content: 不是数据问题，是recv 端的问题。 没有配置listening socket吧

user: user-0
content: 我是以 二进制 就是wb 打开一个图片文件 然后调用fread 写到buff里 再把这个buff用send发出去     客户端 调用recv 接收到buff里  再用fwrite写到另一个图片文件里   但是服务端一发送   客户端的recv函数就直接返回0  客户端就退出了，我在想是不是recv 接收到了什么数据导致返回0

user: user-3
content: 那是你客户端写错了， send/recv就是操作二进制的。

user: user-0
content: 不行 send 发出去  客户端收到直接就退出了  二进制数据

user: user-3
content: create a tcp socket//socket_fd = socket(AF_INET, SOCK_STREAM, 0); connect to server open the file  while (1) { 	size = fread(); 	if (size < 0)	 	{ 		break; 	} 	send(socket_fd, buf, size, 0); }  close socket