Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have the following code:
C#
private void RunCommunicationNewClient(object obj)
{
    var client = (Socket)obj;
    if (client.Connected)
    {
        while (true)
        {
            {
                var buffer = new byte[1024];

                client.Receive(buffer);
                foreach (var sk in _sockets)
                {
                    sk.Send(buffer, buffer.Length, SocketFlags.None);
                }
            }
        }
    }
}


when I run everything is running properly. But when I get the error in the client and server at the following:
C#
while (true)
        {
            {
                var buffer = new byte[1024];

                client.Receive(buffer);//An existing connection was forcibly closed by the remote host

                foreach (var sk in _sockets)
                {
                    sk.Send(buffer, buffer.Length, SocketFlags.None);
                }
            }
        }


// Client

C#
private void ButtonLoginClick(object sender, EventArgs e)
     {
         _buffer = new byte[1024];
         _buffer = Encoding.ASCII.GetBytes(textUserName.Text);
         _socket.Send(_buffer);
         _buffer = new byte[1024];
         _buffer = Encoding.ASCII.GetBytes(textPassword.Text);
         _socket.Send(_buffer);
     }
     private void ConnectToServer()
     {

         try
         {
             _ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.33"), 2009);
             _socket.Connect(_ipEndPoint);
             var vThreadConnect = new Thread(RecevieceData) { IsBackground = true };
             vThreadConnect.Start(_socket);
         }
         catch
         {
             ConnectToServer();
         }

     }
     private void RecevieceData(object obj)
     {
         var socket = (Socket)obj;
         while (true)
         {
             try
             {
                 _buffer = new byte[1024];
                 socket.Receive(_buffer);
                 var info = Encoding.ASCII.GetString(_buffer);
                 MessageBox.Show(Application.ProductName + info);
             }
             catch
             {

                 Disconnect();
                 MessageBox.Show(Resources.Form1_RecevieceData_da_thoat_ket_noi);
                 break;
             }

         }
     }

     private void Form1Load(object sender, EventArgs e)
     {
         _threadConnect = new Thread(ConnectToServer) { IsBackground = true };
         _threadConnect.Start();

     }
     private void Disconnect()
     {
         _socket.Close();
         _threadConnect.Abort();
         Environment.Exit(0);

     }
     protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
     {
         Environment.Exit(1);


     }
Posted
Comments
Pheonyx 23-May-13 6:27am    
Sorry, I have no idea what your question is......
What error? on what line?
Saying what?

Use the improve question to give more details.
Nelek 23-May-13 6:32am    
Read carefully. The error is in the second codesnippet, line "Client.Recieve(Buffer)" and the error message is given as comment after the code.
Richard MacCutchan 23-May-13 6:43am    
The message tells you what the problem is; you need to investigate why it occurred.
MuhammadUSman1 23-May-13 7:05am    
In your code. Server is waiting to Recieve Some data but that time you close your client.that's why Exception occur.
MuhammadUSman1 23-May-13 7:06am    
You need to debug and fix error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900