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]
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]
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:
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?
Re: Simple udp send and recieve
Do you see any exceptions being thrown?
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!
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.
Re: Simple udp send and recieve
Quote:
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.
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.
Re: Simple udp send and recieve
Quote:
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.
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.