静音字符串之谜(服务器从Android客户端接收空字符串)

时间:2012-06-26 21:03:45

标签: java android client

我正试图让我的机器人与我的电脑对话,我差点搞定了。我可以通过互联网将字符串发送到我的计算机和所有,但只有一个问题:当我的计算机读取字符串时它们是空的!

以下是客户端的代码:

public class ClientActivity extends Activity implements View.OnClickListener{

EditText ip1,ip2,ip3,ip4, message, port;
TextView con;
Button send;
InetAddress inet;
Socket s;
OutputStream out;
PrintWriter output;
int sip;
int rport;
byte[] ip;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ip1 = (EditText) findViewById(R.id.etIP1);
    ip2 = (EditText) findViewById(R.id.etIP2);
    ip3 = (EditText) findViewById(R.id.etIP3);
    ip4 = (EditText) findViewById(R.id.etIP4);
    port = (EditText) findViewById(R.id.etPort);
    message = (EditText) findViewById(R.id.etMessage);
    send = (Button) findViewById(R.id.bSend);
    con = (TextView) findViewById(R.id.tvCon);
    ip = new byte[4];

    send.setOnClickListener(this);    }

@Override
public void onClick(View arg0) {
    switch(arg0.getId()){
        case R.id.bSend:

        try{
            rport = Integer.parseInt(port.getText().toString());
        } catch (NumberFormatException e){
            con.setText(e.toString());
            break;
        }

        try{
            sip = Integer.parseInt(ip4.getText().toString());
            sip = (sip << 8) + Integer.parseInt(ip3.getText().toString());
            sip = (sip << 8) + Integer.parseInt(ip2.getText().toString());
            sip = (sip << 8) + Integer.parseInt(ip1.getText().toString());
        } catch (NumberFormatException e){
            con.setText(e.toString());
            break;
        }

        ip[0] = (byte)(0xff & sip);
        ip[1] = (byte)(0xff & (sip >> 8));
        ip[2] = (byte)(0xff & (sip >> 16));
        ip[3] = (byte)(0xff & (sip >> 24));

        try {
            inet = InetAddress.getByAddress(ip);
            s = new Socket(inet, rport);
            out = s.getOutputStream();
            output = new PrintWriter(out);
            output.println(message.getText().toString());
            con.setText("Message Sent");
            s.close();
        } catch (UnknownHostException e) {
            con.setText(e.toString());
        } catch (IOException e) {
            con.setText(e.toString());
        }
        break;
    }
}
}

这是服务器的代码:

public class Main {
public static void main(String[] args) {
    try {
        Boolean end = false;
        ServerSocket ss = new ServerSocket(4331);
        while(!end){
                //Server is waiting for client here, if needed
                Socket s = ss.accept();
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
                String st = input.readLine();
                System.out.println(""+st);
                s.close();     
         }
         ss.close();

        } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

任何帮助都将不胜感激。

编辑:我发现了问题;最初我没有使用模拟器测试我的代码我使用我的实际的Android因为我无法让wifi在模拟器上工作(这是一个简单的修复)所以当我在我的模拟器上运行代码它工作,没有空字符串;但是当我在我的真实机器人上运行它返回null时,我只能想到一个会导致这种情况发生的原因,我正在使用的sdk与我的机器人os版本(它的旧版本)不匹配可能这是原因或我错了吗?

编辑:我明白了,事实证明我只需安装一些来自androids的api 10软件包sdk管理器就像现在的魅力一样:)

1 个答案:

答案 0 :(得分:0)

在您调用客户端上的readline之后,空字符串只是close表示输入流已结束的一种方式。

你的代码基本上应该可行。添加更多日志记录并禁用Nagle算法,以便在连接之前通过调用客户端套接字上的setTcpNoDelay来更快(更简单)地进行调试。

相关问题