Error 'null' in sockets connection , Android [duplicate]

强颜欢笑 提交于 2019-12-02 02:45:02

问题


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..!


回答1:


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 ...




回答2:


I would like to give you some suggestions:

  1. use asynctask in android for networking activities otherwise NetworkOnMainThreadException occur because it is good to run all time consuming activities in background.Also keep in mind do all task in doBackgroung function of asynctask and then update and publish result with help of onPostExecute() and onProgress().
  2. If you are not using asynctask then simply use thread and perform all networking activity on separate thread.
  3. In java software ,track IP address by using Enumeration instead of InetAddress because Enumeration will show all IP address on network and probably you will find answer of your question.(Try to connect to all the IP that is shown by Enumeration method and connection is established with suitable one automatically)


来源:https://stackoverflow.com/questions/35240907/error-null-in-sockets-connection-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!