套接字连接错误'null',Android [英] Error 'null' in sockets connection , Android

查看:69
本文介绍了套接字连接错误'null',Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做的事情:
我现在试图构建一个测试应用程序,只是通过套接字连接在Android手机(4.2.2)(作为客户端)上的应用程序与在PC(Windows 8)(作为服务器)上运行的Java应用程序之间建立连接./p>

我已经完成的工作:
我已经在PC上的Java中为客户端和服务器制作了程序,并对其进行了积极的测试(建立了连接).

网络:
我的手机和PC均已连接到我家的wifi.PC上的 ipconfig 显示地址 192.168.56.1 ,而登录路由器时,其显示我的PC地址为 192.168.0.108 (可能我不了解网络:P).

代码:
客户端(Android)

  public void connectPC(视图){尝试{clientSocket =新的Socket("192.168.0.108",1025);outstream = clientSocket.getOutputStream();instream = clientSocket.getInputStream();data_out =新的DataOutputStream(outstream);data_in =新的DataInputStream(instream);statusView.setText("Connected!");}捕获(异常e){statusView.setText("Error:" + e.getMessage());}} 

服务器:

  import java.net.*;导入java.io. *;公共类ServerSide扩展线程{ServerSocket serverSocket;公共ServerSide(int端口)抛出IOException{serverSocket =新的ServerSocket(1025);serverSocket.setSoTimeout(10000);}公共无效run(){而(真){System.out.println(正在等待端口上的客户端:" + serverSocket.getLocalPort());套接字服务器;尝试 {服务器= serverSocket.accept();System.out.println(已连接至:" + server.getRemoteSocketAddress());} catch(IOException e){//TODO自动生成的catch块System.out.println(e.getMessage());}}}公共静态void main(String ... args){int端口= 6066;尝试{线程t =新的ServerSide(port);t.start();}catch(IOException e){System.out.println(e.getMessage());}}} 

问题:-只是没有建立连接,catch块将 e.getMessage()显示为空.

PS 我也尝试过 192.168.56.1 IP地址.并在清单文件中添加了使用权限

请在这方面提供任何帮助..!

解决方案

您需要打印堆栈跟踪信息,而不仅仅是异常消息.这将为您提供更多信息来调试问题……包括异常的名称以及引发异常的位置.

此外,捕获 Exception 并尝试从中恢复也是一个坏主意.捕获 Exception 可能会捕获您从未期望的各种异常.从您没有想到的异常中恢复是有风险的……因为您不能确保这样做是安全的.通常最好让应用程序死掉...

What I was trying to do:
I was trying to build a test app, for now, simply establishing connection between the app on Android phone (4.2.2)(as client) and a java application running on pc (windows 8)(as server) via sockets connection.

What I've done already:
I've made the programs for both client and server in java on pc and tested them positively (Connection got established).

The network:
Both my phone and pc are connected to wifi at my home.ipconfig on pc shows address 192.168.56.1 while on logging into router it shows address of my pc to be 192.168.0.108 (Probably I don't understand networking :P).

The code:
client(Android)

public void connectPC(View view)
{
    try
    {

        clientSocket = new Socket("192.168.0.108",1025);
        outstream = clientSocket.getOutputStream();
        instream = clientSocket.getInputStream();
        data_out = new DataOutputStream(outstream);
        data_in = new DataInputStream(instream);
        statusView.setText("Connected!");
    }
    catch (Exception e)
    {
        statusView.setText("Error: "+e.getMessage());
    }
}

The Server:

import java.net.*;
import java.io.*;
public class ServerSide extends Thread
{
    ServerSocket serverSocket;
    public ServerSide(int port) throws IOException
    {
        serverSocket = new ServerSocket(1025);
        serverSocket.setSoTimeout(10000);
    }

    public void run()
    {
        while(true)
        {
            System.out.println("Waiting for client on port : "+ serverSocket.getLocalPort());
            Socket server;
            try {
                server = serverSocket.accept();
                System.out.println("Connected to : " +server.getRemoteSocketAddress());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println(e.getMessage());
            }

        }
    }

    public static void main(String... args)
    {
        int port=6066;
        try
        {
            Thread t = new ServerSide(port);
            t.start();
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

The Problem:- The connection simply doesn't establish, the catch block shows e.getMessage() as null.

PS I've tried 192.168.56.1 ip address too. And added uses permission in manifest file

Any help in this regard please..!

解决方案

You need to print the stacktrace rather than just the exception message. That will give you more information to debug the problem ... including the name of the exception, and the place where it was thrown.

Also, it is a bad idea to catch Exception and attempt to recover from it. Catching Exception could catch all sorts of exceptions that you were never expecting. Recovering from exceptions that you weren't expecting is risky ... because you cannot be sure it is a safe thing to do. It is typically better to let the application die ...

这篇关于套接字连接错误'null',Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆