user-0 提问: UDP socket通信异常的问题

回复:
user: user-1
content: socklen_t clilen = sizeof( clientaddr); 这样试试

user: user-2
content: 建议楼主先学会使用抓包软件比如wireshark

user: user-0
content: [quote=引用 10 楼 yuqing89320 的回复:] socklen_t clilen = sizeof( clientaddr); 这样试试[/quote  真的太感谢了！！！！ 按照你说的去修改，可以正常运行了！！！ 你真的很牛！！！

user: user-0
content: @ForestDB, 在第一个Error后面，你的client 进程应该已经终止了吧  我这里是，当我启动第二个client进程后，server/client双方通信发送显示数据一切正常。  还有，你提到“这么多Error" 其实都是一种Invalid argument原因呢。  不过还是特别感谢你哈。

user: user-3
content: $ ./server  =====Waiting for request from client===== [Mon Jun 30 21:42:24 2014]Received:	abc [Mon Jun 30 21:42:24 2014]Response Error(Invalid argument) =====Waiting for request from client===== [Mon Jun 30 21:42:46 2014]Received:	abc [Mon Jun 30 21:42:46 2014]Response Error(Invalid argument) =====Waiting for request from client===== [Mon Jun 30 21:43:49 2014]Received:	abc [Mon Jun 30 21:43:49 2014]Response Error(Invalid argument) =====Waiting for request from client===== [Mon Jun 30 21:44:27 2014]Received:	abc [Mon Jun 30 21:44:27 2014]Response Error(Invalid argument) =====Waiting for request from client===== 这么多Error，检查下吧。

user: user-0
content: 如今CSDN真的很冷清......

user: user-0
content: 如我在1楼所讲，大概问题已经定位到时在服务器端 server1.c中的 sendto 返回值n的原因  我查下函数说明sendto返回为-1表示出错（此时errno会被设置），否则表示时间发送的字节数。 现在就是在client第一个进程的时候 ，服务器端sendto返回-1,结果我解析了errno等于EINVAL（表示传给系统调用的参数不正确）。  可是我仔细对比了 sendto中所有参数 都没什么问题呢

user: user-0
content: sorry，在2楼附上两个文件源码  client1.c ----------------------------- #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<getopt.h> #include<time.h>  #define MAXLINE 1024 #define SERV_PORT 9998  char* print_time(){ 	time_t t = time(NULL); 	char *timestr = ctime(&t); 	//remove the newline character 	int len = strlen(timestr); 	*(timestr + len -1) = '\0'; 	 	return timestr; } int readable_timeo(int sockfd, int seconds){ 	fd_set rset; 	FD_ZERO(&rset); 	FD_SET(sockfd, &rset); 	 	struct timeval tv; 	tv.tv_sec = seconds; 	tv.tv_usec = 0; 	 	return	select(sockfd+1, &rset, NULL, NULL, &tv); } void dg_cli(FILE *fp, int sockfd, const struct sockaddr *pservaddr, socklen_t len){ 	char recvline[MAXLINE + 1]; 	char sendline[MAXLINE]; 	//clear buffer 	memset(recvline, '\0', sizeof(recvline)); 	memset(sendline, '\0', sizeof(sendline));  	int  n = 0; 	printf("===========[%s] Start to work============\n", print_time()); 	 	while((fgets(sendline, MAXLINE, fp) != NULL)){ 		sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, len); 		 		if(readable_timeo(sockfd, 5) == 0){//socket timeout 			printf("[%s] socket timeout\n", print_time()); 			return; 		}else{ 			if((n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) > 0){ 				recvline[n-1] = '\0'; 				printf("[%s]Response:\t%s\n", print_time(), recvline); 			}else{ 				printf("%s socket recvfrom error\n", print_time()); 				return; 			} 		} 		//clear buffer for everytime 		memset(recvline, '\0', sizeof(recvline)); 		memset(sendline, '\0', sizeof(sendline)); 	} } int main(int argc, char *argv[]){ 	if(argc != 2){ 		printf("Usage:%s \tip\n", *argv); 		exit(EXIT_FAILURE); 	} 	//ip host 	char *ip = *(argv+1); 	 	struct sockaddr_in servaddr; 	memset(&servaddr, '\0', sizeof(servaddr)); 	servaddr.sin_family = AF_INET; 	servaddr.sin_port = htons(SERV_PORT); 	inet_pton(AF_INET, ip, &servaddr.sin_addr); 	 	int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 	socklen_t len = sizeof(servaddr); 	 	dg_cli(stdin, sockfd, (const struct sockaddr *)&servaddr, len); 	 	return 0; } -------------------------------  server1.c ------------------------------- #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<time.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h>  #define MAXLINE 1024 #define SERV_PORT 9998  char* print_time(){ 	time_t t = time(NULL); 	char *timestr = ctime(&t); 	 	int len = strlen(timestr); 	*(timestr + len -1) = '\0'; 	 	return timestr; } int main(int argc, char *argv[]){ 	char recvline[MAXLINE]; 	 	struct sockaddr_in servaddr; 	struct sockaddr_in clientaddr; 	memset(&servaddr, '\0', sizeof(servaddr)); 	memset(&clientaddr, '\0', sizeof(clientaddr)); 	memset(recvline, '\0', sizeof(recvline)); 	 	servaddr.sin_family = AF_INET; 	servaddr.sin_port = htons(SERV_PORT); 	servaddr.sin_addr.s_addr = INADDR_ANY; 	 	int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 	socklen_t len = sizeof(servaddr); 	bind(sockfd, (struct sockaddr *)&servaddr, len); 	listen(sockfd, 5); 	 	size_t n = 0; 	socklen_t clilen = 0; 	while(1){ 		printf("=====Waiting for request from client=====\n"); 		 		n = recvfrom(sockfd, recvline, MAXLINE, 0, (struct sockaddr *)&clientaddr, &clilen); 		if(n > 0){ 			printf("[%s]Received:\t\%s", print_time(), recvline); 			n = sendto(sockfd, recvline, n, 0, (const struct sockaddr *)&clientaddr, clilen); 			if(n == -1){ 				printf("[%s]Response Error(%s)\n", print_time(), strerror(errno)); 			}else{ 				printf("[%s]Response Successfully!!!!\n", print_time()); 			} 		}else{ 			printf("[%s]Recvfrom error\n", print_time()); 		} 		memset(recvline, '\0', sizeof(recvline)); 		memset(&clientaddr, '\0', sizeof(clientaddr)); 	} 	return 0; } -------------------------------

user: user-4
content: 没有环境测试代码，看一下你的测试逻辑，我觉得服务端的问题，不是客户端的问题。查一下为啥server端显示reponse error

user: user-5
content: clilen你传的什么东西?

user: user-0
content: 没人帮忙看看吗？ 我写的很详细嘛。