• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with connecting app android to PC server

Sattam

Lurker
Hello,
Please help me with this problem
I'm trying to connect my Android app to a PC server using Socket. Unforunately, the PC server doesn't receive any connection request from the app (there's no connection between them). I'm running the app in android emulator.

I've seen many tutorials and discussions in the internet about this problem and I fellow them literally, but still having the same problem that I mentioned above.

Note: the android app is running on eclipse emulator and the PC server on netbeans


** the PC server source code:


public class server{
ServerSocket server;

public server() {

try
{
server =new ServerSocket(8000); // create server socket
Socket socket = server.accept(); // accept new connection request
System.out.println("The connection is active");

}
catch (IOException e)
{
System.err.println("error.... 1\n");
}
}
public static void main(String[] args) {
new server();
}
}

--------------------

**The android app source code (client):


public class Client extends Activity {
Button con;
Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

con = (Button)findViewById(R.id.myButton);

con.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

try
{

socket = new Socket("My_PC_IPAddress", 8000);
socket.close();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
});

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

----------------

Note:This permission was added to the meinfest.xml
<uses-permission android:name="android.permission.INTERNET"/>


Please help me to solve this problem
Thank you
 
Is port 8000 blocked by a firewall? Is it forwarded by your router? Are you using your external IP or internal IP? Is it a static ip or a dynamic ip?

I would also try removing the socket.close() call from your client code temporarily to ensure it isn't a timing issue.
 
Hi,
Thank you for your help

I tested the port 8000 by running the client code from another machine. The connection is succeed

Is it forwarded by your router? Yes

I'm using internal IP which is also dynamic

I'm running the android app and the server in the same machine

Note: When the client is running at PC gui it works but it doesn't work when the client is running on android emulator

Thank you
 
Back
Top Bottom