2

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

Salim Shamim
  • 656
  • 10
  • 25
  • Print at least the exception class name as well as the message, for example by using `Exception.toString().` Better still, p print the entire stack trace. – user207421 Feb 07 '16 at 07:47

2 Answers2

3

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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)
saksham agarwal
  • 250
  • 3
  • 14