How to realize socket communication between Android client and PC server

Source: Internet
Author: User
Tags constructor socket port number

This paper introduces the implementation of the Android Terminal scanning AP information and sending it to the server side continuously. First, a network virtual link is formed between the Android terminal and the PC based on the TCP protocol. Use ServerSocket to create the TCP server side, and then use the socket's constructor on the Android client to connect to the server. Where the Android terminal is connected to the same LAN as the PC via WiFi connection.

1. PC Server Enable ServerSocket

Two communication entities before establishing a virtual link, one party needs to be prepared to accept the connection request from other communication entities.

Use the ServerSocket object to listen for socket connections from the client.

Create ServerSocket Object  
       
       
serversocket ss = new ServerSocket (30000);  
Listen for requests from the client while  
(true) {  
    Socket s = ss.accept ();  
    ..  
}

If there is no connection, it is always in a waiting state. When a connection request is received, the message is fetched to the input stream and saved to the file.

Receive client message  
       
       
       
BufferedReader in = new BufferedReader (New InputStreamReader (Client.getinputstream ()));    
String str;  
       
BufferedWriter bw = new BufferedWriter (New FileWriter ("D:/apinfo" + (i++) + ". txt"));  
while (str = In.readline ())!= null) {  
    System.out.println (str);  
    Bw.write (str);  
    Bw.newline ();     
}

2. The Android terminal uses socket communication

The client uses the constructor of the socket to connect to the server, specifying the server IP and port number.

Socket s = new socket ("192.168.1.100", 30000);

This way the server-side accept () method is responded to and executed downward, and the server-side and the client form a pair of connected sockets. Again, there is no server and client points, all through the input and output stream to communicate.

Detailed steps:

Handler and TimerTask are used to scan AP information and send it to server side. TIMERTASK specifies the tasks to be performed to reach the specified time.

TimerTask task = new TimerTask () {public  
       
    void run () {message #  
        = new Message ();  
        Message.what = 1;  
        Handler.sendmessage (message);  
    }     
       
;

Handler Pass message content:

Handler Handler = new Handler () {public  
       
    void Handlemessage (msg) {  
        switch (msg.what) {case  
        1:                  
            / /execution Timer time after the task break passed by handler  
            ;  
        Super.handlemessage (msg);  
    }  
       
;

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Next, scan the AP information and send it to the server side, then save the results.

Wifimanager wifimanager= (Wifimanager) Getsystemservice (wifi_service);  
Wifimanager.startscan ();  
Mwifilist = Wifimanager.getscanresults ();

The Wifimanager description shows that it can be used to handle the configuration of the network, the current connected network and the AP information scanning, and so on.

To send a message to the server:

Socket = new Socket ("192.168.1.211", 30000);  
Send message to server  
printwriter out = new PrintWriter (New BufferedWriter (Socket.getoutputstream () ), true);          
OUT.PRINTLN (message);

Where message is the access to the AP information, the test received information in the format:

Ssid:icis_lab, BSSID:1C:AF:F7:9A:65:E4, capabilities: [wpa-psk-tkip+ccmp], Level: -80, frequency:2437

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.