2013-08-22 59 views
0

這裏是我的代碼,用於在局域網內僅在一臺設備上或一臺監聽服務器上發送字符串消息,並且通過在客戶端中定義服務器的IP來完成,客戶端和服務器程序都是android app.But我想廣播這條消息,以便所有的偵聽服務器都能收到我的消息。通過android應用程序在局域網中廣播消息

這裏是我的客戶端代碼:

public class MainActivity extends Activity {  
    private Socket socket;  
    private static final int SERVERPORT = 6000; 
    private static final String SERVER_IP = "10.0.2.2"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);  

     new Thread(new ClientThread()).start(); 
    } 

    public void onClick(View view) { 
     try { 
      EditText et = (EditText) findViewById(R.id.EditText01); 
      String str = et.getText().toString(); 
      PrintWriter out = new PrintWriter(new BufferedWriter(
        new OutputStreamWriter(socket.getOutputStream())), 
        true); 
      out.println(str); 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    class ClientThread implements Runnable { 

     @Override 
     public void run() { 

      try { 
       InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 

       DatagramSocket = new Socket(serverAddr, SERVERPORT); 

      } catch (UnknownHostException e1) { 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      }  
     }  
    }} 

這裏是我的服務器代碼:

public class MainActivity extends Activity {  
    private DatagramSocket serverSocket;  
    Handler updateConversationHandler;  
    Thread serverThread = null;  
    private TextView text;  
    public static final int SERVERPORT = 6000; 

    @Override 
    public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     text = (TextView) findViewById(R.id.text2); 

     updateConversationHandler = new Handler(); 

     this.serverThread = new Thread(new ServerThread()); 
     this.serverThread.start();  
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     try { 
      serverSocket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    class ServerThread implements Runnable { 

     public void run() { 
      Socket socket = null; 
      try { 
       serverSocket = new ServerSocket(SERVERPORT); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      while (!Thread.currentThread().isInterrupted()) { 

       try { 

        socket = serverSocket.accept(); 

        CommunicationThread commThread = new CommunicationThread(socket); 
        new Thread(commThread).start(); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    class CommunicationThread implements Runnable { 

     private Socket clientSocket;  
     private BufferedReader input;  
     public CommunicationThread(Socket clientSocket) { 

      this.clientSocket = clientSocket; 

      try {  
       this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     public void run() {  
      while (!Thread.currentThread().isInterrupted()) { 

       try {  
        String read = input.readLine(); 

        updateConversationHandler.post(new updateUIThread(read)); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

    class updateUIThread implements Runnable { 
     private String msg;  
     public updateUIThread(String str) { 
      this.msg = str; 
     } 

     @Override 
     public void run() { 
      text.setText(text.getText().toString()+"Client Says: "+ msg + "\n"); 
     } 
    }} 

,我要在客戶端或服務器做了哪些改變,這樣我可以播放局域網內所有的字符串消息聽力服務器應該收到,謝謝提前。請幫助。

回答

0

只是嘗試從network.this代碼獲取廣播地址可以幫助你以提取廣播地址。

private InetAddress getBroadcastAddress() throws IOException { 
DhcpInfo dhcp = mWifi.getDhcpInfo(); 
if (dhcp == null) { 
    Log.d(TAG, "Could not get dhcp info"); 
    return null; 
} 

int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; 
byte[] quads = new byte[4]; 
for (int k = 0; k < 4; k++) 
    quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); 
return InetAddress.getByAddress(quads);} 
0

您不能使用TCP廣播消息。嘗試使用UDP套接字通過

+0

我知道,我使用UDP,你可以看到我的上述代碼。 –

+0

您的代碼是否正在編譯?我看到代碼DatagramSocket serverSocket = new ServerSocket(SERVERPORT) – Tishka17

+0

代碼是好的只是幫助我知道如何在目標IP中做什麼changings以便廣播? @ Tishka17 –

相關問題