• How do Game Servers send data back to client?
    6 replies, posted
I'm working on a simple game server for a simple game. Both the client and server are in Java. How do most game servers send data back to the client? I'm using simple Sockets and I've found out that the client will need to forward ports in router to receive data back from the server. It's kind of unrealistic to expect the user to forward ports to run a game; I've never even played an online game that required that.
[QUOTE=PvtCupcakes;19440168]I'm working on a simple game server for a simple game. Both the client and server are in Java. How do most game servers send data back to the client? I'm using simple Sockets and I've found out that the client will need to forward ports in router to receive data back from the server. It's kind of unrealistic to expect the user to forward ports to run a game; I've never even played an online game that required that.[/QUOTE] You need to forward ports for the server if it's behind a router. Connections going out are not affected. Simple as that.
[QUOTE=jA_cOp;19440186]You need to forward ports for the server if it's behind a router. Connections going out are not affected. Simple as that.[/QUOTE] I've got a port forwarded to the server just fine. I can write from the client back to the server. But tonight I was just trying to get the server to send data back, and for some reason the client hangs. It might be a firewall issue, I don't know for sure. It's getting hung right here in the client: [code] //Just showing what the variables are Socket socket = new Socket("my.ip.add.ress", 4000); ObjectOutputStream netOut = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream netIn = new ObjectInputStream(socket.getInputStream()); String message = (String) netIn.readObject(); //hangs on this line [/code] I'm not even sure why it's hanging the whole client, I put it in it's own thread. Maybe I need to dick around with the "synchronized" keyword?
The socket standard is for read calls to block until it has read the requested data. In other words, the server has to actually send an object for the client read to return. It is possible to set them to non-blocking, and it is possible to query them for data, but if you're using a separate thread, it shouldn't be blocking any other threads anyway. If it was a firewall issue, it'd block the connection on its way out, not at a read call. Also, I'm not familiar with Java and thus not ObjectStream, but make sure the server sends in the same format the client is receiving, so that the client won't be waiting for some kind of message terminator.
A blocking call waiting in a non-UI thread shouldn't cause the whole program to become unresponsive. Sounds like you have some bigger problems to work out here. And I'm not sure how you think using synchronized will help you. And you're sure the data is being sent over the wire while the call is blocking? Are you receiving any data at all? Can you read data using other types of InputStreams?
Oh yeah, it's being blocked for some reason. I sent it message from the server and it unblocked. So for some reason my Thread wasn't being a Thread. I found it, I was using: [code] MyThread t = new MyThread(); t.run(); //should have been //t.start(); [/code] The function you put all your threading stuff in is the run() function, but you have to call start() to actually make it a new thread. [editline]12:47AM[/editline] [QUOTE=jA_cOp;19440293] Also, I'm not familiar with Java and thus not ObjectStream[/QUOTE] Problem already solved, but an ObjectStream serializes an object and sends it through a stream. I'm just using Strings for now, but I could eventually send through any serializable object.
I was reading in the area and thought I'd help if you're curious to how some of the bigger guys do it with more advanced games. [url]http://gafferongames.com/game-physics/fix-your-timestep/[/url] [url]http://www.gamasutra.com/view/feature/3094/1500_archers_on_a_288_network_.php?page=1[/url] (More found linked from [url]http://www.reddit.com/r/programming/comments/akmli/ask_proggit_how_do_professional_game_loops_work/[/url])
Sorry, you need to Log In to post a reply to this thread.