Question

I'm trying to make a class for easy Client/Server connections. Inside this class is a Server-Side Client, but it won't pick up things written to the server NetworkStream. In case you're wondering, I intend to have all data go through the server program before sending it out to the Client, so I need something to process all the data on the NetworkStream (Hence Server-Side Client). Here's my Class:

public class ConnectionServer
{
    public readonly IPAddress ServerIP;
    public readonly IPAddress LocalIP;
    public readonly int Port;
    private TcpListener ServerIn;
    private Socket ServerSocket;
    private TcpClient ServerSideClient;
    private NetworkStream NetStream;
    public StreamReader ServerInput;
    public StreamWriter ServerOutput;

    public ConnectionServer(int port)
    {
        try
        {
            Port = port;
            ServerIP = IPAddress.Parse(NetHelper.GetPublicIP());
            LocalIP = IPAddress.Parse(NetHelper.GetLocalIP());

            ServerIn = new TcpListener(IPAddress.Any, Port);
            ServerIn.Start();                
            Console.WriteLine("Server Started At: " + ServerIP.ToString() + ":" + Convert.ToString(Port));
            ServerSideClient = new TcpClient("localhost", Port);
            ServerSocket = ServerIn.AcceptSocket();
            Console.WriteLine("Server-Side Client Socket Accepted.");
            NetStream = ServerSideClient.GetStream();
            ServerInput = new StreamReader(NetStream);
            ServerOutput = new StreamWriter(NetStream);
            ServerOutput.AutoFlush = true;
            Console.WriteLine("All Streams Initialized.");
            ServerOutput.WriteLine("Testing Server-Side Client.");                
            Console.WriteLine("Test Message Sent.");
            Console.WriteLine(ServerInput.ReadLine());
        }

        finally
        {

        }
    }

    ~ConnectionServer()
    {
        ServerInput.Dispose();
        ServerSideClient.Close();
    }

}

It doesn't write anything on Console.WriteLine(ServerInput.ReadLine());. I can't figure out why it won't pick up the message off the stream. Help please?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top