c# socket通信的有关问题
c# socket通信的问题
,我的服务端在运行,并且与客户端通信,但是还有的客户端连不上.
服务端关键代码:
private void socketbind()
{
try
{
string ip = pxl.serverip;
IPAddress ipadd = IPAddress.Parse(ip);
IPEndPoint iep = new IPEndPoint(ipadd, pxl.serverport);
ssk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ssk.Bind(iep);
ssk.Listen(10);
//AcceptConnection();
log.Info("服务已启动!");
thread = new Thread(AcceptConnection);
thread.IsBackground = true;
thread.Start();
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
void AcceptConnection()
{
Socket msgsok = null;
while (true)
{
try
{
msgsok = ssk.Accept();
log.Info(msgsok.RemoteEndPoint.ToString() + "已经连接!");
NetworkStream ns = new NetworkStream(msgsok);
try
{
#region //接收用户标识的长度,最多3位,最大值999
byte[] bt = new byte[3];
log.Info("接收身份信息!");
try
{
ns.Read(bt, 0, bt.Length);
}
catch
{
log.Info(msgsok.RemoteEndPoint.ToString()+"发生异常,关闭连接并释放资源!");
closeConnection(msgsok, ns);
continue;
}
,我的服务端在运行,并且与客户端通信,但是还有的客户端连不上.
服务端关键代码:
private void socketbind()
{
try
{
string ip = pxl.serverip;
IPAddress ipadd = IPAddress.Parse(ip);
IPEndPoint iep = new IPEndPoint(ipadd, pxl.serverport);
ssk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ssk.Bind(iep);
ssk.Listen(10);
//AcceptConnection();
log.Info("服务已启动!");
thread = new Thread(AcceptConnection);
thread.IsBackground = true;
thread.Start();
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
void AcceptConnection()
{
Socket msgsok = null;
while (true)
{
try
{
msgsok = ssk.Accept();
log.Info(msgsok.RemoteEndPoint.ToString() + "已经连接!");
NetworkStream ns = new NetworkStream(msgsok);
try
{
#region //接收用户标识的长度,最多3位,最大值999
byte[] bt = new byte[3];
log.Info("接收身份信息!");
try
{
ns.Read(bt, 0, bt.Length);
}
catch
{
log.Info(msgsok.RemoteEndPoint.ToString()+"发生异常,关闭连接并释放资源!");
closeConnection(msgsok, ns);
continue;
}