Your immediate problem is that you're trying to use the same stream for reading and writing. NetStream
is the input stream--the data from the client. If you want to write to the socket, you need to create a stream from the socket.
As it's written, you're writing to the client stream. You need to write to the server stream.
What I don't understand is why you're using TcpClient
on the client side, and raw sockets on the server side after creating a TcpListener
. You'd be better off using AcceptTcpClient. Then you could write to that client's stream and read from the other stream.
To create a NetworkStream
from the socket, use the constructor. I strongly suggest, though, that you switch to AcceptTcpClient
instead.