C#socket通訊

來源:互聯網
上載者:User

標籤:

關於C#socket通訊,分為同步和非同步通訊,本文簡單介紹一下同步通訊。

通訊兩端分別為用戶端(Client)和伺服器(Server):

(1)Cient:

1:建立一個Socket對像;

2:用socket對像的Connect()方法以上面建立的EndPoint對像做為參數,向伺服器發出串連請求;

3:如果串連成功,就用socket對像的Send()方法向伺服器發送資訊;

4:用socket對像的Receive()方法接受伺服器發來的資訊 ;

5:通訊結束後一定記得關閉socket;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;namespace Client{    class Program    {       static Socket ClientSocket;        static void Main(string[] args)        {            String IP = "127.0.0.1";            int port =8885 ;            IPAddress ip = IPAddress.Parse(IP);  //將IP地址字串轉換成IPAddress執行個體            ClientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//使用指定的地址簇協議、通訊端類型和通訊協定            IPEndPoint endPoint = new IPEndPoint(ip, port); // 用指定的ip和連接埠號碼初始化IPEndPoint執行個體            ClientSocket.Connect(endPoint);  //與遠程主機建立串連            Console.WriteLine("開始發送訊息");            byte[] message = Encoding.ASCII.GetBytes("Connect the Server");  //通訊時實際發送的是位元組數組,所以要將發送訊息轉換位元組            ClientSocket.Send(message);            Console.WriteLine("發送訊息為:" + Encoding.ASCII.GetString(message));            byte[] receive = new byte[1024];            int length = ClientSocket.Receive(receive);  // length 接收位元組數組長度            Console.WriteLine("接收訊息為:" + Encoding.ASCII.GetString(receive));            ClientSocket.Close();  //關閉串連        }    }}

用戶端返回結果:

 

(2)Server: 

1:建立一個Socket對像;

2:用socket對像的Bind()方法綁定EndPoint;

3:用socket對像的Listen()方法開始監聽;

4:接受到用戶端的串連,用socket對像的Accept()方法建立新的socket對像用於和請求的用戶端進行通訊;

5:用新的socket對象接收(Receive)和發送(Send)訊息。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;using System.Threading;namespace Server{    class Program    {        static Socket ReceiveSocket;        static void Main(string[] args)        {            int port = 8885;            IPAddress ip = IPAddress.Any;  // 偵聽所有網路客戶介面的客活動            ReceiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//使用指定的地址簇協議、通訊端類型和通訊協定   
ReceiveSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true); //有關通訊端設定 IPEndPoint endPoint = new IPEndPoint(ip,port); ReceiveSocket.Bind(new IPEndPoint(ip, port)); //綁定IP地址和連接埠號碼 ReceiveSocket.Listen(10); //設定最多有10個排隊串連請求 Console.WriteLine("建立串連"); Socket socket = ReceiveSocket.Accept(); byte[] receive = new byte[1024]; socket.Receive(receive); Console.WriteLine("接收到訊息:" + Encoding.ASCII.GetString(receive)); byte[] send = Encoding.ASCII.GetBytes("Success receive the message,send the back the message"); socket.Send(send); Console.WriteLine("發送訊息為:"+Encoding.ASCII.GetString(send)); } }}

 伺服器返回結果:

 

C#socket通訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.