中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#通過Socket實(shí)現(xiàn)客戶端和服務(wù)器端通信的簡單例子

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
C#通過Socket實(shí)現(xiàn)客戶端和服務(wù)器端通信的簡單例子
下面的代碼演示了如果創(chuàng)建一個(gè)用于在客戶端和服務(wù)端交換信息的代碼Socket Server 服務(wù)器端
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
 
namespace ConsoleApplication1
 {
     Class Program
     {
         static void Main (String[] args)
         {
             // 1. to create a socket
             Socket sListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
             / / 2. Fill IP
             IPAddress IP = IPAddress.Parse ("127.0.0.1");
             IPEndPoint IPE = new IPEndPoint (IP, 4321);
 
             / / 3. binding
             sListen.Bind (IPE);
 
             / / 4. Monitoring
             sListen.Listen (2);
 
             / / 5. loop to accept client connection requests
             while (true)
             {
                 Socket clientSocket;
                 try
                 {
                     clientSocket = sListen.Accept();
                 }
                 catch
                 {
                     throw;
                 }
                 // send data to the client
                 clientSocket.Send (Encoding.Unicode.GetBytes ("You there?!!!!"));
             }
         }
 
     }
 }

 
socket client 客戶端
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
 
namespace ConsoleApplication2
{
    Class Program
     {
         static void Main (String[] args)
         {
             // 1.create socket
             Socket S = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
             // 2. complete remote IP
             IPAddress IP = IPAddress.Parse ("127.0.0.1");
             IPEndPoint IPE = new IPEndPoint (IP, 4321);
 
             // 3. connect to the server
             Console.WriteLine("Start to connect to server ....");
             s.Connect (IPE);
 
             // 4. to receive data
             byte[] buffer = new byte[1024];
             s.Receive (buffer, buffer.Length, SocketFlags.None);
             var Msg = Encoding.Unicode.GetString (buffer);
             Console.WriteLine ("received message: (0)", Msg);
 
             Console.ReadKey ();
         }
     }
}
 

標(biāo)簽: 代碼 服務(wù)器 服務(wù)器端 通信

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:MD5加密算法的java實(shí)現(xiàn)

下一篇:asp.net獲取訪客真實(shí)IP地址的函數(shù)