Questions about my TCP Server

Started by
1 comment, last by Xanather 11 years, 9 months ago
Edit: everything is in C#.

So I finally developed a TCP server, and I must say I just love looking at the code. In the end it turned out more or less like a IRC instead of a game server but definitely has the same structure that ill use for when I eventually code a game. The server basically goes through a loop every 10 milliseconds, looking at all connected clients and processing all available data/transmitting according. This means there are only 3 programmed threads running at one meaning the system could probably support more than 300+ clients at once (I heard of mass TCP connections may be a problem though). I think it should also support a throughput of infinite (now Im just talking about this programmically; on the hardware level though there are of course limitations.). There are a few things that worry me though:

I'm going to type a basic list below, anyone that has knowledge/a answer in any question/problem I have mentioned below please read about the full problem further down (accordingly to the same question number). All replies are extremely appreciated!
1. It feels like I am detecting if a client has lost connection incorrectly.
2. Byte buffers are getting sent around everywhere just to process data, does this use too much processing power?
3. I really need a alternative to using Thread.Sleep(10) in the server program (the server is a console application).

[size=8]1. I feel like I'm doing something extremely hackish in determining if the connection has dropped out, on the server side I have something like this:
[source lang="csharp"]
if (clients.WRITINGready)
{
try
{
clients.stream.Write(clients.WRITINGbytebuffer, 0, clients.WRITINGpacketsize);
clients.WRITINGready = false;
clients.WRITINGbytebuffer = new byte[MaximumBufferSize.Size];
clients.WRITINGpacketsize = 0;
}
catch (System.IO.IOException)
{
clients.anyerrors = true;
}
}[/source]

Now this method is called to every client connected. If there was a problem writing to the TCP stream the code immediately changes the client's anyerrors boolean to false. When the same thread goes back to collecting all available data from each client and sees the anyerrors boolean as true, it removes the client and does all nessassary proccesing to remove this client. I am no expert at TCP but if try to write to a TCP stream that immidately returns with a write IO exception does this mean the connection HAS been closed on the other end, if not what should I do to tackle this? Keep the data to be sent in the application buffer, try sending a few more times in the next few loops and after a specified amount of times attempted that have still failed close the connection?

On the client side I feel like im doing something even more hackish/incorrect. On the client side, the client is constantly trying to receive all available data on a seperate thread. For example calling stream.Read() and blocking until data has been received. However, this call throws a exception when (which is what i assume) is the connection has closed (i.e. server has dropped out). I basically do the same in the server, which is to catch this System.IO.IOException and disconnect from the server accordinly.

[size=8]2. Im exchanging, creating, manipulating and removing type buffers everywhere! i.e. creating a buffer that contains all newly received data, then moving this buffer, using .Nets Buffer class, into another larger buffer for later processing once all data of a message has been received. This is just part of what my code does, it does much more, and therefore there is probably to much code in order to post the source here. Id assume this is ok if the byte buffer sizes were less than 1,000 bytes? What if I start manipulating buffers that contained 64,000 bytes? These are all assumptions though... I feel like ive done the correct method, I am just not certain. But anyway heres a code snippet of the server for additional clarification of what is happening (this is only a part of the code contained in a method so it wont make any sence probably):
[source lang="csharp"]
connection.gotpacketheader = false;
byte[] process_bytes = new byte[connection.packetsize - 5]; //only get actual message, not length or type
Buffer.BlockCopy(connection.bytebuffer, 5, process_bytes, 0, connection.packetsize - 5); //debug
if (connection.bytebuffersize > connection.packetsize) //save any remaining bytes for later use
{
byte[] save_bytes = new byte[connection.bytebuffersize - connection.packetsize];
Buffer.BlockCopy(connection.bytebuffer, connection.packetsize, save_bytes, 0, connection.bytebuffersize - connection.packetsize);
connection.bytebuffer = new byte[MaximumBufferSize.Size]; //clear buffer
Buffer.BlockCopy(save_bytes, 0, connection.bytebuffer, 0, connection.bytebuffersize - connection.packetsize);
connection.bytebuffersize = connection.bytebuffersize - connection.packetsize;
tryagain = true;
}[/source]


[size=8]3. The .net's static Thread.Sleep method ive heard is not accurate at all, and can actually cause the method to block for more or less than what is specified. What other method can I use for a more consistant stable loop (or game loop as you may call it)? I've already tried using .nets Timer classes but I think they are even worse.

(grr i typed this horribly, rushed through it)
Like always, all replies are appriciated,
Xanather smile.png
Advertisement
for 3, check out the System.Diagnostics.Stopwatch class. I believe there are other similar classes in that namespace which may help you smile.png
Ok, will do thanks.

This topic is closed to new replies.

Advertisement