c# socket 新手问题。 socket绑定bind()端口后,用Close()关闭后,不能重新开启 10
程序界面如图:1.要实现的功能。当勾上启动监听后,开始监听服务。(ok)2.取消勾选状态时:关才监听服务。(ok)3.再次勾选,再次重新开启监听。(这里出错了,显示端口已...
程序界面如图:
1.要实现的功能。当勾上 启动监听后,开始监听服务。(ok)
2.取消勾选状态时:关才监听服务。(ok)
3.再次勾选,再次重新开启监听。(这里出错了,显示端口已被绑定)
现在前两个功能都可以。但重新开启时就报错了。
源代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace 网关
{
public partial class Form1 : Form
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverip = Dns.GetHostAddresses("192.168.110.10")[0];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//checkBox1.Checked = true; //启动时自动启动监听
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
IPEndPoint serverduankou = new IPEndPoint(serverip, 8000);
server.Bind(serverduankou);
server.Listen(10);
label1.Text = "监听状态:正常";
}
if (checkBox1.Checked == false)
{
//try
//{
// server.Shutdown(SocketShutdown.Both);
// server.Close();
// label1.Text = "监听状态:关闭";
//}
//catch
//{
// //server.Close();
// //label1.Text = "监听状态:关闭";
//}
server.Close();
label1.Text = "监听状态:关闭";
}
}
}
} 展开
1.要实现的功能。当勾上 启动监听后,开始监听服务。(ok)
2.取消勾选状态时:关才监听服务。(ok)
3.再次勾选,再次重新开启监听。(这里出错了,显示端口已被绑定)
现在前两个功能都可以。但重新开启时就报错了。
源代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace 网关
{
public partial class Form1 : Form
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverip = Dns.GetHostAddresses("192.168.110.10")[0];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//checkBox1.Checked = true; //启动时自动启动监听
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
IPEndPoint serverduankou = new IPEndPoint(serverip, 8000);
server.Bind(serverduankou);
server.Listen(10);
label1.Text = "监听状态:正常";
}
if (checkBox1.Checked == false)
{
//try
//{
// server.Shutdown(SocketShutdown.Both);
// server.Close();
// label1.Text = "监听状态:关闭";
//}
//catch
//{
// //server.Close();
// //label1.Text = "监听状态:关闭";
//}
server.Close();
label1.Text = "监听状态:关闭";
}
}
}
} 展开
展开全部
用下面代码试试看
public partial 吵饥class Form1 : Form
{
Socket server;
IPAddress serverip = Dns.GetHostAddresses("192.168.110.10")[0];
public Form1()
{
InitializeComponent();
}
private 码碰郑void Form1_Load(object sender, EventArgs e)
{
//checkBox1.Checked = true; //启动时自动启动监听
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
if(CreateSocketAndListen())
{
label1.Text = "监听状态:正常";
}
else
{
label1.Text = "Socket发生错误!";
}
return;
}
else
{
if(CloseSocket())
{
label1.Text = "监听状态:关闭";
}
else
{
label1.Text = "Socket发生错误!";
}
}
}
private bool CreateSocketAndListen()
{
try
{
server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint serverduankou = new IPEndPoint(serverip, 8000);
server.Bind(serverduankou);
server.Listen(10);
return true;
}
迟颂 catch
{
socket = null;
return false;
}
}
private bool CloseSocket()
{
if(socket==null) return false;
try
{
server.Shutdown(SocketShutdown.Both);
server.Close( );
server = null;
return true;
}
catch
{
server = null;
retrun false;
}
}
}
}
追问
直接编译不通过。。提示找不到socket.
我把socket = null;改成server=null;
编译。Socket发生错误!
我追踪了一下。是退出socket时发生错误。。
不知道你那里能不能编译成功。thanks.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询