码迷,mamicode.com
首页 > 其他好文 > 详细

C# Socket服务端和客户端互相send和receive

时间:2014-05-11 04:28:28      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

服务端 

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Net.Sockets;
 5 using System.Net;
 6 using System.Threading;
 7 
 8 namespace Controller
 9 {
10     public static class SocketServer
11     {
12         private static byte[] result = new byte[1024];
13         private static int myProt = 6000;   //端口
14         private static Socket serverSocket;
15         private static List<Socket> clientSockets=new List<Socket>();
16         public static void Init()
17         {
18             //服务器IP地址
19             IPAddress ip = IPAddress.Parse("127.0.0.1");
20             serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
21             serverSocket.Bind(new IPEndPoint(ip, myProt));  //绑定IP地址:端口
22             serverSocket.Listen(10);    //设定最多10个排队连接请求
23             Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
24             //通过Clientsoket发送数据
25             Thread myThread = new Thread(ListenClientConnect);
26             myThread.Start();
27             Console.ReadLine();
28         }
29 
30         /// <summary>
31         /// 监听客户端连接
32         /// </summary>
33         private static void ListenClientConnect()
34         {
35             while (true)
36             {
37                 Socket clientSocket = serverSocket.Accept();
38                 clientSockets.Add(clientSocket);
39                 Thread receiveThread = new Thread(ReceiveMessage);
40                 receiveThread.Start(clientSocket);
41             }
42         }
43 
44         /// <summary>
45         /// 接收消息
46         /// </summary>
47         /// <param name="clientSocket"></param>
48         private static void ReceiveMessage(object clientSocket)
49         {
50             Socket myClientSocket = (Socket)clientSocket;
51             while (true)
52             {
53                 try
54                 {
55                     //通过clientSocket接收数据
56                     int receiveNumber = myClientSocket.Receive(result);
57                     string strContent = Encoding.ASCII.GetString(result, 0, receiveNumber);
58                     Console.WriteLine("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
59                 }
60                 catch (Exception ex)
61                 {
62                     Console.WriteLine(ex.Message);
63                     myClientSocket.Shutdown(SocketShutdown.Both);
64                     myClientSocket.Close();
65                     break;
66                 }
67             }
68         }
69 
70         public static void SendMessage(string message)
71         {
72             foreach(Socket clientSocket in clientSockets)
73                 clientSocket.Send(Encoding.ASCII.GetBytes(message));
74         }
75     }
76 }
bubuko.com,布布扣

客户端

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Net.Sockets;
 5 using System.Net;
 6 using System.Threading;
 7 
 8 namespace Player
 9 {
10     public static class SocketClient
11     {
12         private static Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
13         private static byte[] result = new byte[1024];
14         public static void Init()
15         {
16             //设定服务器IP地址
17             IPAddress ip = IPAddress.Parse("127.0.0.1");            
18             try
19             {
20                 clientSocket.Connect(new IPEndPoint(ip, 6000)); //配置服务器IP与端口
21                 Console.WriteLine("连接服务器成功");
22             }
23             catch
24             {
25                 Console.WriteLine("连接服务器失败,请按回车键退出!");
26                 return;
27             }
28 
29             Thread receiveThread = new Thread(ReceiveMessage);
30             receiveThread.Start();
31             
32         }
33         /// <summary>
34         /// 接收消息
35         /// </summary>
36         /// <param name="clientSocket"></param>
37         private static void ReceiveMessage()
38         {            
39             while (true)
40             {
41                 try
42                 {
43                     //通过clientSocket接收数据
44                     int receiveNumber = clientSocket.Receive(result);
45                     string strContent = Encoding.ASCII.GetString(result, 0, receiveNumber);
46                     Console.WriteLine("接收服务端{0}消息{1}", clientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
47                 }
48                 catch (Exception ex)
49                 {
50                     Console.WriteLine(ex.Message);
51                     clientSocket.Shutdown(SocketShutdown.Both);
52                     clientSocket.Close();
53                     break;
54                 }
55             }
56         }
57         public static void SendMessage(string message)
58         {
59             string sendMessage = message;
60             clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));
61         }
62     }
63 }
bubuko.com,布布扣

 

C# Socket服务端和客户端互相send和receive,布布扣,bubuko.com

C# Socket服务端和客户端互相send和receive

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/erictanghu/p/3721045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!