0

I usually don't program in Java, but I decided to write a simple client-server application just to figure how this works in Java.

When I run both server and client through eclipse - everything's fine, but when I run the client in my Android device through Android Studio - it doens't connect to the server.

Both running on same WIFI. Server IP is pc's LAN ip. Server listens on port 32323, client connects to 32323.

Server:

public class Server
{
    private int port;
    private ServerSocket serverSocket;

    private Socket client;

    public Server(int port)
    {
        this.port = port;

        try
        {
            serverSocket = new ServerSocket(port);
        }
        catch (Exception exception) {}


        try
        {
            client = serverSocket.accept();
            System.out.println("logged in");
        }
        catch (Exception exception) {}

    }
}

Client:

// The following line is written inside onCreate:
onlineClient = new OnlineClient(new byte[] {(byte) 192, (byte) 168, 1, 17}, 32323); 

public class OnlineClient
{
    private Socket socket;
    private PrintWriter writer;
    private BufferedReader reader;

    public OnlineClient(byte[] serverIP, int port)
    {
        try
        {
            socket = new Socket(Inet4Address.getByAddress(serverIP), port);
//          writer = new PrintWriter(socket.getOutputStream(), true);
//          reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//          socket.setTcpNoDelay(true);
        }
        catch (IOException e) {}

    }
}
W2a
  • 736
  • 2
  • 9
  • 23
  • 1
    You're losing all the caught exceptions. You should at least log them. It might give you more insight to what's happening, and it's just a good practice to be in always in Java. – Kon May 25 '16 at 14:54
  • 1
    Without stack traces it will be hard for anyone to figure this out. Would be simpler to just use String parameters then the "192.168.1.17"? – Mr00Anderson May 25 '16 at 14:59
  • Why don't you debug your code? – f1sh May 25 '16 at 15:00
  • It's crashing in `Inet4Address.getByAddress(serverIP)` (or the `new Socket` part) because of http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception – zapl May 25 '16 at 15:03

0 Answers0