1
votes

I couldn't find my answer in some topics. I have a client/server code. I have a client side and server side in a separate programs . This is client side:

 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());
        }

This is for when I am connected to server and I want to chat with that socket. This is my connection:

            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);

And this is server side:

 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());
        }

When client first sends a message to server this procedure is ok! But when it send another message it will throw an Exception:

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 ...

3

3 Answers

0
votes

Probably because you're breaking the while loop (look my comment)

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();
}

Why do you create a thread within the 'broken' loop?

Also, don't forget this is a streaming socket, which means that 'packets' can be sent as one buffer or split into multiple 'packets'.

0
votes

It's not clear to me what textbox.Start() does, but it seems to me that after the server reads some data from the client, it breaks out of the "while (true)" and closes the socket connection. This is why you are only able to send a single message.

Socket handler = listener.Accept();
...
while (true)
{
    .... read data
    textbox.Start();
    break;
}

/* Here you close the connection */
handler.Shutdown(SocketShutdown.Both);
handler.Close();
0
votes

I think you want a persistent tcp connection on same socket.

Just don't break the loop not to shutdown the socket.