i have a little promblem. When i connect to my UDP server using localhost as a hostname, everything goes fine, but when i use my ip as a hostname, the client cant connect to the server. What could couse such issue? As i know the server and the client are fine, because i had the same problem with other servers. But if i'm not right, i give you the code... Server:
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPServer extends Thread {
public final static int PORT = 7331;
private final static int BUFFER = 1024;
private DatagramSocket socket;
private ArrayList<InetAddress> clientAddresses;
private ArrayList<Integer> clientPorts;
private HashSet<String> existingClients;
public UDPServer() throws IOException {
socket = new DatagramSocket(PORT);
clientAddresses = new ArrayList();
clientPorts = new ArrayList();
existingClients = new HashSet();
}
public void run() {
byte[] buf = new byte[BUFFER];
while (true) {
try {
Arrays.fill(buf, (byte)0);
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String content = new String(buf, buf.length);
InetAddress clientAddress = packet.getAddress();
int clientPort = packet.getPort();
String id = clientAddress.toString() + "," + clientPort;
if (!existingClients.contains(id)) {
existingClients.add( id );
clientPorts.add( clientPort );
clientAddresses.add(clientAddress);
}
System.out.println(id + " : " + content);
byte[] data = (id + " : " + content).getBytes();
for (int i=0; i < clientAddresses.size(); i++) {
InetAddress cl = clientAddresses.get(i);
int cp = clientPorts.get(i);
packet = new DatagramPacket(data, data.length, cl, cp);
socket.send(packet);
}
} catch(Exception e) {
System.err.println(e);
}
}
}
public static void main(String args[]) throws Exception {
UDPServer s = new UDPServer();
s.start();
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class MessageSender implements Runnable {
public final static int PORT = 7331;
private DatagramSocket sock;
private String hostname;
MessageSender(DatagramSocket s, String h) {
sock = s;
hostname = h;
}
private void sendMessage(String s) throws Exception {
byte buf[] = s.getBytes();
InetAddress address = InetAddress.getByName(hostname);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, PORT);
sock.send(packet);
}
public void run() {
boolean connected = false;
do {
try {
sendMessage("GREETINGS");
connected = true;
} catch (Exception e) {
}
} while (!connected);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
while (!in.ready()) {
Thread.sleep(100);
}
sendMessage(in.readLine());
} catch(Exception e) {
System.err.println(e);
}
}
}
}
class MessageReceiver implements Runnable {
DatagramSocket sock;
byte buf[];
MessageReceiver(DatagramSocket s) {
sock = s;
buf = new byte[1024];
}
public void run() {
while (true) {
try {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
sock.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println(received);
} catch(Exception e) {
System.err.println(e);
}
}
}
}
public class UDPClient {
public static void main(String args[]) throws Exception {
String host = null;
if (args.length < 1) {
System.out.println("Usage: java ChatClient <server_hostname>");
System.exit(0);
} else {
host = args[0];
}
DatagramSocket socket = new DatagramSocket();
MessageReceiver r = new MessageReceiver(socket);
MessageSender s = new MessageSender(socket, host);
Thread rt = new Thread(r);
Thread st = new Thread(s);
rt.start(); st.start();
}
}