服务器端
建立服务器端Socket操作类
public class SocketServer
{
private Socket socket;
public event NetWork OnConnect;
public event NetWork OnClose;
public event NetWork OnReceiveMessage;
public void Start()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.200.212"), 1234);
socket.Bind(ipEndPoint);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConn), socket);
}
public void Stop()
{
//socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
public void Stop(Hashtable ht)
{
foreach (string key in ht.Keys)
{
SocketObject socketObject =(SocketObject)ht[key];
socketObject.socket.Shutdown(SocketShutdown.Both);
socketObject.socket.Close();
}
Stop();
}
public void Send(Socket clientSocket, string message)
{
if (clientSocket != null && clientSocket.Connected == true)
{
try
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
clientSocket.Send(buffer);
}
catch (Exception ex)
{
}
}
}
private void AcceptConn(IAsyncResult iar)
{
Socket client = socket.EndAccept(iar);
SocketObject socketObject = new SocketObject(client);
client.BeginReceive(socketObject.buffer, 0, socketObject.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), socketObject);
if (OnConnect != null)
{
OnConnect(socketObject);
}
socket.BeginAccept(new AsyncCallback(AcceptConn), socket);
}
private void ReceiveData(IAsyncResult iar)
{
try
{
SocketObject socketObject = (SocketObject)iar.AsyncState;
if (socketObject.socket.Connected == true)
{
int returnCount = socketObject.socket.EndReceive(iar);
if (returnCount == 0)
{
if (OnClose != null)
{
OnClose(socketObject);
}
}
else
{
if (OnReceiveMessage != null)
{
OnReceiveMessage(socketObject);
}
socketObject.InitBuffer = true;
socketObject.socket.BeginReceive(socketObject.buffer, 0, socketObject.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), socketObject);//这里必须是None,具体原因未明,如果选择peek,
//则第一次通讯时产生两次接收事件,高手指教。
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
调用Socket操作类
socketServer = new SocketServer();
socketServer.OnClose += new NetWork(socketServer_OnClose);
socketServer.OnConnect += new NetWork(socketServer_OnConnect);
socketServer.OnReceiveMessage += new NetWork(socketServer_OnReceiveMessage);
socketServer.Start();
这里是核心代码,究竟触发事件时执行什么动作,根据实际需求而定。
客户端:
客户端Socket操作类
public delegate void NetWork(object obj);
public class SocketClient
{
public event NetWork OnClose;
public event NetWork OnReceivedData;
private Socket socket;
public void Connect()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Parse("192.168.200.212"), 1234);
SocketObject socketObject = new SocketObject(socket);
socket.BeginReceive(socketObject.buffer, 0, socketObject.buffer.Length, SocketFlags.None, new AsyncCallback(ReceivedData), socketObject);
}
public void Stop()
{
if (socket != null && socket.Connected == true)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
public void Send(string message)
{
if (socket != null && socket.Connected == true)
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
socket.Send(buffer);
}
}
private void ReceivedData(IAsyncResult iar)
{
SocketObject socketObject = (SocketObject)iar.AsyncState;
if (socketObject.socket.Connected == true)
{
int returnCount = socketObject.socket.EndReceive(iar);
if (returnCount == 0)
{
if (OnClose != null)
{
OnClose(socketObject);
}
}
else
{
if (OnReceivedData != null)
{
OnReceivedData(socketObject);
}
socketObject.InitBuffer = true;
socket.BeginReceive(socketObject.buffer, 0, socketObject.buffer.Length, SocketFlags.None, new AsyncCallback(ReceivedData), socketObject);
}
}
}
}
调用操作类
socketClient = new SocketClient();
socketClient.OnClose += new NetWork(socketClient_OnClose);
socketClient.OnReceivedData += new NetWork(socketClient_OnReceivedData);
socketClient.Connect();