user-0 提问: socket编程问题，收到回不去

回复:
user: user-1
content: 我的UDP串口传数据好用，但是TCP不会用，求助

user: user-2
content: 是不是丢包了，UDP网络状况不好时丢包很正常

user: user-3
content: 楼主学会使用抓包软件比如wireshark正当时！

user: user-4
content: 调试一下吧〉

user: user-5
content: 可以使用gdb调试啊，太乱了，先把UDP那个strcmp去掉，估计是你过滤条件给过滤了

user: user-0
content: 这是udp客户端的 #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/shm.h>  #define MYPORT  8887 #define BUFFER_SIZE 1024 #define MULCAST_IP  "224.0.0.1"	 int main() {     ///定义sockfd     int sock_cli = socket(AF_INET,SOCK_DGRAM, 0);     char sendbuf[BUFFER_SIZE];     char recvbuf[BUFFER_SIZE]; //reuseraddr：地址重用（设置后多个客户端可以同时运行） 	{                 int cnt = -1; 		int val = 1; 		cnt = setsockopt(sock_cli, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); 		if(0 > cnt) 		{ 			perror("set reuser addr err:"); 			exit(1); 		} 	}          ///定义sockaddr_in     struct sockaddr_in servaddr;     socklen_t length = sizeof(servaddr);      memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family = AF_INET;     servaddr.sin_port = htons(MYPORT);  ///服务器端口     servaddr.sin_addr.s_addr = inet_addr(MULCAST_IP);  ///服务器ip       ///连接服务器，成功返回0，错误返回-1     if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)     {         perror("connect problem");         exit(1);     }           while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)     {         sendto(sock_cli, sendbuf, strlen(sendbuf),0,(struct sockaddr *)&servaddr, length); ///发送         if(strcmp(sendbuf,"exit\n")==0)             break;         recvfrom(sock_cli, recvbuf, sizeof(recvbuf),0,(struct sockaddr *)&servaddr, &length); ///接收         fputs(recvbuf, stdout);          memset(sendbuf, 0, sizeof(sendbuf));         memset(recvbuf, 0, sizeof(recvbuf));     }      close(sock_cli);     return 0; }

user: user-0
content: #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/shm.h>  #define MYPORT  8887 #define LISTENQ   20 #define BUFFER_SIZE 1024 #define MULCAST_IP  "224.0.0.1" int main() {     char buffer[BUFFER_SIZE];     int server_tcp_sockfd,server_udp_sockfd;     int connfd;     fd_set fd;     int max1;     int n;     struct ip_mreq mreq;     struct sockaddr_in server_sockaddr;     struct sockaddr_in client_addr;     socklen_t length = sizeof(client_addr); //------tcp---------------      ///定义sockfd     server_tcp_sockfd = socket(AF_INET,SOCK_STREAM, 0);     if(server_tcp_sockfd<0)         {           perror("socket problem\n"); 	  exit(1);  	}          memset(&server_sockaddr, 0, sizeof(server_sockaddr));     server_sockaddr.sin_family = AF_INET;     server_sockaddr.sin_port = htons(MYPORT);     server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);      ///bind，成功返回0，出错返回-1     if(bind(server_tcp_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)     {         perror("bind problem\n");         exit(1);     }      ///listen，成功返回0，出错返回-1     if(listen(server_tcp_sockfd,LISTENQ) == -1)     {         perror("listen problem\n");         exit(1);     } //---------udp---------------     if ((server_udp_sockfd= socket( AF_INET, SOCK_DGRAM,0))<0) {     printf("udpsocket problem\n");      exit(1);     }      if (bind(server_udp_sockfd, (struct sockaddr *) &server_sockaddr, sizeof   (server_sockaddr))<0) {     printf("udpbind problem\n");     exit(0);     }     int cnt=-1;     int val = 1; //reuseraddr：地址重用（设置后多个服务器可以同时运行）     cnt = setsockopt(server_udp_sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));     if(0 > cnt) 	{ 		perror("set reuser addr err:"); 		exit(1); 	}     //设置回环许可         int loop = 1; 	cnt = setsockopt(server_udp_sockfd,IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); 	if(0 > cnt) 	{ 		perror("multicast loop problem"); 		exit(1); 	} 	 	mreq.imr_multiaddr.s_addr = inet_addr(MULCAST_IP);	//广播地址 	mreq.imr_interface.s_addr = htonl(INADDR_ANY);		//网络接口为默认 	//将本机加入多播组 	cnt = setsockopt(server_udp_sockfd,IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); 	if(0 > cnt) 	{ 		perror("ip add membership problem"); 		exit(1); 	}       max1 = (server_tcp_sockfd >server_udp_sockfd ? server_tcp_sockfd+1 : server_udp_sockfd+1);      /* Inialize the fd_set */     FD_ZERO(&fd);     FD_SET(server_tcp_sockfd,&fd); /* add passive tcp socket */     FD_SET(server_udp_sockfd,&fd); /* add udp socket */ /*--------------select--------------*/ /* call select */      if (select(max1,&fd,NULL,NULL,NULL)<0) {     perror("select problem");     exit(1);     }    //------if tcp--------------------------              if (FD_ISSET(server_tcp_sockfd,&fd) ) {        	connfd = accept(server_tcp_sockfd, (struct sockaddr*)&client_addr, &length);     	if(connfd<0)     	{     	    perror("connect problem\n");     	    exit(1);     	} 	     	while(1)     	{     	    memset(buffer,0,sizeof(buffer));     	    int len = recv(connfd, buffer, sizeof(buffer),0); 	    if (n<0) { 		perror("Error receiving data"); 		}       	    if(strcmp(buffer,"exit\n")==0)     	        break;     	    fputs(buffer, stdout);     	    buffer[BUFFER_SIZE]='\0'; 	    send(connfd, buffer, sizeof(buffer), 0); 	     	}     	close(connfd);     	close(server_tcp_sockfd);     return 0;     }  //-------------if udp---------------     if (FD_ISSET(server_udp_sockfd,&fd))      { 	while(1) 	{ 		memset(buffer,0,sizeof(buffer));     		n=recvfrom(server_udp_sockfd,buffer,BUFFER_SIZE,0,(struct sockaddr*)&client_addr,&length); 		if(strcmp(buffer,"exit\n")==0)     	        break; 		fputs(buffer, stdout);     		if (n<0) { 			perror("Error receiving data"); 		} else { 			 			sendto(server_udp_sockfd,buffer,BUFFER_SIZE,0,(struct sockaddr*)&client_addr,length); 		}         }         close(server_tcp_sockfd); 	return 0;     }	 }