we Have A serverPC and more ClientPC this system is about a server/client of CafeGame/GameNet .
My Problem: when server is Online And after that, the clients are connected all things is OK! But When Client is Online And after that, the Server are connected We Have Problem.
An existing connection was forcibly closed by the remote host
Server Code
{
public Dictionary<string, ClientData> lstClients = new Dictionary<string, ClientData>();
private delegate void CommandReceivedEventHandler(DataPacket.DataPacket data, ClientData sender);
private event CommandReceivedEventHandler CommandReceived;
private void AcceptSocket(IAsyncResult ar)
{
TcpListener listener = (TcpListener)ar.AsyncState;
try
{
ClientData clientData = new ClientData(listener.EndAcceptSocket(ar));
int i = clientData.Socket.RemoteEndPoint.ToString().IndexOf(':');
string IPAddress = clientData.Socket.RemoteEndPoint.ToString().Substring(0, i);
clientData.IsConnected = true;
if (lstClients.ContainsKey(IPAddress))
{
lstClients.Remove(IPAddress);
lstClients.Add(IPAddress, clientData);
dicClientUserControl[IPAddress].clientData = null;
dicClientUserControl[IPAddress].clientData = clientData;
dicClientUserControl[IPAddress].clientData.IsConnected = true;
}
else
{
Dispatcher.Invoke((Action)(() =>{u = new PCu(clientData, this, 0);}));
clientData.IsOnline = false;
dicClientUserControl.Add(IPAddress, u);
lstClients.Add(IPAddress, clientData);
Dispatcher.Invoke((Action)(() => { WarpPanelClient.Children.Add(u); }));
}
clientData.Socket.BeginReceive(clientData.ReceiveBuffer, 0, clientData.ReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(Receive), clientData);
listener.BeginAcceptSocket(AcceptSocket, listener);
}
catch (Exception ex)
{
listener.BeginAcceptSocket(AcceptSocket, listener);
}
}
private void Receive(IAsyncResult ar)
{
ClientData clientData = (ClientData)ar.AsyncState;
SocketError err;
int byteReceived = clientData.Socket.EndReceive(ar, out err);
if (err != SocketError.Success)
{
byteReceived = 0;
clientData.IsConnected = false;
clientData.IsOnline = false;
}
clientData.IsConnected = true;
DataPacket.DataPacket data = DataPacket.DataPacket.FromByte(clientData.ReceiveBuffer);
CommandReceived.Invoke(data, clientData);
try
{
clientData.Socket.BeginReceive(clientData.ReceiveBuffer, 0, clientData.ReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(Receive), clientData);
}
catch (SocketException ex)
{
clientData.IsConnected = false;
clientData.IsOnline = false;
lstClients[clientData.AddressText] = clientData;
//dicClientUserControl[clientData.AddressText] = u;
}
catch (Exception ex)
{
}
}
private bool SendCMD(DataPacket.DataPacket data)
{
try
{
else if (clientData.IsConnected)
clientData.Socket.Send(data.ToBytes());//?? Crash
return (true);
}
catch (Exception ex)
{
//An existing connection was forcibly closed by the remote host
IS_CONNECTED(false);
return false;
}
}
}
Class in Server
public class ClientData
{
public byte[] ReceiveBuffer = new byte[10240];
DataPacket.DataPacket Data = new DataPacket.DataPacket();
bool isConnected;// Connected Or Not
bool isOnline = false;// Loged in Or Not
public Socket Socket { get; private set; }
public string NameClient { get; set; }
public int Ver { get; set; }
public int id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public ClientData(Socket socket)
{
this.Socket = socket;
int i = Socket.RemoteEndPoint.ToString().IndexOf(':');
AddressText = Socket.RemoteEndPoint.ToString().Substring(0, i);
}
}
Client Code
bool isConnected;
public bool Connect(IPAddress IP, int Port, string SystemName)
{
try
{
if (isConnected == false)
{
SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketClient.Connect(IP, Port);
isConnected = true;
return true;
}
else
return true;
}
catch (Exception ex)
{
if (!isConnected)
{
this.Dispatcher.Invoke((Action)(() =>
{
lblError.Text = "SERVER IS OFFLINE ... !\n" + ex.Message;
}));
return false;
}
else
return true;
}
}
where is the my problem ?