UDP通信接收端,接收二维数组,内容为0与1

   1:  using System;
   2:  using System.Net;
   3:  using System.Net.Sockets;
   4:  using System.Text;
   5:   
   6:   
   7:  public class UDPListener
   8:  {
   9:      private const int listenPort = 5050;
  10:      private const int Height = 200;
  11:      private const int Width = 100;
  12:      private static void StartListener()
  13:      {
  14:          bool[,] test = new bool[Height, Width];   //二维数组的定义方式
  15:          byte[,] test1 = new byte[Height, Width];
  16:          UdpClient listener = new UdpClient(listenPort);    //本机侦听的端口号实例化。
  17:          IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort); //实例化
  18:   
  19:          try   
  20:          {
  21:              long m = 0;
  22:              int n = 0;             //必须为int型,不然会造成溢出。不能为byte,byte就255.
  23:              int i = 0,j = 0;
  24:              while (true)
  25:              {
  26:                  n = 0;
  27:                  Console.WriteLine("Waiting for broadcast");
  28:                  byte[] bytes = listener.Receive(ref groupEP);
  29:                  for (i = 0; i < Height; i++)
  30:                  {
  31:                      for (j = 0; j < Width; j++)
  32:                      {
  33:                          test[i, j] = BitConverter.ToBoolean(bytes,n); //将读到的bytes转换为bool类型的数据,即ture 和  false。
  34:                          n++;
  35:                          if (test[i, j] == true)    //将此true 和  false的数组转为只有0与1存在的数组。
  36:                              test1[i,j] = 1;
  37:                          else
  38:                              test1[i,j] = 0;
  39:                      }
  40:                  }
  41:                  for (i = 0; i < Height; i++)
  42:                  {
  43:                      for (j = 0; j < Width; j++)
  44:                      {
  45:                          Console.Write(test1[i,j]) ;     //调用控制台程序输出即可。
  46:                      }
  47:                      Console.WriteLine();
  48:                  }
  49:                      
  50:                  //Console.WriteLine("Received broadcast from {0} : {1}  hava received {2} times 
",
  51:                  //    groupEP.ToString(),Encoding.ASCII.GetString(bytes, 0, bytes.Length),m);
  52:              }
  53:   
  54:          }
  55:          catch (Exception e)
  56:          {
  57:              Console.WriteLine(e.ToString());
  58:          }
  59:          //finally
  60:          //{
  61:          //    //listener.Close();
  62:          //}
  63:      }
  64:   
  65:      public static int Main()
  66:      {
  67:          StartListener();
  68:   
  69:          return 0;
  70:      }
  71:  }

           UDP接收端小程序,将发送端发送过来的数据转换为稀疏矩阵.

           下面是发送端程序:

 

   1:  #include <Winsock2.h>
   2:  #pragma comment(lib,"WS2_32.lib")
   3:  #include <stdio.h>
   4:  #include<iostream>
   5:  #include <string> 
   6:  #include<vector>
   7:  using namespace std;
   8:  //服务器端口号为5050
   9:  #define DEFAULT_PORT 5050
  10:  //缓冲区长度
  11:  #define DATA_BUFFER 1024
  12:   
  13:  void main(int argc,char *argv[])
  14:  {
  15:   
  16:      
  17:      /*matrix.Num = 1;
  18:      //matrix.point = new Point[2];
  19:      memset(&matrix,0,sizeof(matrix));
  20:      matrix.point[0].x = 1;
  21:      matrix.point[0].y = 2;
  22:      matrix.point[0].value = 3;
  23:      matrix.point[0].U = 4;    
  24:      matrix.point[1].x = 5;
  25:      matrix.point[1].y = 6;
  26:      matrix.point[1].value = 7;
  27:      matrix.point[1].U = 8;
  28:      int n = 2;
  29:      int len = 2*sizeof(Point)+2;
  30:      vector<char> str(len);//声明变长数组
  31:      //char* str = new char[n];
  32:      //memset(&str,0,sizeof(str));//将str,赋值0,长度为sizeof
  33:      memcpy(&str,(char*)&matrix,len);//将matrix里面的值,赋值给str,长度sizeof。与上面刚好相反。
  34:  
  35:      //char *buffer=(char *)&matrix;
  36:                */
  37:      
  38:      WSADATA wsaData;
  39:      SOCKET sClient;
  40:      int iPort=5050;
  41:      //服务器地址长度
  42:      int iLen;
  43:      //接收数据的缓冲
  44:      int iSend;
  45:      int    iRecv;
  46:      //要发送给服务器的信息
  47:      char send_buf[]="I am a client.";
  48:      //接收数据的缓冲区
  49:      char recv_buf[DATA_BUFFER];
  50:      //服务器端地址
  51:      struct sockaddr_in ser;
  52:      //处理命令行中
  53:      //接收数据的缓冲区初始化
  54:      memset(recv_buf,0,sizeof(recv_buf));
  55:      if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
  56:      {
  57:          printf("Failed to load Winsock.
");
  58:          return;
  59:      }
  60:      char map[10][20];
  61:      for(int i = 0;i < 10;i++)
  62:          for(int j = 0;j < 20;j++)
  63:             map[i][j] = 1;
  64:      
  65:      map[0][0] = 0;   //  for test 
  66:      map[0][1] = 0;
  67:      map[0][2] = 0;
  68:      //map[0][1] = 0;
  69:      //for(int i = 0;i < 10;i++)
  70:      //    for(int j = 0;j < 20;j++)    

71: // sendData.obs[i][j] = map[i][j]; //那时候想着把二维数组封装在结构体里面,然后将结构体作为一个对象,通过UDP发送过去。

  72:    //后来发现完全没必要,数组本身我们也可以理解为一个对象,直接发送即可。
  73:      //建立服务器端地址
  74:      ser.sin_family=AF_INET;
  75:      ser.sin_port=htons(iPort);
  76:      ser.sin_addr.s_addr=inet_addr("127.0.0.1");
  77:      //建立客户端数据报套接口
  78:      sClient=socket(AF_INET,SOCK_DGRAM,0);
  79:      long k=0;
  80:      while (1)
  81:      {
  82:          if(sClient==INVALID_SOCKET)
  83:          {
  84:              printf("socket()Failed:%d
",WSAGetLastError());
  85:              return;
  86:          } 
  87:          //向服务器发送数据 
  88:          Sleep(5);//暂停一秒
  89:                             iSend=sendto(sClient,(char*)&map,sizeof(map),0,(struct sockaddr*)&ser,iLen);
  90:          k=k+1;
  91:          if(iSend==SOCKET_ERROR)
  92:          {
  93:              printf("sendto()Failed:%d
",WSAGetLastError());
  94:              return;
  95:          }
  96:          else if(iSend==0)
  97:              return;
  98:          else
  99:              printf("sendto()succeeded. have sent %d times 
",k);
 100:   
 101:          //从服务器接收数据
 102:          //iRecv=recvfrom(sClient,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&ser,&iLen);
 103:          //if(iRecv==SOCKET_ERROR)
 104:          //{
 105:          //    printf("recvfrom()Failed.:%d
",WSAGetLastError());
 106:          //    return;
 107:          //}
 108:          //else if(iRecv==0)
 109:          //    return;
 110:          //else
 111:          //{
 112:          //    //显示从服务器收到的信息
 113:          //    printf("recvfrom():%s
",recv_buf);
 114:          //    printf("---------------------------
");
 115:          //}
 116:          
 117:      }
 118:      closesocket(sClient);
 119:      WSACleanup();
 120:  }
原文地址:https://www.cnblogs.com/zhuxuekui/p/3516805.html