【发布时间】:2014-02-26 18:34:34
【问题描述】:
我正在尝试从 Android 应用程序发送数据 客户端类src:
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");
}
}
问题:仅从客户端(C# 服务器,没有任何错误)接收到一条消息,并且不会发送下一条消息。
【问题讨论】: