1

我的 android 套接字有问题,我有两个 android 应用程序Server,并且ServerClient 在下面写的第二行中 ServerClient给出nullPointerException

InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);

我检查serverAddr的是 10.0.2.2 ,但后来我看到套接字为空。有人可以帮忙吗?

编辑:使用此 serverAddress 我看到它无法访问,也许我必须手动使其可访问?这是来源

*ServerClient*

package com.example.serverclient;

import android.os.Bundle;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.Menu;

public class MainActivity extends Activity {

    private Button bt;
    private TextView tv;
    private Socket socket;
    private String serverIpAddress = "10.0.2.2";
    private static final int REDIRECTED_SERVERPORT = 5000;

    // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
    // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
    // PORT 6000

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       bt = (Button) findViewById(R.id.myButton);
       tv = (TextView) findViewById(R.id.myTextView);
       try {
          InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
          socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
       } catch (UnknownHostException e1) {
          e1.printStackTrace();
       } catch (IOException e1) {
          e1.printStackTrace();
       }
       bt.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
             try {
                EditText et = (EditText) findViewById(R.id.EditText01);
                String str = et.getText().toString();
                OutputStream sk = socket.getOutputStream();

                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                out.println(str);
                Log.d("Client", "Client sent message");
             } catch (UnknownHostException e) {
                tv.setText("Error1");
                e.printStackTrace();
             } catch (IOException e) {
                tv.setText("Error2");
                e.printStackTrace();
             } catch (Exception e) {
                tv.setText("Error3");
                e.printStackTrace();
             }
          }
       });
    }
}


**Server**
package com.example.myserver;

import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    ServerSocket ss = null;
       String mClientMsg = "";
       Thread myCommsThread = null;
       protected static final int MSG_ID = 0x1337;
       public static final int SERVERPORT = 6000;

       @Override
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.TextView01);
        tv.setText("Nothing from client yet");
        this.myCommsThread = new Thread(new CommsThread());
        this.myCommsThread.start();
       }

       @Override
       protected void onStop() {
        super.onStop();
        try {
            // make sure you close the socket upon exiting
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
       }

       Handler myUpdateHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_ID:
                TextView tv = (TextView) findViewById(R.id.TextView01);
                tv.setText(mClientMsg);
                break;
            default:
                break;
            }
            super.handleMessage(msg);
        }
       };
       class CommsThread implements Runnable {
        public void run() {
            Socket s = null;
            try {
                ss = new ServerSocket(SERVERPORT );
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (!Thread.currentThread().isInterrupted()) {
                Message m = new Message();
                m.what = MSG_ID;
                try {
                    if (s == null)
                        s = ss.accept();
                    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    String st = null;
                    st = input.readLine();
                    mClientMsg = st;
                    myUpdateHandler.sendMessage(m);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        }
}
4

3 回答 3

1

你确定你没有得到 aUnknownHostException或 aIOException吗?其中之一必须被调用,否则socket不会为空。

于 2012-10-16T15:26:37.827 回答
0

将错误 3 para 的(Exception e)更改为 NullPointerException,然后运行客户端并发送消息。错误 3 将不再显示在屏幕上,您将在 Myserver 上收到消息。

异常 e --> NullPointerException

于 2014-02-22T04:53:27.193 回答
0

此类返回您的响应,但还需要清单文件中的互联网许可。

public class ChatApplication extends Application {

    private Socket mSocket;
    {
        try {
            mSocket = IO.socket("http://chat.socket.io");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public Socket getSocket() {
        return mSocket;
    }
}
于 2016-09-26T10:25:11.220 回答