Monday, February 24, 2014

android create socket connections always timeout


This is my manifest file

There is set up, network access.
activity code, the following code is executed when clicking the Connect button.
if (isConnecting) {
try {
if (socket! = null && clientThread! = null) {
socket.close ();
/ / ClientThread.interrupt ();
}
} Catch (IOException e) {
e.printStackTrace ();
}
connectBtn.setText ("on-line");
isConnecting = false;
} Else {
clientThread = new Thread (myRunnable); / / build a client thread
clientThread.start ();

connectBtn.setText ("offline");
isConnecting = true;
}
}

This is the code
Runnableprivate Runnable myRunnable = new Runnable () {
@ Override
public void run () {
Log.d ("tag", "myRunnable");
ObjectInputStream ois = null;
String sIP = "192.168.1.138";
int port = 54679;

try {
socket = new Socket (sIP, port);
socket.setSoTimeout (5000);
Message threadMsg = new Message ();
threadMsg.what = 1;

/ *
handleMsg = "myRunnable";
handler.sendMessage (threadMsg); * /

handleMsg = "connected \ n";
handler.sendMessage (threadMsg);
ois = new ObjectInputStream (socket.getInputStream ());
Log.d ("tag", "socket is started");
} Catch (Exception e1) {
Message threadMsg = new Message ();
threadMsg.what = 1;
handleMsg = "Connection Exception:" + e1.getMessage ();
handler.sendMessage (threadMsg);
isConnecting = false;
e1.printStackTrace ();
}
while (isConnecting) {
try {
message = (MessageBean) ois.readObject ();
} Catch (Exception e) {
e.printStackTrace ();
}
Message threadMsg = new Message ();
threadMsg.what = 1;
handleMsg = "Sender:" + message.getSender () + "message:"
+ Message.getMsg ();
handler.sendMessage (threadMsg);
}
}
};

I have checked more than N times, will not ah! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! Always prompt timed out ! !
Really do not know how to check down, help ah! ! ! !<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Or use the simulator, the simulator may need to switch to TCP port, high version of the Android phones do not need to run,
Then confirm 192.168.1.138 This is a pass.
Reply:
Direct test java code, see if you can connect successfully
Reply:
cited a floor birdsaction reply:
run using a mobile phone or use the simulator, the simulator may need to switch to TCP port, does not require a higher version of Android, < br />Then confirm 192.168.1.138 This is a pass.


This is a pass, I use eclipse, written in java server code. Another program will be able to connect, I would not write this! All of the code lines examined, feel the problem lies in socket = new Socket (sIP, port); This line of code, but I see other programs written in the same way, no problem.
Reply:
reference to the second floor a394268045 reply:
direct test java code, see if you can connect successfully


As the code server, and indeed pass. I used another program to test, and now feel the problem lies in socket = new Socket (sIP, port); This line code.
Reply:
Then use this to try: socket = new Socket (); socket.connect (inetAddresss, timeOut);
Reply:
cited a floor birdsaction reply:
run using a mobile phone or use the simulator, the simulator may need to switch to TCP port, does not require a higher version of Android, < br />Then confirm 192.168.1.138 This is a pass.

With real machine running
Reply:
Found a mistake is a mistake that port, one of the numbers wrong. . . . .
Thank you, ah

No comments:

Post a Comment