日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中Socket通信用法实例详解

發布時間:2023/12/18 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中Socket通信用法实例详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了C#中Socket通信用法。分享給大家供大家參考。具體如下:

一、UDP方式:

服務器端代碼:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

static void Main(string[] args)

{

??int recv;

??byte[] data = new byte[1024];

??IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);//定義一網絡端點

??Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//定義一個Socket

??newsock.Bind(ipep);//Socket與本地的一個終結點相關聯

??Console.WriteLine("Waiting for a client..");

??IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//定義要發送的計算機的地址

??EndPoint Remote = (EndPoint)(sender);//

??recv = newsock.ReceiveFrom(data, ref Remote);//接受數據?????

??Console.WriteLine("Message received from{0}:", Remote.ToString());

??Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

??string welcome = "Welcome to my test server!";

??data = Encoding.ASCII.GetBytes(welcome);

??newsock.SendTo(data, data.Length, SocketFlags.None, Remote);

??while (true)

??{

????data = new byte[1024];

????recv = newsock.ReceiveFrom(data, ref Remote);

????Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

????newsock.SendTo(data, recv, SocketFlags.None, Remote);

??}

}

客戶端代碼:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

void MainInfo()

{

??byte[] data = new byte[1024];//定義一個數組用來做數據的緩沖區

??string input, stringData;

??IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.21"), 9050);

??Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

??string welcome = "Hello,are you there?";

??data = Encoding.ASCII.GetBytes(welcome);

??server.SendTo(data, data.Length, SocketFlags.None, ipep);//將數據發送到指定的終結點

??IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

??EndPoint Remote = (EndPoint)sender;

??data = new byte[1024];

??int recv = server.ReceiveFrom(data, ref Remote);//接受來自服務器的數據

??Console.WriteLine("Message received from{0}:", Remote.ToString());

??Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

??while (true)//讀取數據

??{

????input = richTextBox1.Text;//從鍵盤讀取數據

????if (input == "text")//結束標記

????{

??????break;

????}

????server.SendTo(Encoding.ASCII.GetBytes(input), Remote);//將數據發送到指定的終結點Remote

????data = new byte[1024];

????recv = server.ReceiveFrom(data, ref Remote);//從Remote接受數據

????stringData = Encoding.ASCII.GetString(data, 0, recv);

????Console.WriteLine(stringData);

??}

??Console.WriteLine("Stopping client");

??server.Close();

}

二、TCP方式:

服務器端代碼:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

Socket serverSocket = null;

Thread clientThread = null;

Socket clientSocket = null;

Thread thread = null;

IPAddress ips = null;

PEndPoint ipep = null;

void ServerStart()

{

??ips = Dns.GetHostAddresses(Dns.GetHostName())[0];

??//創建IPEndPoint實例?

??ipep = new IPEndPoint(ips, 9050);

??//創建一個套接字?

??serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

??serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

??//將所創建的套接字與IPEndPoint綁定?

??serverSocket.Bind(ipep);

??//設置套接字為收聽模式?

??serverSocket.Listen(20);

??while (listenalive)

??{

????try

????{

??????//在套接字上接收接入的連接?

??????clientSocket = serverSocket.Accept();

??????clientThread = new Thread(new ParameterizedThreadStart(ReceiveData));

??????clientThread.Start(clientSocket);

????}

????catch (Exception ex)

????{

??????WriteErrorLog(ex.Message);

??????serverSocket.Close();

??????serverSocket = null;

????}

??}

}

static void ReceiveData(object obj)

{

??bool keepalive = true;

??Socket s = obj as Socket;

??Byte[] buffer = new Byte[1024];

??//根據收聽到的客戶端套接字向客戶端發送信息?

??IPEndPoint clientep = (IPEndPoint)s.RemoteEndPoint;

??Console.WriteLine("客戶端ip:" + clientep.Address + " 端口:" + clientep.Port);

??string welcome = "連接服務器成功";

??buffer = System.Text.Encoding.Unicode.GetBytes(welcome);

??//向客戶端發送“連接服務器成功”消息

??s.Send(buffer, buffer.Length, SocketFlags.None);

??buffer = new Byte[1024];

??int bufLen = 0;

??string content = string.Empty;

??while (true)

??{

????//在套接字上接收客戶端發送的信息?

????bufLen = 0;

????try

????{

??????bufLen = s.Receive(buffer);

??????if (bufLen == 0)

??????{

????????break;

??????}

??????content += System.Text.Encoding.Unicode.GetString(buffer, 0, bufLen);

????}

????catch (Exception ex)

????{

??????break; ;

????}

??}

??Send(s, content);

??s = null;

??buffer = null;

??clientep = null;

??Thread.CurrentThread.Abort();

}

客戶端代碼:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

void Send(string content)

{

??byte[] data = new byte[1024];

??newclient = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);

??ie = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipadd), port);//服務器的IP和端口

??try

??{

????//因為客戶端只是用來向特定的服務器發送信息,所以不需要綁定本機的IP和端口。不需要監聽。

????newclient.Connect(ie);

??}

??catch (System.Net.Sockets.SocketException e)

??{

????Console.WriteLine(e.ToString());

????return;

??}

??int recv = newclient.Receive(data);

??//連接服務器成功

??string stringdata = System.Text.Encoding.Unicode.GetString(data, 0, recv);

??if (stringdata == "連接服務器成功")

??{

????newclient.Send(System.Text.Encoding.Unicode.GetBytes(content));

????newclient.Shutdown(System.Net.Sockets.SocketShutdown.Send);

????data = new byte[1024];

????recv = newclient.Receive(data);

????string result = System.Text.Encoding.Unicode.GetString(data, 0, recv);????

????newclient.Shutdown(System.Net.Sockets.SocketShutdown.Receive);

????newclient.Close();

????MessageBox.Show(result);

??}

??else

??{

????MessageBox.Show("連接服務器失敗", "友情提示");

??}

}

希望本文所述對大家的C#程序設計有所幫助。

總結

以上是生活随笔為你收集整理的C#中Socket通信用法实例详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。