简体   繁体   中英

The mystery of the silent string (server receiving null string from Android Client)

I'm trying to get my android to talk to my computer, and I've almost got it. I can send strings over the internet to my computer and all, but there's just one problem: when my computer reads the strings they're null!

Here's the code for the 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;
    }
}
}

And here's the code for the Server:

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();
        }
    }
}

Any help at all would be appreciated.

EDIT: I found the problem; originally i wasn't using the emulator to test my code i was using my actual android because i couldn't get the wifi to work on the emulator (it was a simple fix) so when ran the code on my emulator it works, no null string; but when ran on my real android it returns null, there's only one reason i could think of that would cause this to happen, the sdk I'm using doesn't match my androids os version (its older) could this be the reason or am i wrong?

EDIT: I got it, it turns out i just needed to install some of the api 10 packages from androids sdk manager works like a charm now :)

The null strings are just a way for readline to signalize that the input stream has ended, following your call to close on the client.

Your code is basically supposed to work. Add more logging and disable the Nagle algorithm for faster (easier) debugging by calling setTcpNoDelay on the client socket before you connect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM