Results 1 to 8 of 8
  1. #1
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Simple udp send and recieve

    Hello ive tried to make a small and simple program which allows me to send a string over udp and back again. But it doesnt seem to work for some reason. It keeps giving "No connection!" which means that my socket is not being created.
    [ClientThread]
    Java Code:
    package Client;
    
    import java.io.*;
    import java.net.*;
    
    class UPDClientThread extends Thread{
    	byte[] sendBuf = new byte[256];
    	DatagramSocket socket = null;
    	DatagramPacket packet;
    	InetAddress address;
    	String ip;
    	int port;
    	byte[] buf = new byte[256];
    	
    	UPDClientThread(String ip, int port){
    		this.ip = ip;
    		this.port = port;
    		try {
    			System.out.println("Connecting! Please stand by...");
    			DatagramSocket socket = new DatagramSocket();
    			InetAddress address;
    			address = InetAddress.getByName(ip);
    		} catch (SocketException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public String recieve(){
    		
    		packet = new DatagramPacket(buf, buf.length);
    		
    		try {
    			socket.receive(packet);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    		String received = new String(packet.getData(), 0, packet.getLength());
    		return received;
    	}
    	
    	public void Send(String line){
    
    		try {
    			buf = line.getBytes("8859_1");
    		} catch (UnsupportedEncodingException e1) {
    			e1.printStackTrace();
    		}
    		DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
    		try {
    			socket.send(packet);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void run(){
    		while(true){
    			if(socket!=null){
    				System.out.println(recieve());
    				Send("ECHO: "+recieve());
    			}
    			else{
    				System.out.println("No connection!");
    				//return;
    			}
    		}
    	}
    }
    [Server]
    Java Code:
    package Server;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    
    
    public class UDPServerThread extends Thread{
        protected DatagramSocket socket = null;
        protected BufferedReader in = null;
        protected boolean moreQuotes = true;
    	int port;
    	
    	public UDPServerThread(int port){
    		this.port = port;
    		try {
    			socket = new DatagramSocket(port);
    		} catch (SocketException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void run(){
    		while(true){
    			try {
    				Send("Test123");
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	public void Send(String line) throws IOException{
    		byte[] buf = new byte[256];
    		buf = line.getBytes();
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            try{
            	socket.receive(packet);
            }catch(SocketException r){
            	
            }
            InetAddress address = packet.getAddress();
            int port = 5;
            port = packet.getPort();
            System.out.println("" + address + port);
            packet = new DatagramPacket(buf, buf.length, address, port);
            socket.send(packet);
    	}
    	
    	public String getString(){
    		return"";
    	}
    }
    and my main:
    Java Code:
    public class Main {
    	public static void main(String[] args) throws IOException {
    boolean host = true; // set the host or the client
    		System.out.println("Program launched!");
    if(host){
    		UPDClientThread client1 = new UPDClientThread("localhost",9867);
    		client1.start();}
    else{
    		UDPServerThread host1 = new UDPServerThread(9767);
    		host1.start();
    }
    
    	}
    }
    What am i doing wrong?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Simple udp send and recieve

    Do you see any exceptions being thrown?

  3. #3
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Simple udp send and recieve

    Nope, none at all.
    Ive tried running it on the same computer, then it will give me exceptions (port already in use or something like that)
    But when running it from my laptop nothing happens whatshowever, just keeps saying: No connection!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Simple udp send and recieve

    You're going to have to debug, because as far as I can see (with a quick scan of the code) the only way 'socket' can be null is if there was an exception thrown in the constructor.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Simple udp send and recieve

    it doesnt seem to work for some reason.
    Try debugging the code by adding lots of printlns to show where the execution is going and the values of the variables as they are set and used.

  6. #6
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Simple udp send and recieve

    Thats the thing though, im doing that and i can only see that the socket value is equal to -1, or null. And ive tried everything. Followed many tutorials, but i cant seem to figure it out as soon as im making my own code. Internet code works.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Simple udp send and recieve

    i can only see that the socket value is equal to -1, or null
    Where do you assign a value to the variable that is null?
    Make sure you are giving a value to the variable you want to be set and not to another one with the same name.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Simple udp send and recieve

    Things don't change in Java by magic.
    That socket is either never created (ie always null) or it is set to null somewhere.

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  2. Replies: 10
    Last Post: 06-06-2010, 07:49 PM
  3. Replies: 4
    Last Post: 04-06-2010, 09:23 PM
  4. Replies: 2
    Last Post: 02-02-2009, 04:27 PM
  5. Replies: 2
    Last Post: 05-04-2008, 06:27 PM

Posting Permissions

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