写了一个2个TCP通信的类,是异步的,和大家分享一下,请大家帮我完善一下!!

EveryCase 2009-02-06 04:37:09
写了一个2个TCP通信的类,是异步的,和大家分享一下,请大家帮我完善一下!!看看那些地方还需要修改!希望大家多提宝贵的意见!!

/// <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)
{

}

}
}


...全文
76 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞天鹰 2009-02-06
  • 打赏
  • 举报
回复
学习了,4楼的意见提的非常好
yuloukong 2009-02-06
  • 打赏
  • 举报
回复
学习
EveryCase 2009-02-06
  • 打赏
  • 举报
回复
恩。谢谢。我会好好的考虑下。争取过几天弄个好的上来!
EveryCase 2009-02-06
  • 打赏
  • 举报
回复
恩。谢谢。我会好好的考虑下。争取过几天弄个好的上来!
ztenv 2009-02-06
  • 打赏
  • 举报
回复
几个意见:
1、要有异常处理,当产生异常时要偿试恢复连接
2、具体发送什么数据(文件/字符流)可以写在delegate的方式,这样用起来就非常方便了,
3、Socket的发送方法基本上应该都封装(为了更完善)
4、当多个客户端连接到同一服务器时,而服务器需要不同的回应信息时,是否预以考虑?
5、可以考虑做成非可视化的组件(设计时可以从toolbox中选择,运行时不可见)

不要骂我,希望对你的提高有帮助;
EveryCase 2009-02-06
  • 打赏
  • 举报
回复
希望大家都提建议。嘎嘎。我修改好了。在发上来大家共乡呀
xiaoyanwei2000 2009-02-06
  • 打赏
  • 举报
回复
学习学习
悔说话的哑巴 2009-02-06
  • 打赏
  • 举报
回复
写的不错

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧