Results 1 to 3 of 3
- 11-24-2012, 08:56 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Networking, server is only receving server packet sent when client sends at same time
I have a very simple game set up with 2 players. when a player moves on their computer it mocks the movement on the other computer. Everything works fine as long as both players dont move at the same time. when both players hold down a movement key, the client shows both players moving but the server is only moving the server player.
Threw in a few system out prints, and the problem seems to be when the server is receiving and sending a packet at the same time, it just receives the same packet its sending out, even if i clarify to ONLY send to the client IP address, and even though the client recieves the server packet fine.
heres my code, id appreciate any help guys this is bugging me =(
Java Code:public class Network extends Thread { protected DatagramSocket socket = null; protected DatagramPacket packet = null; public static int numberOfPlayers = 1; public String received = ""; public String lastSent = ""; protected boolean isServer = false; protected boolean isClient = false; protected static ArrayList <String> clientIP; String ipAddress = "localhost"; int port = 5667; public Network() { try // Tries to create a socket { //creates the socket socket = new DatagramSocket(port); System.out.println(InetAddress.getLocalHost().getHostName()); System.out.println(InetAddress.getLocalHost().getHostAddress()); } catch(IOException e) { e.printStackTrace(); } } public void run() { while(isServer) // While the server is running... { try { receivePacket(); } catch(IOException e) { e.printStackTrace(); isServer = false; } catch (Exception e) { e.printStackTrace(); } } while(isClient) // While the client is running... { try { receivePacket(); } catch(IOException e) { e.printStackTrace(); isServer = false; } catch (Exception e) { e.printStackTrace(); } } socket.close(); port = 0; } public synchronized void receivePacket() throws Exception { if(isServer) { byte[] buf = new byte[256]; // Data packet = new DatagramPacket(buf, buf.length); // Creates a packet from the buffer socket.receive(packet); // Gets a socket received = new String(packet.getData(), 0, packet.getLength()); // Puts data into a String System.out.println("sv received " + received); if (!received.equals(lastSent)) Translator.translate(received); } else if(isClient) { try { byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length); // Create a new packet socket.receive(packet); received = new String(packet.getData(), 0, packet.getLength()); System.out.println("cl received " + received); if (!received.equals(lastSent)) Translator.translate(received); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to Receive"); } catch (Exception e) { e.printStackTrace(); } } } public void sendPacket(String message) throws IOException { lastSent = message; if(isServer) { for(String go : getClientIPs()) { System.out.println(go); byte[] buf = message.getBytes(); InetAddress address = InetAddress.getByName(go); packet = new DatagramPacket(buf, buf.length, address, port); System.out.println("sv send" + message); socket.send(packet); } } if(isClient) { try { byte[] buf = message.getBytes(); InetAddress address = InetAddress.getByName(ipAddress); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port); System.out.println("cl send " + message); socket.send(packet); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to Send"); } } } public void setServer() { isServer = true; isClient = false; } public void setClient() { isServer = false; isClient = true; } public void setBoth() { isServer = true; isClient = true; } public static ArrayList <String> getClientIPs() { if(clientIP == null) clientIP = new ArrayList <String>(); return clientIP; } }
- 11-24-2012, 04:49 PM #2
Re: Networking, server is only receving server packet sent when client sends at same
Moved from Advanced Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-05-2012, 10:26 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Re: Networking, server is only receving server packet sent when client sends at same
It's probably this line of code that is faulty:
Instead of checking if the message you received is the same as the last you sent, you should check the address of the packet (packet.getAddress()) and make sure it is not your own. That's how you should ignore packets.Java Code:if (!received.equals(lastSent))
Similar Threads
-
How to prevent hacking game client-server by decompile->modify->package new client
By chanphat01001 in forum Advanced JavaReplies: 1Last Post: 06-03-2012, 03:07 AM -
Implement a simple TCP client- server system that makes use of a simple proxy server
By kurtcobain92 in forum Advanced JavaReplies: 3Last Post: 04-04-2012, 12:38 AM -
basic Server Networking
By altosaxplayer88 in forum NetworkingReplies: 5Last Post: 05-23-2011, 02:39 AM -
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 11:31 AM -
Send Datagram packet to root server
By jammin in forum NetworkingReplies: 1Last Post: 11-15-2008, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks