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

标签 java android client

我正在尝试让我的机器人与我的电脑对话,我快成功了。我可以通过 Internet 将字符串发送到我的计算机等等,但只有一个问题:当我的计算机读取字符串时,它们为空!

这是客户端的代码:

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 在模拟器上工作(这是一个简单的修复)所以当在我的模拟器上运行代码时它工作,没有空字符串;但是当在我真正的 android 上运行时它返回 null,我能想到的只有一个原因会导致这种情况发生,我正在使用的 sdk 与我的 androids os 版本(它的旧版本)不匹配这可能是原因还是我错了吗?

编辑:我明白了,事实证明我只需要从 androids sdk 管理器安装一些 api 10 包现在就像一个魅力:)

最佳答案

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

您的代码基本上应该可以工作。通过在连接之前在客户端套接字上调用 setTcpNoDelay 添加更多日志记录并禁用 Nagle 算法以更快(更容易)调试。

关于java - 静默字符串之谜(服务器从Android客户端接收空字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215893/

相关文章:

java - Android ViewGroup.setScaleX() 导致 View 被裁剪

java - 使用 Aquery 将图像下载到 Google map 标记

c# - 可以将 Windows 上的 tcp 客户端连接到 Linux 上的服务器吗?

Java面向事件的Socket库

java - 即使值存在于 xml 中,为什么我在 java 中从 xml 得到 null ?

java - Android应用无法在Api 19上打开

android - 在 Android 中保存状态不起作用

当文本框以编程方式更改时的 javascript 触发事件

java - 无法使用JNI从C++中的jar访问某些类

java - Window bat脚本启动java程序加载dll失败,但是使用IntelliJ idea启动成功?