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 =(
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;
}
}
Re: Networking, server is only receving server packet sent when client sends at same
Moved from Advanced Java
db
Re: Networking, server is only receving server packet sent when client sends at same
It's probably this line of code that is faulty:
Code:
if (!received.equals(lastSent))
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.