Results 1 to 2 of 2

Thread: Using Datagrams

  1. #1
    Kerfuffle is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Using Datagrams

    Ok, so I am relatively new to datagrams etc. I have three projects set up right now: Client, Client2, and Server, which all interact with each other to send coordinates of player1 and player2 back and forth (X, Y, this is in 2D space not 3D). I have everything set up perfectly except for one problem: I can only send the X coordinate back and forth for each player. I am getting the weirdest error, a parsing error. The way I communicate is I have everything sent back and forth in one string. Then I use the .split() function to turn that string into an array that contains the x and y coordinates. I've done several tests and to no avail. Hopefully someone can enlighten me. Here's my code (ignore all of the commented sections, I did ALOT of commenting, also there is ALOT of extra stuff not needed, haven't done cleanup yet either):

    Server:
    Java Code:
    package theGame;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    import javax.swing.JOptionPane;
    
    public class Main {
    
    	static String p1X;
    	static String p1Y;
    	static String p2X;
    	static String p2Y;
    
    	static boolean p1On = false;
    	static boolean p2On = false;
    
    	public static void main(String args[]) throws Exception
    	{
    		//String p2Y = "200";
    		//String p1Y = "100";
    		
    		//new Main();
    		String portString = JOptionPane.showInputDialog(null, "Type in a port to host on.");
    		int port = Integer.parseInt(portString);
    
    
    		try
    		{
    			DatagramSocket serverSocket1 = new DatagramSocket(port);
    
    			byte[] receiveData = new byte[256];
    			byte[] sendData = new byte[256];
    
    			while (true)
    			{
    				receiveData = new byte[256];
    
    				DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    
    				System.out.println("Waiting to recieve packet...");
    	
    				serverSocket1.receive(receivePacket);
    
    				String coords = new String(receivePacket.getData());
    				
    				System.out.println(coords);
    				
    				if (coords.contains(", ") && !coords.contains("null"))
    				{
    					String sp[] = coords.split(", ");
    					
    					if (sp[0].equals("P1"))
    					{
    						p1X = sp[1];
    						p1Y = sp[2];
    						p1On = true;
    					}
    					else if (sp[0].equals("P2"))
    					{
    						p2X = sp[1];
    						p2Y = sp[2];
    						p2On = true;
    					}
    					
    					String coord2 = null;
    
    					if (sp[0].equals("P1"))
    					{
    						//if (p2On)
    						//{
    							coord2 = p2X + ", " + p2Y + ", " + "on";
    						//}
    						//else
    						//{
    						//	coord2 = "200" + ", " + "200" + ", "  + "200" + ", "+ "off";
    						//}
    					}
    					else if (sp[0].equals("P2"))
    					{
    						//if (p1On)
    						//{
    							coord2 = p1X + ", " + p1Y + ", " + "on";
    						//}
    						//else
    						//{
    						//	coord2 = "100" + ", " + "100" + ", " + "100" + ", "+ "off";
    						//}
    					}
    					
    					sendData = coord2.getBytes();
    				}
    				
    				
    
    
    				//System.err.println(p1XS);
    
    				InetAddress ip = receivePacket.getAddress();
    
    				int port2 = receivePacket.getPort();
    
    				//System.out.println("From: " + ip + ":" + port2);
    				//System.out.println("P1X: " + sp[1]);
    				//System.out.println("P1Y: " + sp[2]);
    
    				
    
    				//System.out.println(coord2);
    				
    				
    
    				DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, port2);
    
    				serverSocket1.send(sendPacket);
    
    
    
    
    
    			}
    
    		} catch (SocketException ex) {
    			System.out.println("UDP Port " + port + " is occupied.");
    			System.exit(1);
    		}
    		///////////////////////////////////////////////////////
    		try
    		{
    			DatagramSocket serverSocket1 = new DatagramSocket(port);
    
    			byte[] receiveData = new byte[256];
    			byte[] sendData = new byte[256];
    
    			while (true)
    			{
    				receiveData = new byte[256];
    
    				DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    
    
    				serverSocket1.receive(receivePacket);
    
    				String coords = new String(receivePacket.getData());
    				
    				if (coords.contains(", ") && !coords.contains("null"))
    				{
    					String coord2 = null;
    					
    					String sp[] = coords.split(", ");
    
    					if (sp[0].equals("P1"))
    					{
    						p1X = sp[1];
    						p1Y = sp[2];
    						p1On = true;
    					}
    					else if (sp[0].equals("P2"))
    					{
    						p2X = sp[1];
    						p2Y = sp[2];
    						p2On = true;
    					}
    					
    					if (sp[0].equals("P1"))
    					{
    						//if (p2On)
    						//{
    							coord2 = p2X + ", " + p2Y + ", " + "on";
    						//}
    						//else
    						//{
    						//	coord2 = "200" + ", " + "200" + ", " + "200" + ", " + "off";
    						//}
    					}
    					else if (sp[0].equals("P2"))
    					{
    						//if (p1On)
    						//{
    							coord2 = p1X + ", " + p1Y + ", " + "on";
    						//}
    						//else
    						//{
    						//	coord2 = "100" + ", " + "100" + ", " + "100" + ", " + "off";
    						//}
    					}
    					
    					sendData = coord2.getBytes();
    				}
    				
    
    
    				//System.err.println(p1XS);
    
    				InetAddress ip = receivePacket.getAddress();
    
    				int port2 = receivePacket.getPort();
    
    				//System.out.println("From: " + ip + ":" + port2);
    				//System.out.println("P1X: " + sp[1]);
    				//System.out.println("P1Y: " + sp[2]);
    
    				
    
    				
    				
    
    				//System.out.println(coord2);
    				
    				
    
    				DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, port2);
    
    				serverSocket1.send(sendPacket);
    
    			}
    
    		} catch (SocketException ex) {
    			System.out.println("UDP Port " + port + " is occupied.");
    			System.exit(1);
    		}
    	}
    }
    Client1:
    Java Code:
    package theGame;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketTimeoutException;
    import java.net.UnknownHostException;
    
    import javax.swing.JOptionPane;
    
    import org.lwjgl.LWJGLException;
    import org.lwjgl.input.Keyboard;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    import org.lwjgl.opengl.GL11;
    
    import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
    import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
    import static org.lwjgl.opengl.GL11.GL_PROJECTION;
    import static org.lwjgl.opengl.GL11.GL_QUADS;
    import static org.lwjgl.opengl.GL11.glBegin;
    import static org.lwjgl.opengl.GL11.glClearColor;
    import static org.lwjgl.opengl.GL11.glColor3f;
    import static org.lwjgl.opengl.GL11.glDisable;
    import static org.lwjgl.opengl.GL11.glEnd;
    import static org.lwjgl.opengl.GL11.glLoadIdentity;
    import static org.lwjgl.opengl.GL11.glMatrixMode;
    import static org.lwjgl.opengl.GL11.glOrtho;
    import static org.lwjgl.opengl.GL11.glVertex2f;
    import static org.lwjgl.opengl.GL11.*;
    
    public class Main {
    
    	int p1X = 100;
    	int p1Y = 100;
    	int p1W = 50;
    	int p1H = 50;
    	
    	int p2X = 200;
    	int p2Y = 200;
    	int p2W = 50;
    	int p2H = 50;
    	
    	
    	//static String p2XS = "200";
    	//static String p2YS = "200";
    	
    	
    	public Main()
    	{
    		String host = JOptionPane.showInputDialog(null, "Type in a host ip.");
    		String portString = JOptionPane.showInputDialog(null, "Type in a port to connect with.");
    		int port = Integer.parseInt(portString);
    		
    		
    		
    		
    		
    		
    		
    		try
    		{
    			Display.setDisplayMode(new DisplayMode (1000, 700));
    			Display.setTitle("Client Player1");
    			Display.create();
    		}
    		catch(LWJGLException e)
    		{
    			e.printStackTrace();
    		}
    
    
    		glMatrixMode(GL_PROJECTION);
    		glLoadIdentity();		
    		glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
    		glMatrixMode(GL_MODELVIEW);
    		glEnable(GL_TEXTURE_2D);
    
    		glClearColor(0, 0, 0, 1);
    
    		glDisable(GL_DEPTH_TEST);
    
    		while(!Display.isCloseRequested())
    		{
    			
    		///	if ((p2XS != null && p2YS != null) && !p2XS.equals("null") && !p2YS.equals("null"))
    		//	{
    		//		p2X = Integer.parseInt(p2XS);
    		///		p2Y = Integer.parseInt(p2YS);
    		//	}
    			
    			
    			
    			//System.out.println(p1XS);
    			
    			//boolean switchdir = false;
    			
    			glClear(GL_COLOR_BUFFER_BIT);
    			
    			glColor3f(1, 1, 1);
    			
    			glBegin(GL_QUADS);
    			glVertex2f(p1X, p1Y);
    			glVertex2f(p1X, p1Y + p1H);
    			glVertex2f(p1X + p1W, p1Y + p1H);
    			glVertex2f(p1X + p1W, p1Y);
    			glEnd();
    			
    			glColor3f(0, 1, 1);
    			
    			glBegin(GL_QUADS);
    			glVertex2f(p2X, p2Y);
    			glVertex2f(p2X, p2Y + p2H);
    			glVertex2f(p2X + p2W, p2Y + p2H);
    			glVertex2f(p2X + p2W, p2Y);
    			glEnd();
    			
    			if (Keyboard.isKeyDown(Keyboard.KEY_W))
    			{
    				p1Y = p1Y + 5;
    			}
    			if (Keyboard.isKeyDown(Keyboard.KEY_S))
    			{
    				p1Y = p1Y - 5;
    			}
    			if (Keyboard.isKeyDown(Keyboard.KEY_A))
    			{
    				p1X = p1X - 5;
    			}
    			if (Keyboard.isKeyDown(Keyboard.KEY_D))
    			{
    				p1X = p1X + 5;
    			}
    			
    			//if (Keyboard.isKeyDown(Keyboard.KEY_P))
    			//{
    				try
    				{
    					String p1XS = "" + p1X;
    					String p1YS = "" + p1Y;
    					
    					String p1Coord = "P1" + ", " + p1XS + ", " + p1YS;
    					
    					//int p1 = Integer.parseInt(p1YS);
    					
    					DatagramSocket clientSocket = new DatagramSocket();
    					
    					InetAddress ip = InetAddress.getByName(host);
    					//System.out.println("Attempting to connect to " + host + " on port " + portString);
    					
    					byte[] sendData = new byte[256];
    					byte[] receiveData = new byte[256];
    					
    					
    					sendData = p1Coord.getBytes();
    					
    					//System.out.println("Sending " + sendData.length + " bytes to the server.");
    					
    					DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, port);
    					clientSocket.send(sendPacket);
    					
    					DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    					
    					clientSocket.setSoTimeout(10000);
    					
    					try
    					{
    						clientSocket.receive(receivePacket);
    						
    						String p2Coord = new String(receivePacket.getData());
    						
    						if (p2Coord.contains(", ") && !p2Coord.contains("null"))
    						{
    							String sp[] = p2Coord.split(", ");
    							p2X = Integer.parseInt(sp[0]);
    							p2Y = Integer.parseInt(sp[1]);
    							//p2Y = Integer.parseInt(sp[1]);
    							//System.out.println(sp[1]);
    						}
    						
    						
    						
    						
    						//System.out.println(p2Coord);
    												
    						//System.out.println(sp[0]);
    						//System.out.println(sp[1]);
    						
    					//	if (sp[0].equals("on"))
    					//	{
    					//		if (sp[0].equals("on"))
    					///		{
    					//			if (sp[0].equals("on"))
    					//			{
    					//				p2X = Integer.parseInt(sp[1]);
    					//				p2Y = Integer.parseInt(sp[2]);
    					//			}
    					//		}
    					//	}
    						
    						//if (!sp[1].equals("null") && !sp[2].equals("null"))
    						//{
    						//	p2X = Integer.parseInt(sp[1]);
    						//	p2Y = Integer.parseInt(sp[2]);
    						//}
    					//	if (sp[0].equals("on"))
    					//	{
    					//		if (!sp[1].equals("null") && !sp[2].equals("null"))
    					//		{
    					//			p2X = Integer.parseInt(sp[1]);
    					//			p2Y = Integer.parseInt(sp[2]);
    					//		}
    					//		else
    					//		{
    					//			p2X = 200;
    					//			p2Y = 200;
    					//		}
    					//	}
    						
    						//FileOutputStream fout = new FileOutputStream("coord.txt");
    						//Writer out = new OutputStreamWriter(fout, "UTF8");
    						
    						InetAddress returnIPAddress = receivePacket.getAddress();
    
    						int port2 = receivePacket.getPort();
    						
    						//System.out.println ("From server at: " + returnIPAddress + ":" + port);
    						//System.out.println("Message: " + receive); 
    						
    					} catch (SocketTimeoutException ste) {
    						System.out.println ("Timeout Occurred: Packet assumed lost");
    					}
    					
    					if (Display.isCloseRequested())
    					{
    						clientSocket.close();
    					}
    				}
    				catch (UnknownHostException ex) { 
    					System.err.println(ex);
    				}
    				catch (IOException ex) {
    					System.err.println(ex);
    				}
    				
    				
    				
    			//}
    			
    			Display.update();
    			Display.sync(100);			
    		}
    		if (Display.isCloseRequested())
    		{
    			Display.destroy();
    		}
    		
    		
    		
    		
    	}
    
    
    
    	public static void main(String[] args)
    	{
    
    		new Main();
    	}
    }
    Client2:

    Java Code:
    package theGame;
    
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketTimeoutException;
    import java.net.UnknownHostException;
    
    import javax.swing.JOptionPane;
    
    import org.lwjgl.LWJGLException;
    import org.lwjgl.input.Keyboard;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    import org.lwjgl.opengl.GL11;
    
    import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
    import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
    import static org.lwjgl.opengl.GL11.GL_PROJECTION;
    import static org.lwjgl.opengl.GL11.GL_QUADS;
    import static org.lwjgl.opengl.GL11.glBegin;
    import static org.lwjgl.opengl.GL11.glClearColor;
    import static org.lwjgl.opengl.GL11.glColor3f;
    import static org.lwjgl.opengl.GL11.glDisable;
    import static org.lwjgl.opengl.GL11.glEnd;
    import static org.lwjgl.opengl.GL11.glLoadIdentity;
    import static org.lwjgl.opengl.GL11.glMatrixMode;
    import static org.lwjgl.opengl.GL11.glOrtho;
    import static org.lwjgl.opengl.GL11.glVertex2f;
    import static org.lwjgl.opengl.GL11.*;
    
    public class Main {
    
    	int p1X = 100;
    	int p1Y = 100;
    	int p1W = 50;
    	int p1H = 50;
    	
    	int p2X = 200;
    	int p2Y = 200;
    	int p2W = 50;
    	int p2H = 50;
    	
    	
    	public Main()
    	{
    		String host = JOptionPane.showInputDialog(null, "Type in a host ip.");
    		String portString = JOptionPane.showInputDialog(null, "Type in a port to connect with.");
    		int port = Integer.parseInt(portString);
    		
    		
    		
    		
    		
    		
    		
    		try
    		{
    			Display.setDisplayMode(new DisplayMode (1000, 700));
    			Display.setTitle("Client Player2");
    			Display.create();
    		}
    		catch(LWJGLException e)
    		{
    			e.printStackTrace();
    		}
    
    
    		glMatrixMode(GL_PROJECTION);
    		glLoadIdentity();		
    		glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
    		glMatrixMode(GL_MODELVIEW);
    		glEnable(GL_TEXTURE_2D);
    
    		glClearColor(0, 0, 0, 1);
    
    		glDisable(GL_DEPTH_TEST);
    
    		while(!Display.isCloseRequested())
    		{
    			
    		//	if ((p1XS != null && p1YS != null) && !p1XS.equals("null") && !p1YS.equals("null"))
    		//	{
    		//		p1X = Integer.parseInt(p1XS);
    		//		p1Y = Integer.parseInt(p1YS);
    		//	}
    			
    			//System.out.println(p1XS);
    			
    			
    			
    			glClear(GL_COLOR_BUFFER_BIT);
    			
    			glColor3f(1, 1, 1);
    			
    			glBegin(GL_QUADS);
    			glVertex2f(p1X, p1Y);
    			glVertex2f(p1X, p1Y + p1H);
    			glVertex2f(p1X + p1W, p1Y + p1H);
    			glVertex2f(p1X + p1W, p1Y);
    			glEnd();
    			
    			glColor3f(0, 1, 1);
    			
    			glBegin(GL_QUADS);
    			glVertex2f(p2X, p2Y);
    			glVertex2f(p2X, p2Y + p2H);
    			glVertex2f(p2X + p2W, p2Y + p2H);
    			glVertex2f(p2X + p2W, p2Y);
    			glEnd();
    			
    			if (Keyboard.isKeyDown(Keyboard.KEY_W))
    			{
    				p2Y = p2Y + 5;
    			}
    			if (Keyboard.isKeyDown(Keyboard.KEY_S))
    			{
    				p2Y = p2Y - 5;
    			}
    			if (Keyboard.isKeyDown(Keyboard.KEY_A))
    			{
    				p2X = p2X - 5;
    			}
    			if (Keyboard.isKeyDown(Keyboard.KEY_D))
    			{
    				p2X = p2X + 5;
    			}
    			
    			//if (Keyboard.isKeyDown(Keyboard.KEY_P))
    			//{
    				try
    				{
    					String p2XS = "" + p2X;
    					String p2YS = "" + p2Y;
    					
    					String p2Coord = "P2" + ", " + p2XS + ", " + p2YS;
    					
    					//int p2 = Integer.parseInt(p2YS);
    					
    					DatagramSocket clientSocket = new DatagramSocket();
    					
    					InetAddress ip = InetAddress.getByName(host);
    					//System.out.println("Attempting to connect to " + host + " on port " + portString);
    					
    					byte[] sendData = new byte[256];
    					byte[] receiveData = new byte[256];
    					
    					
    					sendData = p2Coord.getBytes();
    					
    					//System.out.println("Sending " + sendData.length + " bytes to the server.");
    					
    					DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, port);
    					clientSocket.send(sendPacket);
    					
    					DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    					
    					clientSocket.setSoTimeout(10000);
    					
    					try
    					{
    						clientSocket.receive(receivePacket);
    					
    						String p1Coord = new String(receivePacket.getData());
    						
    						if (p1Coord.contains(", ") && !p1Coord.contains("null"))
    						{
    							String sp[] = p1Coord.split(", ");
    							p1X = Integer.parseInt(sp[0]);
    							p1Y = Integer.parseInt(sp[1]);
    							//p1Y = Integer.parseInt(sp[1]);
    							//System.out.println(sp[1]);
    						}
    						
    						//System.out.println(p1Coord);
    						
    						//System.out.println(sp[0]);
    						//System.out.println(sp[1]);
    						
    						//p1X = Integer.parseInt(sp[1]);
    						//p1Y = Integer.parseInt(sp[2]);
    						
    						
    						//if (sp[0].equals("on"))
    						//{
    						//	if (sp[0].equals("on"))
    						//	{
    						//		if (sp[0].equals("on"))
    						//		{
    						//			p1X = Integer.parseInt(sp[1]);
    						//			p1Y = Integer.parseInt(sp[2]);
    						//		}
    						//	}
    						//}
    						
    						
    						//if (!sp[0].equals("null") && !sp[1].equals("null"))
    						//{
    						//	p1XS = sp[0];
    						//	p1YS = sp[1];
    						//}
    						//if (sp[0].equals("on"))
    						//{
    							//if (Keyboard.isKeyDown(Keyboard.KEY_P))
    							//{
    							//	p1X = Integer.parseInt(sp[1]);
    							//	p1Y = Integer.parseInt(sp[2]);
    							//}
    						//	else
    						//	{
    						//		p1X = 100;
    						//		p1Y = 100;
    						//	}
    						//}
    						
    						//System.out.println(sp);
    						//System.out.println(sp[2]);
    						//System.out.println(sp[1]);
    						
    						//if (sp[0] != null && sp[1] != null)
    						//{
    						//	p1X = Integer.parseInt(sp[0]);
    						//	p1Y = Integer.parseInt(sp[1]);
    						//}
    						
    						
    						InetAddress returnIPAddress = receivePacket.getAddress();
    
    						int port2 = receivePacket.getPort();
    						
    						//System.out.println ("From server at: " + returnIPAddress + ":" + port);
    						//System.out.println("Message: " + receive); 
    						
    					} catch (SocketTimeoutException ste) {
    						System.out.println ("Timeout Occurred: Packet assumed lost");
    					}
    					
    					if (Display.isCloseRequested())
    					{
    						clientSocket.close();
    					}
    				}
    				catch (UnknownHostException ex) { 
    					System.err.println(ex);
    				}
    				catch (IOException ex) {
    					System.err.println(ex);
    				}
    				
    				
    			//}
    			
    			Display.update();
    			Display.sync(100);			
    		}
    		if (Display.isCloseRequested())
    		{
    			Display.destroy();
    		}
    		
    		
    		
    		
    	}
    
    
    
    	public static void main(String[] args)
    	{
    
    		new Main();
    	}
    }
    Lines 177 on Client1 and 172 on client2 are where the debugger is saying that I parsed wrong.

    Also, if your are going to test this you will need to have the LWJGL libraries in your build path. (Ask me if you don't know how to put them in.)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Using Datagrams

    You'll get more help if you make a Short, Self Contained, Correct Example using Java SE classes that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Network Programming Using Datagrams
    By IandI in forum Networking
    Replies: 4
    Last Post: 03-18-2009, 12:24 AM
  2. Using Datagrams to Get the Date
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:09 PM

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
  •