我在服务器和客户端之间创建了一个TCP连接,但是在几次请求之后,我收到此错误消息:
System.Net.Sockets.SocketException:'现有连接被远程主机强行关闭'
在此行上:
_clientSocket.Send(buffer,0,buffer.Length,SocketFlags.None);
如何解决此错误?
服务器代码
private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static List<Socket> _clientSockets = new List<Socket>();
private static byte[] _buffer = new byte[1024];
protected override void OnStart(string[] args)
{
SetupServer();
}
private static void SetupServer()
{
Log.writeEventLog("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
_serverSocket.Listen(1);// max listen client
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void AcceptCallback(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
Log.writeEventLog("Client connected");
socket.BeginReceive(_buffer,0,_buffer.Length,SocketFlags.None,new AsyncCallback(ReceiveCallback),socket);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] dataBuf = new byte[received];
Array.Copy(_buffer,dataBuf,received);
string text = Encoding.ASCII.GetString(dataBuf);
Log.writeEventLog("TEXT RECEIVED: " + text);
string response = string.Empty;
if(text.ToLower() != "get authentication key")
{
response = "Invalid Request";
}
else
{
response = "TEST";
}
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
private static void SendCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
客户代码
private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
LoopConnect();
SendLoop();
Console.ReadLine();
Console.ReadKey();
}
private static void SendLoop()
{
while (true)
{
Console.WriteLine("Enter a request: ");
string req = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(req);
_clientSocket.Send(buffer,0,buffer.Length,SocketFlags.None);
var receivedBuffer = new byte[2048];
int rec = _clientSocket.Receive(receivedBuffer,SocketFlags.None);
byte[] data = new byte[rec];
Array.Copy(receivedBuffer, data, rec);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(data));
}
}
private static void LoopConnect()
{
int attempts = 0;
while (!_clientSocket.Connected)
{
try
{
attempts++;
_clientSocket.Connect(IPAddress.Loopback, 100);
}
catch (SocketException) {
Console.Clear();
Console.WriteLine("Connection attenpts: "+ attempts.ToString());
}
}
Console.Clear();
Console.WriteLine("Connected");
}
答案 0 :(得分:0)
更新:
socket.BeginSend(data, 0, data.Length, SocketFlags.None,
new AsyncCallback(SendCallBack), socket);