2014-11-04 31 views
0

進入套接字編程我正在開發一個程序,用於在手機上運行我的android應用程序,以便基於我的機器與我的serlet進行通信。我得到一個異常說,套接字已關閉。 的servletside使用servlets進行android和servlet溝通

private ServerSocket serverSocket; 
    public static final int SERVERPORT = 8080; 
Socket socket=null; 
     serverSocket = new ServerSocket(SERVERPORT); 
     socket = serverSocket.accept(); 
     InputStream is =socket.getInputStream(); 
     DataInputStream dis =new DataInputStream(is); 
     String input = dis.readUTF(); 
     data1 = dis.readUTF(); 
     System.out.println(input+"   "+data1); 
     OutputStream dout=socket.getOutputStream(); 
     DataOutputStream dat = new DataOutputStream(dout); 
     dat.writeUTF("Hello to you too"); 
     dat.close(); 

的應用程序方面

private Socket socket; 
    private static final int SERVERPORT = 8080; 
    private static final String SERVER_IP = "192.168.1.8"; 
InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 
         socket = new Socket(serverAddr, SERVERPORT); 
         DataOutputStream out1 = new DataOutputStream(socket.getOutputStream()); 
         out1.writeUTF(ip); 
         out1.writeUTF(mac); 
        out1.close(); 
        Log.d("Message","WTF"); 
         InputStream is =socket.getInputStream(); 
         DataInputStream dis =new DataInputStream(is); 
         while(dis.available()==0); 
         Log.d("Message","came here 2"); 
         String WTF = dis.readUTF(); 
         dis.close(); 
         Log.d("Message",WTF); 
+0

你應該告訴更多正在發生的事情和異常到來之前被執行的語句。請發佈logcat。你也應該知道哪個陳述確實會引發這個錯誤。你沒有告訴服務器是否正確接收到IP和MAC。 – greenapps 2014-11-04 10:13:36

回答

0

儘量把對清單中的下一行;

<uses-permission android:name="android.permission.INTERNET"/> 
+0

2015-01-02 12:04:50