2016-10-14 5 views
0

私はJavaで書かれた簡単なピアツーピアプログラムを持っています。私は次のコードを持っています。java UDPソケットを使用したピアツーピア

import java.net.*; 
import java.io.*; 
public class Broadcasts { 

private final Runnable receiver; 
private final Runnable sender; 
private boolean run = true; 


    public Broadcasts(UDPSocketHandler parent) throws UnknownHostException{ 
    InetAddress aHost = InetAddress.getLocalHost(); 

    sender = new Runnable() { 
     public void run() { 
      byte data[] = "Hello".getBytes(); 
      DatagramSocket socket = null; 
      try { 
       socket = new DatagramSocket(); 
      } catch (SocketException ex) { 
       ex.printStackTrace(); 
       parent.quit(); 
      } 

      DatagramPacket packet = new DatagramPacket(
        data, 
        data.length, 
        aHost, 
        9090); 
      while (run) { 
       try { 

        System.out.println("what is sent"+new String(packet.getData())); 
        socket.send(packet); 
        Thread.sleep(1000); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
        parent.quit(); 
       } catch (InterruptedException ex) { 
        ex.printStackTrace(); 
        parent.quit(); 
       } 
      } 
     } 
    }; 


    receiver = new Runnable() { 
     public void run() { 
      byte data[] = new byte[0]; 
      DatagramSocket socket = null; 
      try { 
       socket = new DatagramSocket(9090); 
      } catch (SocketException ex) { 
       ex.printStackTrace(); 
       //parent.quit(); 
      } 
      DatagramPacket packet = new DatagramPacket(data, data.length); 
      //System.out.println("this is what has been received"+packet.getData()); 
      String temp; 
      // while (run) { 
       try { 
        socket.receive(packet); 
        System.out.println("this is what has been received"+packet.getData()); 

        //System.out.println("Message received ..."+ temp); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
        parent.quit(); 
       } 

      //} 
     } 
    }; 


    new Thread(sender).start(); 
    new Thread(receiver).start(); 

} 

    public void quit() { 
    run = false; 
    } 
} 

は、それから私はこの問題は、受信機が何も受信しないということです

public class UDPSocketHandler { 

    private final Broadcasts broadcasts; 
// continue running application? 
    private boolean run = true; 

    public UDPSocketHandler() throws UnknownHostException 
    { 
    // start socket server to accept incoming connections 
    new Thread().start(); 


    // late initialize of UDP broadcast and receive, to ensure needed 
    // objects are instantiated 
    broadcasts = new Broadcasts(this); 
} 

// global quit method shuts down everything and exits 
public void quit() { 
    run = false; 
    broadcasts.quit(); 
    System.exit(0); 
} 

// application entry 
    public static void main(String[] args) throws UnknownHostException{ 
    new UDPSocketHandler(); 

    } 
} 

私の通信を処理するために、以下のクラスを持っています。私が理解したところから、このプログラムで送信者を実行し、このプログラムで受信することができました(question)。それは実際に私がしたいことですが、TCPの代わりにUDPを使用しています。私のコードの問題は何ですか?

答えて

0

いくつかの努力と時間を経て、私は最終的に私のプログラムを働かせることができました。私は以下を持っています:

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.*; 

public class SocketTest { 
    private boolean run = true; 


    public static void main(String[] args) throws IOException { 

    startServer(); 
    startSender(); 
    } 

    public static void startSender() throws UnknownHostException{ 
    InetAddress aHost = InetAddress.getLocalHost(); 
    (new Thread() { 
     @Override 
     public void run() { 
      byte data[] = "Hello".getBytes(); 
      DatagramSocket socket = null; 
      try { 
       socket = new DatagramSocket(); 
       socket.setBroadcast(true); 
      } catch (SocketException ex) { 
       ex.printStackTrace(); 
       //parent.quit(); 
      } 

      DatagramPacket packet = new DatagramPacket(
        data, 
        data.length, 
        aHost, 
        9090); 
      int i=0; 
      while (i<10) { 
       try { 

        System.out.println("what us mmmm.."+new String(packet.getData())); 
        socket.send(packet); 
        Thread.sleep(50); 
        i++; 
        System.out.println(i); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
        // parent.quit(); 
       } catch (InterruptedException ex) { 
        ex.printStackTrace(); 
        // parent.quit(); 
       } 
      } 
     }}).start(); 
    } 


    public static void startServer() { 
    (new Thread() { 
     @Override 
     public void run() { 

       //byte data[] = new byte[0]; 
       DatagramSocket socket = null; 
       try { 
        socket = new DatagramSocket(9090); 
        //socket.setBroadcast(true);; 

       } catch (SocketException ex) { 
        ex.printStackTrace(); 
        //parent.quit(); 
       } 
       DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); 
       //System.out.println("this is what has been received111"+packet.getData()); 
       String temp; 
       while (true) { 
       try { 
        socket.receive(packet); 
        temp=new String(packet.getData()); 
        System.out.println("this is what has been received"+temp); 


        //System.out.println("Message received ..."+ temp); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
        //parent.quit(); 
       } 

       } 
      } 

    }).start(); 
} 
} 

このコードでは、各ノードはクライアントとサーバーの両方として動作できます。要求を送信するサーバーの正しいポート番号を渡すだけです。このサンプルコードでは、ポート番号はパケットを自身に送信するクライアントと同じです。これが他人を助けてくれることを願っています

関連する問題