Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServerSocket not accepting on droid emulator [duplicate]

I'm trying to set up a basic network connection using the droid emulators with eclipse but I'm having problems. The server reaches the line "Socket s = ss.accept();" and waits for the client to connect, but the client can't seem to connect.

Now I've been able to have my client connect to a python server that I created and does the same thing as the server I made for the droid. I've allowed the Internet Uses on both client and server in the AndroidManifest so I'm at a lose as to why this server isn't working on my droid emulator.

Sever:

       ServerSocket ss = new ServerSocket(8888);
            while(!end){
                    Socket s = ss.accept();
                    Log.v("Server","Connection found");
                    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    PrintWriter output = new PrintWriter(s.getOutputStream(),true); 
                    String st = input.readLine();

Client:

        Socket s = new Socket(MYIPADDR,8888);

        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));

Any help would be greatly appreciated.

like image 458
Travis Frazier Avatar asked Nov 13 '22 10:11

Travis Frazier


1 Answers

Just adding to Luka's answer since you're probably going to run into future issues:

If you're using sockets, you may want to use a library to help with all the maintenance that's really required for a stable connection.

A good library to use for handling the connection would be: https://github.com/koush/android-websockets

It'll keep the connection alive and is fairly simple to change around to fit your sending needs. The only thing it's missing is a callback feature to deal with incoming data, but that would be way easier to implement than entirely setting up something from scratch to deal with the whole connection.

like image 83
Cruceo Avatar answered Nov 16 '22 02:11

Cruceo