2013-01-03 33 views
0

我正嘗試在兩個不同的系統上在運行於android模擬器和服務器上的客戶端程序之間建立一個簡單的UDP連接。服務器端很好,但客戶端不斷崩潰。這是模擬器的問題嗎?我應該重定向港口,使其工作?在Android模擬器上的UDP

客戶端(在Android模擬器):

package com.example.clientrecv; 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.widget.Button; 
import android.widget.Toast; 


public class MainActivity extends Activity 
{ 
public String text; 
public int serverport=1234; 
public byte[] message=new byte[1000]; 
public Button b; 
public DatagramPacket p; 
public DatagramSocket s; 
public Toast t; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    b=(Button) findViewById(R.id.button1); 

       try { 
        p = new DatagramPacket(message,message.length); 
        s = new DatagramSocket(serverport); 
        try { 
         s.receive(p); 
        } catch (IOException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
        text= new String(message,0,p.getLength()); 
        Log.d("hello","the message:"+text); 
        s.close(); 
       // TODO Auto-generated method stub 



    } catch (SocketException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

} 
} 
       public void showmsg() 
       { 
        t=Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG); 
        t.show(); 
       } 
} 


SERVER SIDE: (on pc) 


import java.io.*; 
import java.net.*; 
class serversend 
{ 
public static void main(String args[]) throws Exception 
    { 
String strmsg="Server says hello"; 
int serverport=1234; 
int len=strmsg.length(); 
System.out.println("starting"); 
byte[] message=strmsg.getBytes(); 
try{ 
InetAddress local=InetAddress.getByName("localhost"); 
DatagramSocket s=new DatagramSocket(); 
DatagramPacket p=new DatagramPacket(message,len,local,serverport); 
System.out.println("Running"); 
s.send(p); 
System.out.println("Sent"); 
}catch(Exception e) 
{ 
    System.out.println("caught"); 
} 
    } 
} 
+1

您是否定義了客戶端和服務器之間的通信端口? –

+0

是的。但我不知道如何使它溝通。它甚至有可能嗎? – user1944690

+0

我甚至嘗試用以下命令重定向它:telnet localhost 5554 redir add udp: user1944690

回答

0

有可用,可以幫助您在使用UDP 這兩者之間的溝通應該添加在服務器端,客戶端端口服務器和客戶機之間的通信,大量的實例

InetAddress local = InetAddress.getByName("192.168.1.102"); 

這裏有以下鏈接客戶端服務器通信使用UDP LINK UDP1 Link UDP2

+0

這些應該在客戶端,對不對?並且該代碼可以與使用java的普通UDP協同工作。但我無法在模擬器上運行它。 – user1944690

+0

你有沒有通過給定的鏈接? 是不是顯示適當的結果?任何崩潰警告?如果是這樣,然後添加日誌貓隨着你的問題 –

+0

是的,我經歷了。模擬器說「應用程序已停止」 – user1944690

0

Android 3.0版本之後的Android不允許您在主UI線程中執行聯網操作。您必須爲此定義一個新線程...以下是您可以這樣做的方法:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

      //here you Set up you parameters and launch the thread (e.g): 

      this.newThread = new Thread(new mThread()); 
     this.newThread.start(); 


     /* Next you define your newThread's run method in wich all networking 
     operations must take place*/ 


    class mThread implements Runnable { 
     public void run() { 

     // Do all networking tasks you need            

      } 
     }