EDN Admin
Well-known member
I was wondering if this is a good approach on how to code server/client game networking with C# and XNA (XNA is not used for this anyway). I have already started making a game called Rada, I am just wondering if what I have made so far is right/efficient,
and if there is better ways to do this. I have asked similar questions about why the game was not that compatible with the internet etc... I decided to ask this "overall" question instead.<br/>
<br/>
<span style="text-decoration:underline Server <br/>
1 Listener thread <br/>
1 Upload thread (when someone connects it gets added to a player database, so when the upload thread is processing what to send to the client(s) it will simply go through each player in the database and send what is needed, I do not know if
this is a good approach, if this thread is blocked it means no other client can receive data).<br/>
x Download thread (when someone connects a new download thread is started to listen to all data sent by the client, if data exists, it is read in a byte buffer of 1. The byte buffer is checked if it has a | to tell that is a message end (all
messages end with a specific symbol), if it does not exist in that byte the thread continues to read the data received until it gets to that message end/symbol).<br/>
A example of a algorithm that does this:<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void DownloadThread()
{
<span style="color:Blue; byte[] bytes = <span style="color:Blue; new <span style="color:Blue; byte[1];
<span style="color:Blue; int i; <span style="color:Blue; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
<span style="color:Blue; string messagestring = <span style="color:#A31515; "";
messagestring += System.Text.Encoding.ASCII.GetString(bytes, 0, i); bytedownused += 1;
<span style="color:Blue; bool continuereading = <span style="color:Blue; true;
<span style="color:Blue; while (continuereading)
{
<span style="color:Blue; if (stream.DataAvailable)
{
i = stream.Read(bytes, 0, bytes.Length);
messagestring += System.Text.Encoding.ASCII.GetString(bytes, 0, i); bytedownused += 1;
<span style="color:Blue; if (messagestring.EndsWith(<span style="color:#A31515; "|"))
{
<span style="color:Blue; if (stream.DataAvailable == <span style="color:Blue; false)
{
continuereading = <span style="color:Blue; false; <span style="color:Green; //got to the splitter AND end of message
}
}
}
}
ProcessDownload(messagestring);
}
}
[/code]
I think this should read data completely and fully. Even supports multiple messages at once.<br/>
Please reply with what you think...<br/>
<br/>
<br/>
<span style="text-decoration:underline Client <br/>
1 Upload thread <br/>
1 Download thread <br/>
<br/>
<span style="text-decoration:underline Network Classes used <br/>
NetworkStream (client and server)<br/>
TcpClient (client and server)<br/>
TcpListener(server)<br/>
<br/>
<span style="text-decoration:underline Things to know <br/>
Client messages are processed and uploaded every 10 milliseconds.<br/>
Server messages are processed and uploaded every 10 milliseconds.<br/>
Is this good? Messages ARE NOT sent every 10 milliseconds, they are only sent if necessary, e.g. a players x and y position is not (re)sent if it is the same as the last time it was sent.<br/>
<br/>
Thank you for reading and reply with suggestions if you can! If you need any more details simply ask.<br/>
<br/>
I also have another question below as well (do not answer if you do not want to, I will just appreciate it, it is about game development), it is a little off topic but here it goes.<br/>
<br/>
Ok, well, I have taught myself how to code VB.net and C#/XNA off google so far for the last 3-4 years (as I am still at school). But I think I have reached the point where I need to either...<br/>
1. get a course of programming (which I cant yet).<br/>
2. get a book<br/>
As I am starting to ask too in-depth questions (I think).<br/>
<br/>
What programming language is the best to learn? I am willing to learn the main C/C++ although I have seen easier languages such as Java (Minecraft) and quick development languages such as C# XNA (Terraria) being used very well and successfully (I am sure there
are many more examples out there). What is the best language I should learn for specifically game development?<br/>
<br/>
Thank you again!<hr class="sig Take a look at my site: http://www.xanather.com
View the full article
and if there is better ways to do this. I have asked similar questions about why the game was not that compatible with the internet etc... I decided to ask this "overall" question instead.<br/>
<br/>
<span style="text-decoration:underline Server <br/>
1 Listener thread <br/>
1 Upload thread (when someone connects it gets added to a player database, so when the upload thread is processing what to send to the client(s) it will simply go through each player in the database and send what is needed, I do not know if
this is a good approach, if this thread is blocked it means no other client can receive data).<br/>
x Download thread (when someone connects a new download thread is started to listen to all data sent by the client, if data exists, it is read in a byte buffer of 1. The byte buffer is checked if it has a | to tell that is a message end (all
messages end with a specific symbol), if it does not exist in that byte the thread continues to read the data received until it gets to that message end/symbol).<br/>
A example of a algorithm that does this:<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void DownloadThread()
{
<span style="color:Blue; byte[] bytes = <span style="color:Blue; new <span style="color:Blue; byte[1];
<span style="color:Blue; int i; <span style="color:Blue; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
<span style="color:Blue; string messagestring = <span style="color:#A31515; "";
messagestring += System.Text.Encoding.ASCII.GetString(bytes, 0, i); bytedownused += 1;
<span style="color:Blue; bool continuereading = <span style="color:Blue; true;
<span style="color:Blue; while (continuereading)
{
<span style="color:Blue; if (stream.DataAvailable)
{
i = stream.Read(bytes, 0, bytes.Length);
messagestring += System.Text.Encoding.ASCII.GetString(bytes, 0, i); bytedownused += 1;
<span style="color:Blue; if (messagestring.EndsWith(<span style="color:#A31515; "|"))
{
<span style="color:Blue; if (stream.DataAvailable == <span style="color:Blue; false)
{
continuereading = <span style="color:Blue; false; <span style="color:Green; //got to the splitter AND end of message
}
}
}
}
ProcessDownload(messagestring);
}
}
[/code]
I think this should read data completely and fully. Even supports multiple messages at once.<br/>
Please reply with what you think...<br/>
<br/>
<br/>
<span style="text-decoration:underline Client <br/>
1 Upload thread <br/>
1 Download thread <br/>
<br/>
<span style="text-decoration:underline Network Classes used <br/>
NetworkStream (client and server)<br/>
TcpClient (client and server)<br/>
TcpListener(server)<br/>
<br/>
<span style="text-decoration:underline Things to know <br/>
Client messages are processed and uploaded every 10 milliseconds.<br/>
Server messages are processed and uploaded every 10 milliseconds.<br/>
Is this good? Messages ARE NOT sent every 10 milliseconds, they are only sent if necessary, e.g. a players x and y position is not (re)sent if it is the same as the last time it was sent.<br/>
<br/>
Thank you for reading and reply with suggestions if you can! If you need any more details simply ask.<br/>
<br/>
I also have another question below as well (do not answer if you do not want to, I will just appreciate it, it is about game development), it is a little off topic but here it goes.<br/>
<br/>
Ok, well, I have taught myself how to code VB.net and C#/XNA off google so far for the last 3-4 years (as I am still at school). But I think I have reached the point where I need to either...<br/>
1. get a course of programming (which I cant yet).<br/>
2. get a book<br/>
As I am starting to ask too in-depth questions (I think).<br/>
<br/>
What programming language is the best to learn? I am willing to learn the main C/C++ although I have seen easier languages such as Java (Minecraft) and quick development languages such as C# XNA (Terraria) being used very well and successfully (I am sure there
are many more examples out there). What is the best language I should learn for specifically game development?<br/>
<br/>
Thank you again!<hr class="sig Take a look at my site: http://www.xanather.com
View the full article