Results 1 to 3 of 3
  1. #1
    j4sbass is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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;
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Networking, server is only receving server packet sent when client sends at same

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: Networking, server is only receving server packet sent when client sends at same

    It's probably this line of code that is faulty:

    Java 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.

Similar Threads

  1. Replies: 1
    Last Post: 06-03-2012, 03:07 AM
  2. Replies: 3
    Last Post: 04-04-2012, 12:38 AM
  3. basic Server Networking
    By altosaxplayer88 in forum Networking
    Replies: 5
    Last Post: 05-23-2011, 02:39 AM
  4. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  5. Send Datagram packet to root server
    By jammin in forum Networking
    Replies: 1
    Last Post: 11-15-2008, 12:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •