2012-03-14 3 views
2

クライアントJava UDPメッセージ交換はlocalhostでは有効ですが、インターネットでは機能しませんか?

import java.io.*; 
    import java.net.*; 
    import java.util.*; 

    public class ClientA 
    { 
    final private static int PORT = 5005; // arbitrarily assigned port to use 

public static void main(String args[]) throws 
IOException 
{ 
    DatagramSocket socket = new DatagramSocket(PORT); // create new connection on that port 
    while (true) 
    { 
     byte buffer[] = new byte[256]; // data buffer 
     DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // new packet containing buffer 

     socket.receive(packet); // look for packet 

     String clientBMsg = new String(packet.getData()); // get data from received packet 
     InetAddress address = packet.getAddress(); // get address of received packet 

     System.out.println("ClientB at " + address + " says " + clientBMsg); 

     buffer = null; 
     String msgString = "I'm ClientA, vegetables are fun"; 
     buffer = msgString.getBytes(); // put String in buffer 


     int port = packet.getPort(); // get port of received packet 
     packet = new DatagramPacket(buffer, buffer.length, address, port); // create new packet with this data 
     socket.send(packet); // send packet back containing new buffer! 
     System.out.println("Message Sent"); 
     socket.close(); 
     } 
} 
    } 

クライアントB

import java.io.*; 
    import java.net.*; 


    public class ClientB 
    { 
final private static int PORT = 5005; // arbitrarily assigned port - same as server 

public static void main(String args[]) throws 
    IOException { 
     // if (args.length == 0) { // requires host 
      // System.err.println 
       // ("Please specify host"); 
      // System.exit(-1); 
     // } 
     // String host = args[0]; // user defined host 
     DatagramSocket socket = new DatagramSocket(); // open new socket 

     String host = "localhost";//"86.0.164.207"; 
     byte message[] = new byte[256]; // empty message 
     String msgString = "Hello, I'm client B and I like trees"; 
     message = msgString.getBytes(); // put String in buffer 

     InetAddress address = InetAddress.getByName(host); // determines address 
     System.out.println("Sending to: " + address); // tells user it's doing something 
     DatagramPacket packet = new DatagramPacket(message, message.length, address, PORT); // create packet to send 

     socket.send(packet); // send packet 
     System.out.println("Message Sent"); 

     message = new byte[256]; 
     packet = new DatagramPacket(message, message.length); 
     socket.receive(packet); // wait for response 
     String clientAreply = new String(packet.getData()); 
     System.out.println("ClientA at " + host + " says " + clientAreply); 
     socket.close(); 

     } 
     } 

これは、ローカルホスト上で動作しますが、私は私のIPアドレスを入れたときに、それだけでメッセージを送信し、何も受信されない理由を私は理解していません。

誰でも正しい方向に向けることができますか?

ありがとうございました!

+1

万が一ファイアウォールやNATの後ろにいるのですか? –

+0

私のルーターでポートが転送されたことを確認しましたか? – lex

+0

ネットワークのセットアップについて説明してください。その2つのプロセスをどこで実行するかは不明です。 –

答えて

4

DatagramSocketの方法を使用してインターネットインターフェイスにバインドする必要があります。それ以外の場合は、127.0.0.1またはlocalhostのみを受信します。このように:あなたは、ルータの背後にある場合には

DatagramSocket socket = new DatagramSocket(null); 
socket.bind(new InetSocketAddress(InetAddress.getByName("your.ip.add.ress"),5005); 

その後、あなたは、ルータによってあなたに与えられたローカルIPアドレスをリッスンすべきであり、ルーターの設定でこのアドレスにポート転送を使用します。

+0

申し訳ありませんが、これはクライアントAまたはBのためにする必要がありますか? – lex

+0

ありがとうCarl T. –

+0

両方のクライアントは、彼らはインターネット上でお互いを見ることができなくなります彼らのIPを持っている必要があります。 Localhostと127.0.0.1は、同じマシン上で動作している場合にのみ適用する必要があります。 – Stainedart

0

私は、TCPとUDPソケットでSocket Testツールを使用して提案することができます:

私は2ウェイのソケットプログラムとのトラブルシュートの問題にそれを使用。 2つのSocketTestプログラムと比較結果などを使用して、エンドツーエンドのリンクをテストできます。非常に便利です。

関連する問題