读书人

socket客户端断开重连后不能与服务器通

发布时间: 2014-01-08 00:30:58 作者: rapoo

socket客户端断开重连后不能与服务器通信
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndex = 0;
IPAddress[] p = Dns.GetHostAddresses(Dns.GetHostName());
IP.Text = p[2].ToString();
Port.Text = "8080";
}


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
label2.Text = "服务器IP:";
label3.Text = "服务器端口号:";
}
else
{
label2.Text = "本地IP:";
label3.Text = "本地端口号:";
}
}
Socket sc;
Socket temp;
byte[] buffer = new byte[1024];

private void open_Click(object sender, EventArgs e)
{
if (open.Text == "连接")
{
if (comboBox1.SelectedIndex == 0)
{
int port = int.Parse(Port.Text);
IPAddress ip = IPAddress.Parse(IP.Text);
IPEndPoint ipe = new IPEndPoint(ip, port);

sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sc.Connect(ipe);
txtreceived.Text += "连接成功...\r\n";
sc.BeginReceive(buffer, 0, 1024, SocketFlags.None, new AsyncCallback(readcallback), this);
open.Text = "断开";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}
else
{
int port = int.Parse(Port.Text);


IPAddress ip = IPAddress.Parse(IP.Text);
IPEndPoint ipe = new IPEndPoint(ip, port);

sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);//套接字重用
sc.Bind(ipe); //绑定EndPoint对象
sc.Listen(0); //开始监听
txtreceived.Text += "服务器建立成功...\r\n";

try
{
Thread thread = new Thread(accept);
thread.Start();
open.Text = "断开";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}
}
else
{
if (comboBox1.SelectedIndex == 0)
{
if (sc!= null && sc.Connected)
{
try
{
System.Threading.Thread.Sleep(10);
//关闭客户端Socket,清理资源
sc.Close();
open.Text = "连接";
}
catch { }
}
}
else
{
if (temp != null)
{
try


{
//关闭Socket之前,首选需要把双方的Socket Shutdown掉
temp.Close();
//Shutdown掉Socket后主线程停止10ms,保证Socket的Shutdown完成
System.Threading.Thread.Sleep(10);
//关闭客户端Socket,清理资源
sc.Close();
open.Text = "连接";
}
catch { }
}
}
}
}

public void accept()
{
temp = sc.Accept();
temp.BeginReceive(buffer, 0, 1024, SocketFlags.None, new AsyncCallback(readcallback), this);
}

private void readcallback(IAsyncResult ar)
{
comboBox1.Invoke(new EventHandler(delegate
{
if (comboBox1.SelectedIndex == 0)
{
try
{
int bytesRead = sc.EndReceive(ar);
if (bytesRead > 0)
{
txtreceived.Text += Encoding.UTF8.GetString(buffer, 0, bytesRead);
}

sc.BeginReceive(buffer, 0, 1024, SocketFlags.None, new AsyncCallback(readcallback), this);
}
catch { }
}


else
{
try
{
int bytesRead = temp.EndReceive(ar);
if (bytesRead > 0)
{
txtreceived.Text += Encoding.UTF8.GetString(buffer, 0, bytesRead);
}

temp.BeginReceive(buffer, 0, 1024, SocketFlags.None, new AsyncCallback(readcallback), this);
}
catch { }
}
}));
}

private void send_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
try
{
byte[] s = Encoding.UTF8.GetBytes(sendtxt.Text);
sc.Send(s, s.Length, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
try
{
byte[] s = Encoding.UTF8.GetBytes(sendtxt.Text);
temp.Send(s, s.Length, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


}
}

很简单的一个socket通信程序,目前服务器方面有问题,客户端断开后重连接就不能再与服务器通信了,应该是accept的问题,求大神指导一下,顺便问一下断开那部分的代码应该怎么写比较好,我现在写的感觉还是有问题,虽然能实现
[解决办法]
没看到你的accept()在哪里调用的啊
[解决办法]
客户端断开 服务端会抛出一个异常
你在异常处理中将这个socket连接释放掉
[解决办法]

引用:
Quote: 引用:

楼主可以在服务器里做文章,应该是服务器里没有做客户端断开连接后的处理,没有把相应链接的线程的资源进行释放。
对 我也觉得是这样的..我是个新手,能不能告诉我服务器怎么判断客户端有没有断开啊?

在下面这段程序中的catch语句块中加入处理的语句,清除掉相关的用户资源,自己去图书馆借书看看网络编程的书里面有很多的同步、异步处理的例子的。
 try
{
Thread thread = new Thread(accept);
thread.Start();
open.Text = "断开";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

读书人网 >C#

热点推荐