2011-11-08 74 views
0

我試圖編寫一個程序,使用具有android手機作爲客戶端和我的電腦作爲服務器我發現和稍微修改代碼雙方和我的服務器代碼工作,但我可以' t得到客戶的活動,我不知道如何得到的程序談話我想基本上能夠有任何一方輸入文本,並已發送和設置可見其他我知道有多少堅持你們有問題,所以...我如何獲得這些溝通?服務器/客戶端Android到PC通信問題

客戶端代碼(仿真器崩潰之前,我甚至可以開始看到這個問題)

package com.example.Socket; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.*; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import java.io.*; 
import java.net.*; 


public class ClientActivity extends Activity { 

    private EditText serverIp; 

    private Button connectPhones; 

    private String serverIpAddress = ""; 

    private boolean connected = false; 

    private Handler handler = new Handler(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.client); 

     serverIp = (EditText) findViewById(R.id.server_ip); 
     connectPhones = (Button) findViewById(R.id.connect_phones); 
     connectPhones.setOnClickListener((android.view.View.OnClickListener) connectListener); 
    } 

    private OnClickListener connectListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (!connected) { 
       serverIpAddress = serverIp.getText().toString(); 
       if (!serverIpAddress.equals("")) { 
        Thread cThread = new Thread(new ClientThread()); 
        cThread.start(); 
       } 
      } 
     } 
    }; 

    public class ClientThread implements Runnable { 

     public void run() { 
      try { 
       InetAddress serverAddr = InetAddress.getByName(serverIpAddress); 
       Log.d("ClientActivity", "C: Connecting..."); 
       Socket socket = new Socket(serverAddr, 9999); 
       connected = true; 
       while (connected) { 
        try { 
         Log.d("ClientActivity", "C: Sending command."); 
         PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket 
            .getOutputStream())), true); 
          // where you issue the commands 
          out.println("Hey Server!"); 
          Log.d("ClientActivity", "C: Sent."); 
        } catch (Exception e) { 
         Log.e("ClientActivity", "S: Error", e); 
        } 
       } 
       socket.close(); 
       Log.d("ClientActivity", "C: Closed."); 
      } catch (Exception e) { 
       Log.e("ClientActivity", "C: Error", e); 
       connected = false; 
      } 
     } 
    } 
} 

服務器代碼

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

/** 
* Simple server using Java Sockets. 
* @author Jonathan Engelsma (http://www.cis.gvsu.edu/~engelsma) 
* 
*/ 
public class Server { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 

    try { 
     // First we create a server socket and bind it to port 9999. 
     ServerSocket myServerSocket = new ServerSocket(9999); 


     // wait for an incoming connection... 
     System.out.println("Server is waiting for an incoming connection on host=" 
       + InetAddress.getLocalHost().getCanonicalHostName() 
       + " port=" + myServerSocket.getLocalPort()); 
     Socket skt = myServerSocket.accept(); 

     // ok, got a connection. Let's use java.io.* niceties to read and write from the connection. 
     BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
     PrintStream myOutput = new PrintStream(skt.getOutputStream()); 

     // attempt to read input from the stream. 
     String buf = myInput.readLine(); 

     // if we got input, print it out and write a message back to the remote client.. 
     if (buf != null) { 
      System.out.println("Server read: [" + buf + "]"); 
      myOutput.print("got it"); 
     } 

     // close the connection. 
     skt.close(); 
     System.out.println("Server is exiting"); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
     System.out.println("Whoops, something bad happened! I'm outta here."); 
    } 



} 

}

+0

它什麼時候崩潰?當你嘗試連接時它會崩潰嗎?或活動何時啓動? – Jbecwar

+0

它說程序被迫關閉錯誤是從模擬器不是蝕,它似乎是一個運行時錯誤 –

回答

0

是否使用

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

在Android Manifest中?這可能會導致你的問題。

+0

不,它仍然強制關閉應用程序 –

+0

放在您的客戶端和服務器的一個斷點,然後一步一步你的代碼,這應該讓你知道哪個調用它正在打破。圍繞着一個try/catch。使用套接字,你可以使用catch(UnknownHostException e)和catch(IOException e)。請記住,如果這是在另一個try/catch中,它將默認爲外部catch(Exception e),它不會爲您提供足夠的信息來調試問題。同樣使用LogCat,紅色語句表明有問題。讓我們知道拋出的異常和LogCat輸出,並且應該很容易找到問題的原因。 – Dave

+0

並且當然不要選擇「run as」選擇「debug as」。 ;) – Dave

0

如果您使用的是ICS或JB,那麼您可能被禁止在主要活動中打開網絡連接。您將收到有關網絡權限的不透明錯誤消息。

我不能重新發布我的答案在這裏另一個相當類似的StackOverflow問題(它被認爲是由版主發送垃圾郵件),但你可以CHECK IT OUT HERE

我發佈了一個功能發送和接收套接字連接器客戶端使用asynctask那裏。