/////////////////////////////////////////////////////////////////////////server////////////////////////////////////////////////////////////////////
The class Serv
{
//listening socket
Public Socket listenfd;
//client connection
Public Conn [] conns;
//maximum number of connections
Public int maxConn=50;
//get connection pool index, returns a negative number said access failure
Public int NewIndex ()
{
If (conns==null) return 1;
for(int i=0; I & lt; Conns. Length; I++)
{
If (conns [I]==null)
{
Conns [I]=new Conn ();
return 1;
} else if (conns [I] isUse==false)
{
Return the I;
}
}
return -1;
}
//open server
Public void Start (string host, int port)
{
//the connection pool
Conns=new Conn [maxConn];
for(int i=0; I & lt; MaxConn; I++)
{
Conns [I]=new Conn ();
}
//Socket
Listenfd=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
//Bind
IPAddress ipAdr=IPAddress. Parse (host);
IPEndPoint ipEp=new IPEndPoint (ipAdr, port);
Listenfd. Bind (ipEp);
//Listen
Listenfd. Listen (maxConn);
//the Accept
Listenfd. BeginAccept (AcceptCb listenfd);
Console. WriteLine (" [] server startup success ");
}
//the Accept callback, when he has initiated the client connection processing
Private void AcceptCb IAsyncResult (ar)
{
Try
{
Console. WriteLine ("!!!!! Despite the arrival of a new connection ");
//get a socket connection
The Socket Socket=listenfd. EndAccept (ar);
//to get connection object available
Int index=NewIndex ();
If (index & lt; 0)
{
Socket. The Close ();
Console. WriteLine (" [warning] connection is full ");
}
The else
{
Console. WriteLine ("!!!!! Already available connection object ");
Conn Conn=conns [index];
Conn. Init (socket);
String=conn. (adr) GetAdress ();
Console. WriteLine (" the client connection [" + + "(adr)] conn pool ID:" + index);
//is ready to receive the message
Conn. Socket. BeginReceive (conn. ReadBuff, conn. BuffCount, conn. BuffRemain (), SocketFlags. None, ReceiveCb, conn);
}
Console. WriteLine (" again ready to handle new connection ");
//again ready to handle a client connection
Listenfd. BeginAccept (AcceptCb listenfd);
}
The catch (Exception e)
{
Console. WriteLine (" AcceptCb failure: "+ e.M essage);
}
}
//receives the message callback
Private void ReceiveCb IAsyncResult (ar)
{
Conn Conn=(Conn) ar. AsyncState;
Try
{
Int count=conn. Socket. EndReceive (ar);
//close signal
If (count & lt;=0)
{
Console. WriteLine (" received [" + conn. GetAdress () + "] disconnected ");
Conn. Close ();
return;
}
//data processing
String STR=. Encoding UTF8. Get string (conn. ReadBuff, 0, count);
Console. WriteLine (" received [" + conn. GetAdress () + "] data: "+ STR);
STR=conn. GetAdress () + ":" + STR.
Byte [] bytes=System. Text.. Encoding UTF8. GetBytes (STR);
//send data to all of the connection object (i.e., data synchronization)
for(int i=0; I & lt; Conns. Length; I++)
{
If (conns [I]==null) continue;
if (! Conns [I] isUse) continue;
Console. WriteLine (" the message is relayed to "+ conns [I] GetAdress ());
Conns [I]. Socket. Send (bytes);
}
//continue to receive
Conn. Socket. BeginReceive (conn. ReadBuff, conn. BuffCount, conn. BuffRemain (), SocketFlags. None, ReceiveCb, conn);
} the catch (Exception e)
{
Console. WriteLine (" received [" + conn. GetAdress () + "] disconnected ");
Conn. Close ();
}
}
}
////////////////////////////////////////////////////////////////endpoint class///////////////////////////////////////////////////////////////////
The class Conn
{
//buffer size
Public const int BUFFER_SIZE=1024;
//Socket
Public Socket Socket;
//whether or not to use the
Public bool isUse=false;
//Buff
Public byte [] readBuff=new byte [BUFFER_SIZE];
Public int buffCount=0;
//the constructor
Public Conn ()
{
ReadBuff=new byte [BUFFER_SIZE];
}
//initialize
Public void Init (Socket Socket)
{
This. The socket=socket;
IsUse=true;
BuffCount=0;
}
//the number of bytes of buffer remaining
Public int BuffRemain ()
{
Return BUFFER_SIZE - buffCount;
}
//get the client address
Public string GetAdress ()
{
if (! IsUse) return "unable to get the address;
Return the socket. RemoteEndPoint. ToString ();
}
//close
Public void the Close ()
{
if (! IsUse) return;
Console. WriteLine (" [broken links] "+ GetAdress ());
Socket. The Close ();
IsUse=false;
}
}
////////////////////////////////////////////////////////server main program//////////////////////////////////////////////////////////////////////
Class Program
{
The static void Main (string [] args)
{
Console. WriteLine (" open server ");
Serv Serv=new Serv ();
Serv. Start (" 127.0.0.1 ", 1234);
While (true) {
Thread.sleep (1);
}
}
}
//////////////////////////////////////////////////////////////////////////client////////////////////////////////////////////////////////////
Public class Client02
{
//the client receives the message
Private string recvStr;
//sent to the server message
Private string sendStr;
//the Socket and the receive buffer
The Socket Socket;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull