远程主机强迫关闭了一个现有的连接
public static ManualResetEvent allDone = new ManualResetEvent(false);
private Thread yth;
private bool listenerRun = true;
Socket listener;
private const int maxsocket = 10000;
int port = 54321;
private void buttonStart_Click(object sender, EventArgs e)
{
listenerRun = true;
yth = new Thread(new ThreadStart(ListenClient));
yth.IsBackground = true;
yth.Start();
this.buttonStart.Enabled = false;
this.buttonStop.Enabled = true;
}
public void ListenClient()
{
while (listenerRun)
{
allDone.Reset();
listener.BeginAccept(new AsyncCallback(AcceptCallBack), listener);
allDone.WaitOne();
}
}
private void AcceptCallBack(IAsyncResult ar)
{
try
{
allDone.Set();
Socket sok = (Socket)ar.AsyncState;
Socket client = sok.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = client;
if(client.Available>0)
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallBack), state);
}
catch (Exception Err)
{
throw Err;
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
}
System.Net.IPAddress[] addressList;
private void Form1_Load(object sender, EventArgs e)
{
this.buttonStop.Enabled = false;
dtu.Columns.Add("hostname", Type.GetType("System.String"));
dtu.Columns.Add("remoteip", Type.GetType("System.String"));
dtu.Columns.Add("activetime", Type.GetType("System.DateTime"));
this.dataGridView1.DataSource = dtu;
addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
IPAddress ipaddress = addressList[0];
IPEndPoint ipendpoint = new IPEndPoint(ipaddress, port);
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(ipendpoint);
listener.Listen(50);
}
private void buttonStop_Click(object sender, EventArgs e)
{
this.buttonStop.Enabled = false;
this.buttonStart.Enabled = true;
listenerRun = false;
}
private void ReadCallBack(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket hander = state.workSocket;
int readbyte = hander.EndReceive(ar);
if (readbyte > 0)
{
string strmsg = Encoding.UTF8.GetString(state.buffer, 0, readbyte);
hander.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallBack), state);
}
}
catch (Exception Err)
{
throw Err;
}
}
客户端
private Socket sok;
private IPAddress ipaddress;
private IPEndPoint ipendpoint;
int port = 54321;
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(Get_TEst));
th.IsBackground = true;
th.Start();
this.buttonStart.Enabled = false;
}
private void Get_TEst()
{
try
{
ipaddress = IPAddress.Parse("192.168.1.102");
ipendpoint = new IPEndPoint(ipaddress, port);
sok = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sok.Connect(ipendpoint);
while (true)
{
byte[] sendbyte = new byte[1024];
string str = hostName + "|" + clientIP;
//string str = " ";
sendbyte = Encoding.UTF8.GetBytes(str);
sok.Send(sendbyte);
Thread.Sleep(5000);
}
}
catch (Exception Err)
{
throw Err;
}
}
string clientIP = "";
string hostName = "";
private void Form1_Load(object sender, EventArgs e)
{
clientIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
hostName = Dns.GetHostName();
}
当客户端退出的时候 出现错“远程主机强迫关闭了一个现有的连接”
经过调试 错误出现在这个函数里
private void ReadCallBack(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket hander = state.workSocket;
int readbyte = hander.EndReceive(ar);
if (readbyte > 0)
{
string strmsg = Encoding.UTF8.GetString(state.buffer, 0, readbyte);
hander.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallBack), state);
}
}
catch (Exception Err)
{
throw Err;
}
}
请教怎么解决这个问题啊?
相关推荐
Eclipse ADB报错:Adb connection Error远程主机强迫关闭了一个现有的连接
Eclipse ADB报错:Adb connection Error远程主机强迫关闭了一个现有的连接
C# UDP通信,远程主机强迫关闭了一个现有连接。解决方案和相关代码
C# UDP通信报错:远程主机强迫关闭了一个现有的连接。 是windows的一个bug,在linux下没有这个bug。 将代码加入程序即可解决
Android ADT远程主机强迫关闭了一个现有的连接 Connection attempts: 1 解决方法-附件资源
Android ADT远程主机强迫关闭了一个现有的连接 Connection attempts: 1 解决方法-附件资源
android工具adb
android开发中,出现“远程主机强迫关闭了一个现有连接”错误是因为android sdk工具中的adb.exe的最大连接数比较少,如果虚拟机中跑的线程比较多,就会出现该错误,该adb对最大连接数进行了修改,直接覆盖掉sdk 中自带的adb即可。
python socket 超时设置 errno 10054
python socket.error: [Errno 10054] 远程主机强迫关闭了一个现有的连接。问题解决方案: 前几天使用python读取网页。因为对一个网站大量的使用urlopen操作,所以会被那个网站认定为攻击行为。有时就不再允许下载。导致urlopen()后,request.read()一直卡死在那里。最后会抛出errno 10054. 这个错误是connection reset by peer.也就是传说的远端主机重置了此连接。原因可能是socket超时时间过长;也可能是request = urllib.request.urlopen(url)之后,没有进行request.cl


