已建立的连接被主机中的软件中止
An established connection was aborted by the software in host machine
我在某些主题中找不到答案。
我有一个 client/server 代码。
我在单独的程序中有客户端和服务器端。
这是客户端:
try
{
byte[] bytes = new byte[1024];
string text = "";
text = uitbtextmsg.Text;
byte[] msg = Encoding.ASCII.GetBytes(text);
int bytesSent = User.Send(msg);
int bytesRec = User.Receive(bytes);
uirtbmsg.Text = uirtbmsg.Text + '\n' + " " + text;
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
这是当我连接到服务器并且我想用那个套接字聊天时使用的。
这是我的连接:
byte[] bytes = new byte[1024];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[2];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
Socket User = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
User.Connect(remoteEP);
这是服务器端:
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[2];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10000);
while (true)
{
Socket handler = listener.Accept();
data = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
TextRecieved = data;
Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
textbox.Start();
break;
}
//uirtbreport.Text = data;
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
当客户端第一次向服务器发送消息时,这个过程就可以了!但是当它发送另一条消息时它会抛出异常:
System.Net.Socket.SocketException(0x80004005):An established
connection was aborted by the software in your host machine at
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags) at
System.Net.Sockets.Socket.Receive(Byte[] buffer)\r\n at
Client.Form1.uibtnsendmsg_Click(Object sender, EventArgs e) in ...
可能是因为你打破了 while 循环(看我的评论)
while (true)
{
Socket handler = listener.Accept();
data = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
TextRecieved = data;
Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
textbox.Start();
// -----------> HERE!!!!!!
break;
}
//uirtbreport.Text = data;
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
为什么要在 'broken' 循环中创建线程?
此外,不要忘记这是一个流套接字,这意味着 'packets' 可以作为一个缓冲区发送或拆分为多个 'packets'.
我不清楚 textbox.Start() 做了什么,但在我看来,在服务器从客户端读取一些数据后,它 中断 "while (true)" 并关闭套接字连接。
这就是为什么您只能发送一条消息。
Socket handler = listener.Accept();
...
while (true)
{
.... read data
textbox.Start();
break;
}
/* Here you close the connection */
handler.Shutdown(SocketShutdown.Both);
handler.Close();
我想你想在同一个套接字上建立一个持久的 tcp 连接。
只是不要中断循环而不是关闭套接字。
我在某些主题中找不到答案。 我有一个 client/server 代码。 我在单独的程序中有客户端和服务器端。 这是客户端:
try
{
byte[] bytes = new byte[1024];
string text = "";
text = uitbtextmsg.Text;
byte[] msg = Encoding.ASCII.GetBytes(text);
int bytesSent = User.Send(msg);
int bytesRec = User.Receive(bytes);
uirtbmsg.Text = uirtbmsg.Text + '\n' + " " + text;
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
这是当我连接到服务器并且我想用那个套接字聊天时使用的。 这是我的连接:
byte[] bytes = new byte[1024];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[2];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
Socket User = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
User.Connect(remoteEP);
这是服务器端:
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[2];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10000);
while (true)
{
Socket handler = listener.Accept();
data = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
TextRecieved = data;
Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
textbox.Start();
break;
}
//uirtbreport.Text = data;
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
当客户端第一次向服务器发送消息时,这个过程就可以了!但是当它发送另一条消息时它会抛出异常:
System.Net.Socket.SocketException(0x80004005):An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.Socket.Receive(Byte[] buffer)\r\n at Client.Form1.uibtnsendmsg_Click(Object sender, EventArgs e) in ...
可能是因为你打破了 while 循环(看我的评论)
while (true)
{
Socket handler = listener.Accept();
data = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
TextRecieved = data;
Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
textbox.Start();
// -----------> HERE!!!!!!
break;
}
//uirtbreport.Text = data;
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
为什么要在 'broken' 循环中创建线程?
此外,不要忘记这是一个流套接字,这意味着 'packets' 可以作为一个缓冲区发送或拆分为多个 'packets'.
我不清楚 textbox.Start() 做了什么,但在我看来,在服务器从客户端读取一些数据后,它 中断 "while (true)" 并关闭套接字连接。 这就是为什么您只能发送一条消息。
Socket handler = listener.Accept();
...
while (true)
{
.... read data
textbox.Start();
break;
}
/* Here you close the connection */
handler.Shutdown(SocketShutdown.Both);
handler.Close();
我想你想在同一个套接字上建立一个持久的 tcp 连接。
只是不要中断循环而不是关闭套接字。