2014-02-25 54 views
2

通過TCP發送數據我想從Android應用 客戶端類的src發送數據:從Android應用

import java.io.*; 
import java.net.Socket; 
import java.net.UnknownHostException; 

public class CClient  
implements Runnable  
{ 
    private Socket socket; 
    private String ServerIP = "10.0.0.14"; 


    public void run() 
    { 
     try 
     { 
      socket = new Socket("10.0.0.14", 16606); 

     } 
     catch(Exception e) 
     { 
      System.out.print("Whoops! It didn't work!:"); 
      System.out.print(e.getLocalizedMessage()); 
      System.out.print("\n"); 
     } 
    } 

    public void Send(String s) 
    { 
     try 
     { 
      PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 
      outToServer.print(s + "\n"); 
      outToServer.flush(); 


     } 
     catch (UnknownHostException e) { 
      System.out.print(e.toString()); 
     } catch (IOException e) { 
      System.out.print(e.toString()); 
     }catch (Exception e) { 
      System.out.print(e.toString()); 
     } 

    } 
} 

使用CClient類的活動來創建連接併發送數據。 這裏是活動代碼

import android.os.Bundle; 
import android.app.Activity; 
import android.view.*; 
import android.widget.*; 


public class Auth extends Activity { 

    private ProgressBar mProgress; 
    private TextView mTV; 

    private CClient mClient; 

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


     mProgress = (ProgressBar) findViewById(R.id.progressBar1); 
     mTV = (TextView) findViewById(R.id.textView4); 

     mClient = new CClient();     
     Thread myThready = new Thread(mClient); 
     myThready.start(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.auth, menu); 
     return true; 
    } 

    public void proc_Login(View v) 
    { 

     for (int i=0; i<5; i++) 
      mClient.Send("asaadsasdasd"); 
    } 

} 

問題:recive從客戶只有一個消息(C#服務器,而沒有任何錯誤)和下一個消息慣於發送。

回答

2

此代碼有效。我不得不添加一個簡單的按鈕來觸發proc_Login調用,我在我的服務器(debian)上使用

netcat -l -p 8546
進行了測試,每次按下按鈕時我都會得到5個「asaadsasdasd」。 (我使用了端口8546,因爲它已經在我的防火牆上打開了)。

當然,我在AndroidManifest.xml文件中添加

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

也許你的服務器代碼收到一行後關閉套接字。

注意你也不需要一個線程,一個AsyncTask會更合適,雖然它的工作原理。

MainActivity.java:

package com.defranoux.testtcpsend; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    private CClient mClient; 

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

    mClient = new CClient();     
    Thread myThready = new Thread(mClient); 
    myThready.start(); 

    Button button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
     proc_Login(arg0);   
     } 
    });  
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
    } 

    public void proc_Login(View v) 
    { 
     for (int i=0; i<5; i++) 
      mClient.Send("asaadsasdasd"); 
    } 

} 

CClient.java:

package com.defranoux.testtcpsend; 

import java.io.*; 
import java.net.Socket; 
import java.net.UnknownHostException; 

public class CClient  
implements Runnable  
{ 
    private Socket socket; 
    private String ServerIP = "<my server ip goes here>"; 
    private static final int ServerPort = 8546; 

    @Override 
    public void run() 
    { 
     try 
     { 
      socket = new Socket(ServerIP, ServerPort); 
     } 
     catch(Exception e) 
     { 
      System.out.print("Whoops! It didn't work!:"); 
      System.out.print(e.getLocalizedMessage()); 
      System.out.print("\n"); 
     } 
    } 

    public void Send(String s) 
    { 
     try 
     { 
      PrintWriter outToServer = new PrintWriter(
       new OutputStreamWriter(
        socket.getOutputStream())); 
      outToServer.print(s + "\n"); 
      outToServer.flush(); 


     } 
     catch (UnknownHostException e) { 
      System.out.print(e.toString()); 
     } catch (IOException e) { 
      System.out.print(e.toString()); 
     }catch (Exception e) { 
      System.out.print(e.toString()); 
     } 

    } 
} 
+0

同樣的結果,只發送一條消息。 – user3351852

+0

奇怪。在客戶端,你有沒有例外? –

+0

沒有任何例外。只有一個發送。 – user3351852

0

你永遠清理到服務器的連接。您需要在刷新流時關閉這些輸出流。

outToServer.flush(); 
outToServer.close(); 
+0

同樣的事情。 – user3351852

+0

你能解釋爲什麼你的CClient作爲一個單獨的線程運行,當你在線程中做的唯一工作是初始化套接字嗎?發送方法仍在主線程中執行 – Kakarot

+0

由於無法從主線程創建連接,只能從新線程創建連接。 – user3351852