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连接释放掉
[解决办法]
try
{
Thread thread = new Thread(accept);
thread.Start();
open.Text = "断开";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}