You are on page 1of 3

0

I want to create server socket where users will always be to connect, and to send
message only when I send them specific message,"CLIENT_ACTION"( they will know
when, client side is not important now). So basically I know when to expect message
from the client. But now, will they be able to connect on server socket, even if my
logic is running. Picture it like this: Chat starting when at least 2 clients
connects to server, but it has specific tasks to do with current connected
clients,and new clients should be able to connect to server, but not to participate
in current "round" until it is done.Then everything starts over again, with new
clients who connected in the meantime.

Will this code works as expected ? Will clients be able to connect while round is
ongoing?
How will I know that client will respond successfully? What if he doesn't answer
e.g in 30 sec, will I recieve empty buffer?

How many clients I can handle ?

I generally need help with synchronous server settings becaues I couldn't find a
lot of documentation. This code is probably bad so I would like suggestions from
your side and is this achievable at all

class Program
{
private static Socket _serverSocket;
private static readonly List<Socket> _clientSockets = new List<Socket>();
private const int BUFFER_SIZE = 2048;
private const int PORT = 1582;
private static readonly byte[] buffer = new byte[BUFFER_SIZE];
private static bool isRoundActive = false;

static void Main(string[] args)


{
Console.Title = "Server";
StartServer();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(StartRoundHandler);
timer.Interval = 1000;
timer.Enabled = true;

System.Timers.Timer connectionTimer = new System.Timers.Timer();


connectionTimer.Elapsed += new ElapsedEventHandler(CheckForConnectionsHandler);
connectionTimer.Interval = 2000;
connectionTimer.Enabled = true;
while (true)
{
}
}

private static void CheckForConnectionsHandler(object sender, ElapsedEventArgs e)


{
try
{
Socket client = _serverSocket.Accept();
if (!_clientSockets.Contains(client))
{
_clientSockets.Add(client);
Console.WriteLine("Client connected and added to lobby");
}
}
catch (Exception ex)
{

}
}

private static void StartRoundHandler(object sender, ElapsedEventArgs e)


{
if (!isRoundActive && _clientSockets.Count > 0)
StartRound();
}

private static void StartRound()


{
isRoundActive = true;
// Logic
isRoundActive = false;
}

private static void StartServer()


{
try
{
_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT));
_serverSocket.Listen(0);
Console.WriteLine("Setting up server on port " + PORT);
}
catch (Exception ex)
{

}
}

private static string RecieveAction(Socket client)


{
try
{
if (client.Connected)
{
// I need response from each client
int received = client.Receive(buffer);
var action = Encoding.UTF8.GetString(buffer);

return action;
}
}
catch (Exception ex)
{
return "something";
}
}

private static void AskClientForAction(Socket client)


{
var command = "CLIENT_ACTION";
byte[] data = Encoding.ASCII.GetBytes(command);
client.Send(data);
}

private static void SendMessageToAllClients(string message)


{
byte[] data =
Encoding.ASCII.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(message));
_clientSockets.ForEach(x => x.Send(data));
}

You might also like