通過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#服務器,而沒有任何錯誤)和下一個消息慣於發送。
同樣的結果,只發送一條消息。 – user3351852
奇怪。在客戶端,你有沒有例外? –
沒有任何例外。只有一個發送。 – user3351852