user4329506
user4329506

Reputation:

TcpClient/TcpServer/Listener, Server stops reading or client stops after 30 seconds

Client

class Client
{
    public TcpClient client;
    public StreamWriter server;
    public ServerPlayer serverPlayer;
    public Player player;
    public void Connect(Player p)
    {
        player = p;
        serverPlayer = new ServerPlayer();
        client = new TcpClient(AddressFamily.InterNetwork);
        client.Connect("Don't wory my IPV4 Is Here", 8888);
        NetworkStream stream = client.GetStream();
        server = new StreamWriter(stream);
        Timer t = new Timer(Send, null, 0, 10);
    }
    public void Send(Object o)
    {
        server.WriteLine("Hello I am bob");
        server.Flush();

    }

}

Server

class TcpServerConnection
{
    private static TcpListener tcpListener;
    static void Main(string[] args)
    {
        tcpListener = new TcpListener(IPAddress.Any, 8888);
        tcpListener.Start();
        Console.WriteLine("Server started.");

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = tcpListener.AcceptTcpClient();

            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

    private static void HandleClientComm(object client)
    {
        while (true)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();
            StreamReader server = new StreamReader(clientStream);
            Console.WriteLine(server.ReadLine());
        }


}
}

Problem

Track of events:

Started!

Hello I am bob

Hello I am bob

(Many Hello I am bobs later)

Hello I am bob

(Hello I am bobs just stopped)

(About 30 seconds)

Don't know if it because the client stops sending or server stops receiving or both!? But once this is ran about 30 seconds into this the server stops getting the send information. No error is thrown simply it just doesn't send.

Upvotes: 0

Views: 721

Answers (3)

user4329506
user4329506

Reputation:

Found out it was my internet. After my google-fu. I set my port for my server to be TCP open. Found out my router was getting suspicious after a spam of messages going into a random port. IP:IPV4 PORT:8888 SETTINGS:OPEN

Upvotes: 1

ssett
ssett

Reputation: 432

Since you are creating one and only one connection to the Server, and then sending messages constantly in the Client, accordingly when handling the client messages in the Server program, you should accept a TcpClient and then read messages in a loop, instead of accepting a Tcplient and reading a message all in a loop.

private static void HandleClientComm(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();
    StreamReader server = new StreamReader(clientStream);
    while (true)
    {
        Console.WriteLine(server.ReadLine());
    }
}

Upvotes: 0

kmacdonald
kmacdonald

Reputation: 3471

Ok in your ServerConnection object the first while loop is fine. but the one in your handler is going to cause some problems try this:

private static void HandleClientComm(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();
    using(var stream = new StreamReader(clientStream))
    {
       while (stream.Peek() >= 0) 
       {
           Console.WriteLine(server.ReadLine());
       }
    }
}

Upvotes: 0

Related Questions