socket 服务 连接不上的问题。拜托了大神们 .NET 2.0 WINFORM 。一个socket 服务开启监听后,有很多的客户端连接。使用一段时间后,会出现某个客户端连接不上,重启客户端也不行。除非关掉服务端,在开启服务,那个连接不上的客户端才能连接上。大神们,能帮我分析下原因吗?服务端和客户端都是异步。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 检查一下服务端的socket连接等相关的资源有没有释放掉。 服务端的资源有没有释放掉,和你客户端重启与否有关系吗?比如通过socket建立连接之后,客户端重启,而服务端与客户端保持连接的资源没有释放。 public void OnRecievedData(IAsyncResult ar) { try { SocketServerClient client = (SocketServerClient)ar.AsyncState; byte[] aryRet = client.GetRecievedData(ar); if (aryRet.Length < 1) { client.Sock.Close(); this.my_ClientList.Remove(client); } else { string data = Encoding.GetEncoding("GBK").GetString(aryRet); this.OnReceivedString(this, new ReceivedMessageEventArgs(data, client, this.my_ClientList)); client.SetupRecieveCallback(this); } } catch { } }客户端 下线后,服务端会收到 “0” ,上面的代码就回收了 我把服务器端代码贴出来 ,麻烦大家帮我看看 有没有问题 public class ServerSocked { // Fields private Socket listener; public ArrayList my_ClientList = new ArrayList(); public bool isRun = true; // Events public event ReceiveMessageHandler receiveddata; // Methods public void closeSocket() { try { this.isRun = false; this.listener.Close(); foreach (SocketServerClient clientSend in this.my_ClientList) { clientSend.Sock.Close(); this.my_ClientList.Remove(clientSend); //return; } } catch { } GC.Collect(); GC.WaitForPendingFinalizers(); } /// <summary> /// 建立新连接 /// </summary> /// <param name="sockClient"></param> public void NewConnection(Socket sockClient) { try { SocketServerClient client = new SocketServerClient(sockClient); this.my_ClientList.Add(client); client.socketname = client.Sock.RemoteEndPoint.ToString(); client.SetupRecieveCallback(this); } catch { } } public void OnConnectRequest(IAsyncResult ar) { try { // if (this.isRun) // { Socket listener = (Socket)ar.AsyncState; this.NewConnection(listener.EndAccept(ar)); listener.BeginAccept(new AsyncCallback(this.OnConnectRequest), listener); // } } catch { } } public void OnReceivedString(object sender, ReceivedMessageEventArgs e) { try { if (this.receiveddata != null) { this.receiveddata(sender, e); } } catch { } } public void OnRecievedData(IAsyncResult ar) { try { SocketServerClient client = (SocketServerClient)ar.AsyncState; byte[] aryRet = client.GetRecievedData(ar); if (aryRet.Length < 1) { client.Sock.Close(); this.my_ClientList.Remove(client); } else { string data = Encoding.GetEncoding("GBK").GetString(aryRet); this.OnReceivedString(this, new ReceivedMessageEventArgs(data, client, this.my_ClientList)); client.SetupRecieveCallback(this); } } catch { } } /// <summary> /// 开始监听 /// </summary> /// <param name="nPortListen"></param> /// <param name="ip"></param> public void startSocket(int nPortListen, string ip) { try { this.isRun = true; IPAddress ipAddr = IPAddress.Parse(ip); this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.listener.Bind(new IPEndPoint(ipAddr, nPortListen)); this.listener.Listen(1024); this.listener.BeginAccept(new AsyncCallback(this.OnConnectRequest), this.listener); } catch { this.isRun = false; //throw new ApplicationException("socket start error"); } } // Nested Types public delegate void ReceiveMessageHandler(object sender, ReceivedMessageEventArgs e); } 客户端和服务端 有没有保持心跳连接?你可以参考下腾讯的QQ 处理方式 每台电脑的QQ过2分钟都会向服务器发送一个请求。如果服务器接收不到客户端发送的请求默认 客户端已经掉线关掉连接请求。TCP 还是UDP 都需要注意适时释放链接请求。 做这个项目的人也是脑残 谁开发的啊不用wcf脑残脑残脑残啊自己写Socket 你对效率要求就这么高啊 SocketAsyncEventArgs 保准解决后顾之忧 http://technet.microsoft.com/zh-cn/library/system.net.sockets.socketasynceventargs(v=vs.90).aspx已经封装好了 包括异步套接字池,自己只需要实现具体接收数据后处理的方法 socket客户端的几个疑点 各位大侠,如何实现文档内容与其行号的联动啊? 征求C#的输入与输出方法 iis每天00:00点都会被重新定向 C# GDI+绘图中,如何实现,随着鼠标的移动画矩形线,松开鼠标时显示最后状态下的矩形框? picturebox控件paint事件 求助:怎么在弹出窗口时有提示音(100分) 今天的面食题,关于多线程的 谢谢 Visual Studio 2005 相对路径问题 & excel下载问题 一个简单的入门题,需要ADO.net的人帮忙。 关于程序中的错误处理!
比如通过socket建立连接之后,客户端重启,而服务端与客户端保持连接的资源没有释放。
{
try
{
SocketServerClient client = (SocketServerClient)ar.AsyncState;
byte[] aryRet = client.GetRecievedData(ar);
if (aryRet.Length < 1)
{
client.Sock.Close();
this.my_ClientList.Remove(client);
}
else
{
string data = Encoding.GetEncoding("GBK").GetString(aryRet);
this.OnReceivedString(this, new ReceivedMessageEventArgs(data, client, this.my_ClientList));
client.SetupRecieveCallback(this);
}
}
catch { }
}客户端 下线后,服务端会收到 “0” ,上面的代码就回收了
public class ServerSocked
{
// Fields private Socket listener;
public ArrayList my_ClientList = new ArrayList();
public bool isRun = true;
// Events
public event ReceiveMessageHandler receiveddata; // Methods
public void closeSocket()
{
try
{
this.isRun = false;
this.listener.Close();
foreach (SocketServerClient clientSend in this.my_ClientList)
{
clientSend.Sock.Close();
this.my_ClientList.Remove(clientSend);
//return;
}
}
catch
{
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
/// <summary>
/// 建立新连接
/// </summary>
/// <param name="sockClient"></param>
public void NewConnection(Socket sockClient)
{
try
{
SocketServerClient client = new SocketServerClient(sockClient);
this.my_ClientList.Add(client);
client.socketname = client.Sock.RemoteEndPoint.ToString();
client.SetupRecieveCallback(this);
}
catch { }
} public void OnConnectRequest(IAsyncResult ar)
{
try
{
// if (this.isRun)
// {
Socket listener = (Socket)ar.AsyncState;
this.NewConnection(listener.EndAccept(ar));
listener.BeginAccept(new AsyncCallback(this.OnConnectRequest), listener);
// }
}
catch
{ }
} public void OnReceivedString(object sender, ReceivedMessageEventArgs e)
{
try
{
if (this.receiveddata != null)
{
this.receiveddata(sender, e);
}
}
catch { }
} public void OnRecievedData(IAsyncResult ar)
{
try
{
SocketServerClient client = (SocketServerClient)ar.AsyncState;
byte[] aryRet = client.GetRecievedData(ar);
if (aryRet.Length < 1)
{
client.Sock.Close();
this.my_ClientList.Remove(client);
}
else
{
string data = Encoding.GetEncoding("GBK").GetString(aryRet);
this.OnReceivedString(this, new ReceivedMessageEventArgs(data, client, this.my_ClientList));
client.SetupRecieveCallback(this);
}
}
catch { }
}
/// <summary>
/// 开始监听
/// </summary>
/// <param name="nPortListen"></param>
/// <param name="ip"></param>
public void startSocket(int nPortListen, string ip)
{
try
{
this.isRun = true;
IPAddress ipAddr = IPAddress.Parse(ip);
this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.listener.Bind(new IPEndPoint(ipAddr, nPortListen));
this.listener.Listen(1024);
this.listener.BeginAccept(new AsyncCallback(this.OnConnectRequest), this.listener);
}
catch
{
this.isRun = false;
//throw new ApplicationException("socket start error");
}
} // Nested Types
public delegate void ReceiveMessageHandler(object sender, ReceivedMessageEventArgs e);
}
TCP 还是UDP 都需要注意适时释放链接请求。
不用wcf脑残脑残脑残啊
自己写Socket 你对效率要求就这么高啊
http://technet.microsoft.com/zh-cn/library/system.net.sockets.socketasynceventargs(v=vs.90).aspx已经封装好了 包括异步套接字池,自己只需要实现具体接收数据后处理的方法