When using a socket, a prompt box pops up, the content is An existing connection was forcibly closed by the remote host

  • Thread starter Thread starter iDontKnowEverything
  • Start date Start date
I

iDontKnowEverything

Guest
I have read the answers to other questions but can't solve my problem.My code can sometimes run and sometimes this error pops up, I can't solve it.I have tried many ways that don't work.My project is used to open an external game in the wpf program and set the difficulty. This is the code for the socket part.

public delegate void ListenSuccessHanlder(Socket serverSocket);
public delegate void ConnectSuccessHanlder(Socket clientSocket);
public delegate void ReceiveHanlder(string msg);
public sealed class SocketHelper
{
private static volatile SocketHelper instance;
private static object syncRoot = new Object();
private static string socketIP = "127.0.0.1";
private static int port = 12356;
private Socket serverSocket;
private Socket clientSocket;
private int socketFlag = 0;
private Thread listenThread;
private Thread receiveThread;
private Thread sendThread;
private static bool isSendFinsh = true;

public event ListenSuccessHanlder ListenSuccessHanlder;
public event ConnectSuccessHanlder ConnectSuccessHanlder;
public event ReceiveHanlder ReceiveHanlder;


public static SocketHelper Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new SocketHelper();
}
}
return instance;
}
}
public void ConnectServer(string _socketIP, int _port)
{
try
{

socketIP = _socketIP;
port = _port;
IPAddress ip = IPAddress.Parse(socketIP);
serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, port));
serverSocket.Listen(1);
socketFlag = 1;
listenThread = new Thread(ListenClientConnect);
listenThread.IsBackground = true;
listenThread.Start();
if (ListenSuccessHanlder != null)
ListenSuccessHanlder(serverSocket);


}
catch (Exception ex)
{


}
}
public void Close()
{
ListenSuccessHanlder = null;
ReceiveHanlder = null;
ConnectSuccessHanlder = null;
if (serverSocket == null)
return;

if (!serverSocket.Connected)
return;

try
{
serverSocket.Shutdown(SocketShutdown.Both);
}
catch
{
}

try
{
serverSocket.Close();
}
catch
{
}
try
{
if (receiveThread.IsAlive)
receiveThread.Abort();
}
catch
{

}
try
{
if (listenThread.IsAlive)
listenThread.Abort();
}
catch
{

}
}

private void ListenClientConnect()
{
try
{
clientSocket = serverSocket.Accept();
receiveThread = new Thread(ReceiveMessage);
receiveThread.IsBackground = true;
receiveThread.Start(clientSocket);
socketFlag = 2;
if (ConnectSuccessHanlder != null)
ConnectSuccessHanlder(serverSocket);

}
catch (Exception ex)
{

}
}


private void Send(string str)
{
if (socketFlag != 2)
{

return;
}
isSendFinsh = false;
byte[] buffer = Encoding.UTF8.GetBytes(str);
clientSocket.Send(buffer);

}

private void ReceiveMessage(object o)
{
Socket mClientSocket = o as Socket;
try
{

while (true)
{


string recStr = "";
byte[] recByte = new byte[8192 * 8];
int receiveNumber = mClientSocket.Receive(recByte);
UTF8Encoding utf8 = new UTF8Encoding();
recStr += utf8.GetString(recByte, 0, receiveNumber);
if (ReceiveHanlder != null)
ReceiveHanlder(recStr);


}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}



}

public void SendSocketMsg(string str)
{
if (socketFlag != 2)
{

return;
}
Console.WriteLine(str);
byte[] buffer = Encoding.UTF8.GetBytes(str);
clientSocket.Send(buffer);
//socketTasks.Push(str);
}




}

Continue reading...
 
Back
Top