Wednesday, April 23, 2014

android socket communication problem


posts by bx276626237 on 2014-04-22 11:00:22 Edit
Will android whether physical machine and the computer desktop socket communications?
Network cable is plugged into the computer. android physical machine using WiFi. Computer and android is not the same physical machine linked a route.

Server-side code:
public class SimpleServer {
public static void main (String [] args) throws IOException {
/ / Create a ServerSocket, Socket to listen for client connection requests
ServerSocket ss = new ServerSocket (4242); / / ①
/ / Use cycle continuously receiving requests from the client
while (true) {
/ / Whenever Socket client receives the request, the server also generates a corresponding Socket
Socket s = ss.accept ();
OutputStream os = s.getOutputStream ();
os.write ("Hello, you receive the server's New Year greeting \ n!" getBytes ("utf-8").);
/ / Close the output stream, close Socket
os.close ();
s.close ();
}
}
}

android-side code:
public class SimpleClient extends Activity {
private EditText show;

@ Override
protected void onCreate (Bundle savedInstanceState) {
/ / TODO Auto-generated method stub
super.onCreate (savedInstanceState);
setContentView (R.layout.simple_client_main);
show = (EditText) findViewById (R.id.show);
new Thread () {

@ Override
public void run () {
try {
/ / Create a connection to a remote server Socket
Socket socket = new Socket ("61.129.36.26", 4242);
/ / The corresponding input stream Socket packed into BufferedReader
BufferedReader br = new BufferedReader (
new InputStreamReader (socket.getInputStream ()));
/ / For common IO operations
String line = br.readLine ();
show.setText ("data from the server:" + line);
/ / Close the input stream, socket
br.close ();
socket.close ();
. Toast.makeText (SimpleClient.this, "link computer", Toast.LENGTH_SHORT) show ();
} Catch (Exception e) {
e.printStackTrace ();
}
}

.} Start ();
}
}



After the PC client and server running android have no reaction. . . . . . .
android client has added network permissions:
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
If the connection is not a problem in the same route, I think you have to set up port mapping in router in it
Reply:
These two routes in the computer can pass it to each other PING, PING not want to pass it relates to the socket holes, and can PING, nothing more than your cell phone privileges wrong, or phone or computer is certain security software firewall is blocking the category.
Reply:
Not the same route, you should now this situation is not acceptable,
Reply:
The reason is not a LAN
Reply:
First, determine whether the same network, can ping each other

No comments:

Post a Comment