Home > other >  Computer Java write simple receiver to receive less than Android's machine client sends data
Computer Java write simple receiver to receive less than Android's machine client sends data

Time:10-11

I written in Java socket with TCP connection server and client, and then use computer cheetahs wifi opened a hotspot, mobile phone connection, computers can ping the mobile phone, but the mobile client is unable to connect to the server computer, why?
PC code:
The static ServerSocket ServerSocket=null;

The static a DataInputStream data_input=null;
The static DataOutputStream data_output=null;

Public static void main (String [] args) throws IOException {
//TODO Auto - generated method stub
Try {


Serversocket=new serversocket (6566);
System. The out. Println (" listening port 6566 ");
While (true) {

The Socket client_socket=serversocket. The accept ();
Try {

Data_input=new a DataInputStream (client_socket. GetInputStream ());
String MSG=data_input. ReadUTF ();
System. The out. Println (MSG);

} the catch (Exception e) {
e.printStackTrace();
}
The finally {
Try {
Data_input. Close ();
Client_socket. Close ();
Data_output. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
} the catch (Exception e) {
e.printStackTrace();
}

}

Mobile client only a BUtton (UI) :
Public class MainActivity extends the Activity {
Public Button sendbtn=null;
Public Socket client_socket=null;

The public a DataInputStream data_input=null;
Public DataOutputStream data_output=null;



@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Sendbtn=(Button) the findViewById (R.i d.s endbtn);
Toast. MakeText (getApplicationContext (), "started", Toast. LENGTH_SHORT), show ();
Sendbtn. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
Try {
Client_socket=new Socket (" 192.168.191.1 ", 6566);// here should write computer terminal IP right?

Data_output=new DataOutputStream (client_socket. GetOutputStream ());
Data_input=new a DataInputStream (client_socket. GetInputStream ());
Toast. MakeText (getApplicationContext (), "connected", Toast. LENGTH_SHORT), show ();// test didn't sell them, may be no connection, I don't know why?

} the catch (Exception e) {
e.printStackTrace(); }
String text="android";
Try {
If (data_output!=null)
{
Data_output. WriteUTF (text);
Data_output. Close ();
Client_socket. Close ();

Toast. MakeText (getApplicationContext (), "send", Toast. LENGTH_SHORT), show ();
}

} the catch (Exception e)
{
e.printStackTrace();
}

}
});
}
}
  • Related