connecting clients to server with emulator on different computers
Posted
by
prolink007
on Stack Overflow
See other posts from Stack Overflow
or by prolink007
Published on 2011-11-28T01:29:10Z
Indexed on
2011/11/28
1:50 UTC
Read the original article
Hit count: 214
I am writing an application that communicates using sockets. I have a server running on one android emulator on a computer, then i have 2 other clients running on android emulators on 2 other computers. I am trying to get the 2 clients to connect to the server.
This works when i run the server and clients on the same computer, but when i attempt to do this on the same wifi network and on separate computers it gives me the following error. The client and server code is posted below. A lot is stripped out just to show the important stuff. Also, after the server starts i telnet into the server and run these commands redir add tcp:5000:6000
(i have also tried without doing the redir
but it still says the same thing). Then i start the clients and get the error. Thanks for the help!
Both the 5000 port and 6000 port are open on my router. And i have windows firewall disabled on the computer hosting the server.
11-27 18:54:02.274: W/ActivityManager(60): Activity idle timeout for HistoryRecord{44cf0a30 school.cpe434.ClassAidClient/school.cpe434.ClassAid.ClassAidClient4Activity}
11-27 18:57:02.424: W/System.err(205): java.net.SocketException: The operation timed out
11-27 18:57:02.454: W/System.err(205): at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
11-27 18:57:02.454: W/System.err(205): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:114)
11-27 18:57:02.465: W/System.err(205): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:245)
11-27 18:57:02.465: W/System.err(205): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
11-27 18:57:02.465: W/System.err(205): at java.net.Socket.startupSocket(Socket.java:780)
11-27 18:57:02.465: W/System.err(205): at java.net.Socket.<init>(Socket.java:314)
11-27 18:57:02.465: W/System.err(205): at school.cpe434.ClassAid.ClassAidClient4Activity.onCreate(ClassAidClient4Activity.java:102)
11-27 18:57:02.474: W/System.err(205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-27 18:57:02.474: W/System.err(205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-27 18:57:02.474: W/System.err(205): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 18:57:02.474: W/System.err(205): at android.os.Looper.loop(Looper.java:123)
11-27 18:57:02.486: W/System.err(205): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-27 18:57:02.486: W/System.err(205): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 18:57:02.486: W/System.err(205): at java.lang.reflect.Method.invoke(Method.java:521)
11-27 18:57:02.486: W/System.err(205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-27 18:57:02.486: W/System.err(205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-27 18:57:02.486: W/System.err(205): at dalvik.system.NativeStart.main(Native Method)
The server code
public class ClassAidServer4Activity extends Activity {
ServerSocket ss = null;
String mClientMsg = "";
String mClientExtraMsg = "";
Thread myCommsThread = null;
public static final int SERVERPORT = 6000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Nothing from client yet");
this.myCommsThread = new Thread(new CommsThread());
this.myCommsThread.start();
}
class CommsThread implements Runnable {
public void run() {
// Socket s = null;
try {
ss = new ServerSocket(SERVERPORT );
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {
try {
Socket socket = ss.accept();
connectedDeviceCount++;
Thread lThread = new Thread(new ListeningThread(socket));
lThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class ListeningThread implements Runnable {
private Socket s = null;
public ListeningThread(Socket socket) {
// TODO Auto-generated constructor stub
this.s = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (!Thread.currentThread().isInterrupted()) {
Message m = new Message();
// m.what = QUESTION_ID;
try {
if (s == null)
s = ss.accept();
BufferedReader input = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String st = null;
st = input.readLine();
String[] temp = parseReadMessage(st);
mClientMsg = temp[1];
if(temp.length > 2) {
mClientExtraMsg = temp[2];
}
m.what = Integer.parseInt(temp[0]);
myUpdateHandler.sendMessage(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
The client code
public class ClassAidClient4Activity extends Activity {
//telnet localhost 5554
//redir add tcp:5000:6000
private Socket socket;
private String serverIpAddress = "192.168.1.102";
private static final int REDIRECTED_SERVERPORT = 5000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
mQuestionAdapter.add("UnknownHostException");
e1.printStackTrace();
} catch (IOException e1) {
mQuestionAdapter.add("IOException");
e1.printStackTrace();
}
}
}
© Stack Overflow or respective owner