UDP通信受信側は、2次元配列を受信し、内容は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:  }

<!--
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
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];//その際,2次元配列を構造体にカプセル化し,構造体をオブジェクトとして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:  }

<!--
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->