Reputation: 9
I'm in coding 3D a while, and now, i wan't to create a game which works with Networking. I use the System.Net and System.Net.Sockets.
My Server is stupid as hell, it's just able to hold a single connection (later on, i will change it to multi-connection-abilities). It has a TcpListener which listens at 127.0.0.1 on Port 10000. (Just to test). In there, i have a DO While Loop which checks for received Data and Displays that.
My Client is a TcpClient. It connects to the Server and Sends a Message. Nothing special here, it's done with the TcpClient.GetStream().Write(). This works great. I'm also able to read one Answer right after the Send.
But what if the Server will Send some infos without the Client's Asking? For Example: All Players on the Server receive an item. How do i have to set up my Client, to be able to receive such messages?
Does my client have to ask in a loop? Or do i have to make a delegate somehow? I can create a loop which asks for such infos every 200 miliseconds or something, but how will this change the Performance of a 3D Game?
How is this done in Professional Game Developement?
Upvotes: 0
Views: 10695
Reputation: 988
Use the asynchronous capabilities of the TcpClient and TcpListener classes.
In your client:
private TcpClient server = new TcpClient();
async Task Listen()
{
try {
IPAddress IP = IPAddress.Loopback // this is your localhost IP
await server.ConnectAsync(IP,10000); // IP, port number
if(server.Connected) {
NetworkStream stream = server.GetStream();
while (server.Connected) {
byte[ ] buffer = new byte[server.ReceiveBufferSize];
int read = await stream.ReadAsync(buffer, 0, buffer.Length);
if (read > 0 ){
// you have received a message, do something with it
}
}
}
}
catch (Exception ex) {
// display the error message or whatever
server.Close();
}
}
Upvotes: 1
Reputation: 2353
Use Asynchronous Client Socket:
" The client is built with an asynchronous socket, so execution of the client application is not suspended while the server returns a response."
http://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx
private static void ReceiveCallback( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
In this ReceiveCallback you can create a switch that handles the messages of the server.
For example: First byte of the message is a command and after the command is the right data. Within an switch you can handle that command and execute some code.
Upvotes: 0