I'm trying to create a simple TCP client. The user enters the host, port, and a send statement(GET, HEAD, etc).

Then the Headers and everything else is returned back in a textbox. When I send the information, it pauses for a second, then it just does the crlf.

sentText is where I entered my send statement. textOutput is where everything is to be displayed.

connectSocket.Connect(host, port);

            System.IO.StreamReader connectionRead
                = new System.IO.StreamReader(new NetworkStream(connectSocket));

            //sendString(connectSocket, sendText);
            connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(sendText.Text));
            this.textOutput.AppendText(connectionRead.ReadLine() + "\r\n");

I have also tried

connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(sendText.Text));
           
            while (connectionRead.Peek() > 0)
            {
               this.textOutput.AppendText(connectionRead.ReadLine() + "\r\n");

            }
            connectSocket.Close();

I'm not really sure what that does, but I don't even the the crlf with that.

Recommended Answers

All 6 Replies

while (connectionRead.Peek() > 0) should be while (connectionRead.Peek() >= 0) This will probably not help but it's a start.
I assume "textOutput" is a textbox.
Why don't you rename it to something more usefull like you should do for the "sendText" textbox? Find & Replace is a build in utility just for that purpose. textOutput and sendText are not TEXT they are TEXTBOXES. Name them accordingly.

Thanks Danny. I put the = in and it did the same thing.

I tried changing the code to this

Socket connectSocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            connectSocket.Connect(host, port);

            System.IO.StreamReader connectionRead
                = new System.IO.StreamReader(new NetworkStream(connectSocket));

            connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(sendText.Text));
           this.textOutput.AppendText(connectionRead.ReadLine() + "test\r\n");
            while (connectionRead.Peek() >= 0)
            {
               

            }
            connectSocket.Close();

This time is sat there for a second, then displayed "test".
Sorry, I haven't renamed my textboxes yet.

If the stream that ReadLine is reading does not contain a carriage return or linefeed or a combination of both "\r\n" a null reference is returned if the end of the stream is reached.
So it seems likely that on the sendside you have to put in a \r or \n or so.

And really do something about your variabale names!
Come on man... sendText.Text!
Are you trying to get me a heartdisease?

What are you connecting to? Does it need line feeds at the end of your Send before it will respond?

Lol agree with ddanbe, like we told you yesterday use
tbSendText.Text

And just to shed some light on what the other guys are saying about the crlf (\r\n), the socket you are connected to needs to know when you are done sending. Since you can send 5 bytes, then later 10 bytes for example, you must tell the connected socket when you are done. The crlf typically is used to say "i am done", then the response is sent back from the connected socket.

commented: respect +1

It works! Thanks guys. I changed it back to the first way and added the extra \r\n.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.