Server Question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Server Question

Me1Me1 Posts: 29Member
Hi,
I have a Java background and I thought I would try my hand at some C#, but something seems to have me puzzled.
Basically I am trying to make a server for a game which can have multiple Players. and at any time any player could send a message to the server but the server will always be sending messages out. I have tried doing it with The following code, but as soon as i make a client even connect and disconnect (without sending any messages to the server) the server's ReadLine() command seems to keep being called. This has confused me since in Java it is only called when a message is sent to the server as in it is Listening. Any help would be greatly appriciated. Or if someone could direct me to a page that could help cause most server tutorials i have found have all been solo connection with listen and reply set ups. PS. Sorry about the formatting of the code

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Test{
public class GameServer{

private volatile bool _shouldStop;


public static void Main(){
new GameServer().runServer();
}

private void runServer(){
Int32 port = 1234;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

TcpListener tcpListener = new TcpListener(localAddr, port);
tcpListener.Start();

ThreadStart job1 = new ThreadStart(DoWork);
Thread workerThread1 = new Thread(job1);
workerThread1.Start();

bool serverRunning = true;
while (serverRunning){
ClientConnection connectionTemp = new ClientConnection(tcpListener.AcceptTcpClient());
ThreadStart job2 = new ThreadStart(connectionTemp.DoWork);
Thread workerThread2 = new Thread(job2);
workerThread2.Start();
}
}

public void DoWork(){
while (!_shouldStop){
//cycle through players and write output
}
}
public void RequestStop(){
_shouldStop = true;
}

private class ClientConnection{

TcpClient connection;
NetworkStream networkStream = null;
StreamWriter streamWriter = null;
StreamReader streamReader = null;

private volatile bool _shouldStop;

public ClientConnection(TcpClient connection){
this.connection = connection;
Console.WriteLine("Client Connected");
try{
networkStream = connection.GetStream();
streamWriter = new StreamWriter(networkStream);
streamReader = new StreamReader(networkStream);
} catch (Exception e){
}
}


public void DoWork(){
while (!_shouldStop){
string line = streamReader.ReadLine();
Console.WriteLine(" line read '" + line + "'");
Thread.Sleep(1000); // only used to slow down output on screen
}
}
public void RequestStop(){
_shouldStop = true;
}


private void close(){
streamReader.Close();
streamWriter.Close();
connection.Close();
}
}
}
}

Sign In or Register to comment.