111,092
社区成员




/// <summary>
/// TCP异步通信类(客户端)
/// </summary>
public class TCPClient
{
Socket ClientSocket;
IPAddress IP;
Thread ReceiveThread;
byte[] ReceiveData = new byte[1024];
public TCPClient()
{
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void ClientConnect(string IPstr,int port)
{
IP = IPAddress.Parse(IPstr);
ClientSocket.BeginConnect(IP, port, new AsyncCallback(ClientHasConnect), ClientSocket);
}
public void ClientHasConnect(IAsyncResult ar)
{
try
{
Socket TempSocket = ar.AsyncState as Socket;
TempSocket.EndConnect(ar);
}
catch (Exception ex)
{
}
try
{
//此时的发送的数据包应该抽象出来
string str = "fasongbaobao";
byte[] SendByte = System.Text.Encoding.ASCII.GetBytes(str);
ClientSocket.BeginSend(SendByte, 0, SendByte.Length, SocketFlags.None,
new AsyncCallback(ClientSendData), ClientSocket);
}
catch (Exception e1)
{
}
ReceiveThread = new Thread(new ThreadStart(ReceiveDataFromServer));
ReceiveThread.Start();
}
public void ClientSendData(IAsyncResult ar)
{
Socket TempSocket = ar.AsyncState as Socket;
TempSocket.EndSend(ar);
}
public void ReceiveDataFromServer()
{
while (true)
{
try
{
ClientSocket.BeginReceive(ReceiveData, 0, ReceiveData.Length, SocketFlags.None,
new AsyncCallback(ClientReceiveData), ClientSocket);
}
catch (Exception e2)
{
}
}
}
public void ClientReceiveData(IAsyncResult ar)
{
Socket TempSocket = ar.AsyncState as Socket;
TempSocket.EndReceive(ar);
//接收数据的处理放到事件中去处理!!
string strx = System.Text.Encoding.ASCII.GetString(ReceiveData);
EventHandler<TxeventArgs> temp = ReceiveProcess;
if (temp != null)
{
temp(this, new TxeventArgs(strx));
}
}
public event EventHandler<TxeventArgs> ReceiveProcess;
}
public class TxeventArgs:EventArgs
{
private string str;
public TxeventArgs(string sx)
{
str = sx;
}
}
/// <summary>
/// TCP异步通信类(服务器)
/// </summary>
public class TCPServer
{
Socket ServerListen;
IPEndPoint endpoint;
IPAddress IP;
Socket TranSocket;
public TCPServer(string IPstr,int port)
{
ServerListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IP = IPAddress.Parse(IPstr);
endpoint = new IPEndPoint(IP, port);
ServerListen.Bind(endpoint);
ServerListen.Listen(50);
Thread ListenThread = new Thread(new ThreadStart(TCPAccept));
ListenThread.Start();
}
public void TCPAccept()
{
try
{
ServerListen.BeginAccept(new AsyncCallback(TCPEndAccept), ServerListen);
}
catch (Exception e1)
{
}
}
public void TCPEndAccept(IAsyncResult ar)
{
try
{
Socket TempSocket = ar.AsyncState as Socket;
TranSocket = TempSocket.EndAccept(ar);
}
catch (Exception e2)
{ }
try
{
byte[] RecData = new byte[1024];
TranSocket.BeginReceive(RecData, 0, RecData.Length, SocketFlags.None,
new AsyncCallback(TCPEndRec), TranSocket);
}
catch (Exception e3)
{
}
}
public void TCPEndRec(IAsyncResult ar)
{
Socket TempSocket = ar.AsyncState as Socket;
TempSocket.EndReceive(ar);
Thread SendThread = new Thread(new ThreadStart(SendData));
SendThread.Start();
}
public void SendData()
{
byte[] TcpSendData = System.Text.Encoding.ASCII.GetBytes("huiying");
try
{
TranSocket.BeginSend(TcpSendData, 0, TcpSendData.Length, SocketFlags.None,
new AsyncCallback(TCPEndSend), TranSocket);
}
catch (Exception e4)
{
}
}
public void TCPEndSend(IAsyncResult ar)
{
try
{
Socket TempSocket = ar.AsyncState as Socket;
TempSocket.EndSend(ar);
}
catch (Exception e5)
{
}
}
}