Results 1 to 8 of 8
Thread: Simple udp send and recieve
- 01-19-2012, 04:42 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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]
[Server]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; } } } }
and my main: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""; } }
What am i doing wrong?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(); } } }
- 01-19-2012, 04:46 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Simple udp send and recieve
Do you see any exceptions being thrown?
- 01-19-2012, 05:16 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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!
- 01-19-2012, 06:07 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-19-2012, 06:12 PM #5
Re: Simple udp send and recieve
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.it doesnt seem to work for some reason.
- 01-19-2012, 06:19 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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.
- 01-19-2012, 06:27 PM #7
Re: Simple udp send and recieve
Where do you assign a value to the variable that is null?i can only see that the socket value is equal to -1, or 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.
- 01-19-2012, 06:28 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Simple program, simple problem
By taymilll in forum New To JavaReplies: 12Last Post: 06-20-2011, 05:12 AM -
simple send email APP, but when press send button appeared:
By lse123 in forum Advanced JavaReplies: 10Last Post: 06-06-2010, 06:49 PM -
how to start up my RMI server(under ISP) so that it can recieve remote calls ??
By lokeshmsit in forum NetworkingReplies: 4Last Post: 04-06-2010, 08:23 PM -
To send and recieve data stored in fileusing TCP program
By rosemary in forum NetworkingReplies: 2Last Post: 02-02-2009, 03:27 PM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks