很怪啊 我发一个消息一个机子,那个机子收到了,但是它给我发时,我却收不到。我们是在区域网的,默认网关是一样的。我的是广播技术。
//发送
 WSADATA   wsaData;                     //指向WinSocket信息结构的指针   
          SOCKET   sockListener;   
          SOCKADDR_IN   sin,saUdpServ;   
          BOOL   fBroadcast=TRUE;   
          char   sendBuff[1024];   
          int   nSize;   
          int   ncount=0;   
          if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)//进行WinSocket的初始化   
          {   
                  printf("Can't   initiates   windows   socket!Program   stop.\n");//初始化失败返回-1   
                  return   -1;   
          }   
          sockListener=socket(AF_INET,SOCK_DGRAM,0);   
          if(setsockopt(sockListener,SOL_SOCKET,SO_BROADCAST,   
                  (CHAR   *)&fBroadcast,sizeof(BOOL))==SOCKET_ERROR)
  {   printf("Broadcast set error is %d",WSAGetLastError());
  WSACleanup();
  return -1;
  }   
    
          sin.sin_family=AF_INET;   
          sin.sin_port =htons(5000);   
          sin.sin_addr.s_addr = htonl(INADDR_ANY);             if(bind(sockListener, (SOCKADDR   *)&sin, sizeof(sin))!=0)   
          {   
                  printf("Can't   bind   socket   to   local   port!Program   stop.\n");//初始化失败返回-1   
                  return   -1;   
          }   
          saUdpServ.sin_family=AF_INET;   
          saUdpServ.sin_addr.s_addr=htonl(INADDR_BROADCAST);   
          saUdpServ.sin_port=htons(7001);//发送用的端口,可以根据需要更改             nSize=sizeof(SOCKADDR_IN);   
          while(ncount<15)   
          {   
                  sprintf(sendBuff,"Message   %d",ncount++);   
                  sendto(sockListener,sendBuff,   
                          lstrlen(sendBuff),   
                          0,   
                          (SOCKADDR   *)   &saUdpServ,   
                          sizeof(SOCKADDR_IN));   
                  printf("%s\n",sendBuff);   
 
  }
  getchar();
//接受
WSADATA   wsaData;                     //指向WinSocket信息结构的指针   
          SOCKET   sockListener;   
          SOCKADDR_IN   sin,saClient;   
          char   cRecvBuff[1024];   
          int   nSize,nbSize;   
          int   iAddrLen=sizeof(saClient);   
          if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)//进行WinSocket的初始化   
          {   
                  printf("Can't   initiates   windows   socket!Program   stop.\n");//初始化失败返回-1   
                  return   -1;   
          }   
          sockListener=socket(AF_INET, SOCK_DGRAM,0);   
          sin.sin_family=AF_INET;   
          sin.sin_port=htons(7001);//发送端使用的发送端口,可以根据需要更改   
          sin.sin_addr.s_addr =htonl(INADDR_ANY);   
          if(bind(sockListener, (SOCKADDR   FAR   *)&sin,   sizeof(sin))!=0)   
          {   
                  printf("Can't   bind   socket   to   local   port!Program   stop.\n");//初始化失败返回-1   
                  return   -1;   
          }   
          while(1)   
          {   
                  nSize=sizeof(SOCKADDR_IN);   
                  if((nbSize=recvfrom(sockListener,cRecvBuff,1024,0,   
                          (SOCKADDR   FAR   *)   &saClient,&nSize))==SOCKET_ERROR)   
                  {   
                          printf("Recive   Error");   
                          break;   
                  }   
                  cRecvBuff[nbSize]='\0'; 
  printf("sender ip:%s\n",inet_ntoa(saClient.sin_addr));
                  printf("%s\n",cRecvBuff);   
          }