发送数据及接收数据

IPAddress myip=IPAddress.Parse(“127.0.0.1”);

IPEndPoint myserver=new IPEndPoint(myip,2020);

Socket sock=new Socket(AddressFamily.Inte.Network,SocketType.Stream,ProtocolType.Tcp);

Sock.Bind(myserver);

Sock.Listen(50);

Socket bbb=sock.Accept();

 

发送数据

 

         方法1:socket类的send

Socket bbb=sock.Accept();

Byte[] bytes=new Byte[64];

string send="aaaaaaaaaaaa";

bytes=System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());

bbb.Send(bytes,bytes.length,0);//将byte数组全部发送

         方法2.NetworkStream类Write

Socket bbb=sock.Accept();

.NetWorkStream stre=new NewWorkStream(bbb);

Byte[] ccc=new Byte[512];

string sendmessage="aaaaaaaaaaaaaa";

ccc=System.Text.Encoding.BigEndianUnicode.GetBytes(sendmessage);

stre.Write(ccc,0,ccc.length);

 

 

 

接收数据:

            Socket类Receive

           

.....

Socket bbb=sock.Accept();

........

Byte[] ccc=new Byte[512];

bbb.Receive(ccc,ccc.Length,0);

string rece=System.Text.Encoding.BigEndianUnicode.GetString(ccc);

richTextBox1.AppendText(rece+"\r\n");

 

            .NetworkStream类Read

bbb=sock.Accept();

.......

.NetworkStream stre=new.NetworkStream(bbb);

Byte[] ccc=new Byte[512];

stre.Read(ccc,0,ccc.Length);

string readMessage=System.Text.Encoding.BigEndianUnicode.GetString(ccc);

你可能感兴趣的:(数据)