0

我的套接字连接有问题。它说这一行中有一个空指针异常:

Socket client = new Socket(ip,8000);

但是IP没问题,端口应该没问题!

这是完整的代码:

public class socket { 

public static void out(String out, Context context){

    final String PREFS_NAME = "SAVEDIP";
    final String PREF_IP = "";
    String ip = null;
    SharedPreferences pref = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    ip = pref.getString(PREF_IP, "");
    Log.e("IP", ip);


    try {
        Socket client = new Socket(ip,8000);    
        //PrintWriter output = new PrintWriter(client.getOutputStream(), true);
        //BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        //output.println(out);
        Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
        //output.close();
        //in.close();
        client.close();
    } catch (UnknownHostException e) {
        Log.e("TAG", "UnknowHostExeption");
        e.printStackTrace();
    } 
    catch (IOException e) {
        Log.e("TAG", "IOExeption");
        e.printStackTrace();
    }
}

}

我正在传递上下文和文本:

c = getApplicationContext();
socket.out("exit", c);

我得到的错误如下:

    04-23 15:58:30.577: E/TAG(26540): IOExeption
04-23 15:58:30.577: W/System.err(26540): java.net.SocketException: The operation timed out
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
04-23 15:58:30.577: W/System.err(26540):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:219)
04-23 15:58:30.577: W/System.err(26540):    at java.net.Socket.startupSocket(Socket.java:781)
04-23 15:58:30.577: W/System.err(26540):    at java.net.Socket.tryAllAddresses(Socket.java:194)
04-23 15:58:30.577: W/System.err(26540):    at java.net.Socket.<init>(Socket.java:258)
04-23 15:58:30.587: W/System.err(26540):    at java.net.Socket.<init>(Socket.java:222)
04-23 15:58:30.587: W/System.err(26540):    at com.example.andremote.socket.out(socket.java:28)
04-23 15:58:30.587: W/System.err(26540):    at com.example.andremote.Remote$11.onClick(Remote.java:175)
04-23 15:58:30.587: W/System.err(26540):    at android.view.View.performClick(View.java:2408)
04-23 15:58:30.597: W/System.err(26540):    at android.view.View$PerformClick.run(View.java:8817)
04-23 15:58:30.597: W/System.err(26540):    at android.os.Handler.handleCallback(Handler.java:587)
04-23 15:58:30.597: W/System.err(26540):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 15:58:30.597: W/System.err(26540):    at android.os.Looper.loop(Looper.java:143)
04-23 15:58:30.597: W/System.err(26540):    at android.app.ActivityThread.main(ActivityThread.java:4914)
04-23 15:58:30.607: W/System.err(26540):    at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:58:30.607: W/System.err(26540):    at java.lang.reflect.Method.invoke(Method.java:521)
04-23 15:58:30.607: W/System.err(26540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-23 15:58:30.607: W/System.err(26540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-23 15:58:30.607: W/System.err(26540):    at dalvik.system.NativeStart.main(Native Method)

请告诉我我在哪里做错了,我找不到solotiun!

4

1 回答 1

0

显然在这条线上

ip = pref.getString(PREF_IP, "");

在右侧返回空值,因此您的 ip 变量仍然为空值或无效值,因此当您将其作为参数传递给 Socket 构造函数时,因为您的 ip 变量(主机名)无效,它会产生 IOException。尝试检查您的 ip 变量是否为 null 或在将其传递给 Socket 构造函数之前。

于 2013-04-23T14:11:02.533 回答