??????????????C#??C#???????????????????QAQ??
????????????????????????(???????????)???????????Щ?????????????????????????д?????????
???????????????????????????Socket???????????????????????£????????????~
????????????????????serverSocket????????г????????????AddressFamily:IP????????SocketType:Socket????????????ProtoType:????Э?飩;
?????????????????server???????IP:port;????????????????????????????Client.
?????????????????????????????Client????????ip:port??????serverSocket.Accept()???????????????????????????????е???????????????棩
??????????????server????????Client????????????????????Client??????????ClientB?????Server?????ClientB???????????????????????????Client??????????ClientA?????????ClientA?????????????????ClientB???????????Clients?????????????????????????????????
????????????????????
????Server?????
1 namespace SocketServer
2 {
3     class Program
4     {
5         private static byte[] result = new byte[1024];
6
7         static Socket serverSocket;
8         private static string client;
9         private static Socket clientSocket;
10         //?????????????Client???????????????????Client???????ж??
11         //?????????????Socket[]??????????????????????齫client????????????????
12         //???????????????????′ε?????????????????Client
13         private static Socket clientSocketA=null;
14         private static Socket clientSocketB=null;
15
16         static void Main(string[] args)
17         {
18             Program.SetPort(8885);
19         }
20         private static void SetPort(int port)
21         {
22             IPAddress ip = IPAddress.Parse("127.0.0.1");//set ip
23             serverSocket = new Socket(AddressFamily.InterNetwork??
24                 SocketType.Stream?? ProtocolType.Tcp);//initialize
25             serverSocket.Bind(new IPEndPoint(ip?? port));//bind
26             serverSocket.Listen(10);
27             //?????????
28             Console.WriteLine("????{0}???"?? serverSocket.LocalEndPoint.ToString());
29             //?????????????????????????
30             Thread myThread = new Thread(ListenClientConnect);
31             myThread.Start();
32             Console.ReadLine();
33
34         }
35         /// <summary>
36         /// ?????????????
37         /// </summary>
38         private static void ListenClientConnect()
39         {
40             while (true)
41             {
42                 //Client??????? ??????????
43                 clientSocket = serverSocket.Accept();
44
45                 //????????????????Client???????д??
46                 if (clientSocketA == null)
47                 {
48                     clientSocketA = clientSocket;
49                 }
50                 else if (clientSocketB == null)
51                 {
52                     clientSocketB = clientSocket;
53                 }
54                 else
55                 {
56                     //?????????????????????????????????α???????
57                     if (clientSocketB.IsBound)
58                     {
59                         clientSocketA = clientSocketB;
60                         clientSocketB = clientSocket;
61                     }
62                     else
63                     {
64                         clientSocketB = clientSocketA;
65                         clientSocketA = clientSocket;
66                     }
67
68                 }
69                 clientSocket.Send(Encoding.ASCII.GetBytes("say hello"));
70                 //??????????Client???
71                 Thread receivedThread = new Thread(ReceiveMessage);
72                 receivedThread.Start(clientSocket);
73
74             }
75         }
76
77         private static void ReceiveMessage(object clientSocket)
78         {
79             Socket myClientSocket = (Socket) clientSocket;
80
81             while (true)
82             {
83                 try
84                 {
85                     int revceiveNumber = myClientSocket.Receive(result);
86                     //Console.WriteLine("????????{0}???{1}"?? myClientSocket.RemoteEndPoint.ToString()
87                     //    ?? Encoding.ASCII.GetString(result?? 0?? revceiveNumber));
88                     Console.WriteLine(Encoding.ASCII.GetString(result?? 0?? revceiveNumber));
89                     if (myClientSocket == clientSocketA)
90                     {
91                         Console.WriteLine("receive from A");
92                         if (clientSocketB!=null&&clientSocketB.IsBound)
93                         {
94                             Console.WriteLine("a IS BOUND");
95                             clientSocketB.Send(result?? 0?? revceiveNumber?? SocketFlags.None);
96                         }
97                         else
98                         {
99                             myClientSocket.Send(Encoding.ASCII.GetBytes("the people is not online! Send Failed!"));
100                             Console.WriteLine("????????????????????");
101                         }
102                     }
103                     else
104                     {
105                         Console.WriteLine("receive from B");
106                         if (clientSocketA != null && clientSocketA.IsBound)
107                         {
108                             Console.WriteLine("a IS BOUND");
109                             clientSocketA.Send(result?? 0?? revceiveNumber?? SocketFlags.None);
110                         }
111                         else
112                         {
113                             myClientSocket.Send(Encoding.ASCII.GetBytes("the people is not online! Send Failed!"));
114                             Console.WriteLine("????????????????????");
115                         }
116
117                     }
118
119                 }
120                 catch(Exception ex)
121                 {
122                     Console.WriteLine(ex.Message);
123                     myClientSocket.Shutdown(SocketShutdown.Both);
124                     myClientSocket.Close();
125                     break;
126
127                 }
128             }
129
130         }
131     }
132 }
????Client?????(????????? ????????)
1 namespace SocketClient
2 {
3     class Program
4     {
5         private static byte[] result = new byte[1024];
6         private static Socket clientSocket;
7         private static void ListenServer()
8         {
9             while (true)
10             {
11                 result = new byte[1024];
12                 int receiveLength = clientSocket.Receive(result);
13
14                 Console.WriteLine("{0}"?? Encoding.ASCII.GetString(result?? 0?? receiveLength));
15             }
16
17         }
18         static void Main(string[] args)
19         {
20
21             IPAddress ip = IPAddress.Parse("127.0.0.1");
22             clientSocket = new Socket(AddressFamily.InterNetwork?? SocketType.Stream?? ProtocolType.Tcp);
23             try
24             {
25                 clientSocket.Connect(ip?? 8885);
26                 Console.WriteLine("????????");
27
28             }
29             catch (Exception e)
30             {
31                 Console.WriteLine("????????");
32                 return;
33             }
34             Thread threadRead = new Thread(ListenServer);
35             threadRead.Start();
36
37
38             while(true)
39             {
40                 try
41                 {
42                     Thread.Sleep(1000);
43                     string sendMessage = Console.ReadLine();
44                     clientSocket.Send(Encoding.ASCII.GetBytes("Sylvia:"+sendMessage));
45
46                 }
47                 catch (Exception ex)
48                 {
49                     clientSocket.Shutdown(SocketShutdown.Both);
50                     clientSocket.Close();
51                     break;
52                 }
53
54             }
55             Console.WriteLine("??????? ????????");
56             Console.ReadKey();
57         }
58     }
59 }
????д???????????????Send???????????byte[]??????С??????????byte[]??????С????????????С??