Socket Android mobile client and PC Server LAN inline Test

Source: Internet
Author: User
Tags set socket

Socket Android mobile client and PC Server LAN inline Test

Socket Android mobile client and PC Server LAN inline test, I use PC server, Android tablet client, PC simulator client,

Some time ago, in order to deepen my understanding of Socket communication and knowledge, I imitated some elements of QQ and wrote some source code ideas from other cool people.

An Android chat software called IQ, which consists of Android clients, JAVA pc servers, and mysql databases.

I often use read/write IO streams, SCOKET, threads, etc. This blog mainly introduces you to the test environment, not to mention the source code implementation method, that is, how to use and write well.

I have read a lot of blogs and I am talking about it in general terms. This is not conducive to learning for beginners, so I will explain it in detail today,

If you are interested in studying Socket, you can join us. If you don't talk nonsense, read the blog.


First of all, we only need to know that IP addresses in a LAN can access each other, that is, within the same network segment. In this case, it may be hard to understand. I have some core code

Use the code to show you:

Public class IQserver {public IQserver () {ServerSocket Ssocket = null; try {Ssocket = new ServerSocket (4632); System. out. println ("Server is Loging ...... "+ new Date (); while (true) {// accept () is a blocking function. The so-called obstructive method // means that the method will wait for the customer's request after it is called, until a customer starts and requests to connect to the same port, // then accept () returns a SocketSocket socket = Ssocket corresponding to the customer. accept ();

The above code is the IQServer class code segment on the PC server, where the ServerSocket creates an object. Ssocket = new ServerSocket (4632); Set monitoring

Listen to the port and listen to the SOCKET from this port

Public class IQClient {private Context context; public Socket socket; public IQClient (Context context) {this. context = context;} // log on to the public boolean sendLoginInfo (Object obj) {boolean B = false; try {socket = new Socket (); try {// host: for the Server IP address port: for the server port number ms for the connection delay set socket. connect (new InetSocketAddress ("10.0.0.2", 4632), 3000);} catch (SocketTimeoutException e) {// return false when connection to the server times out;

The above code segment is the client IQClient code segment. We can see that my current test IP address is 10.0.0.2, port 4632, and the delay is 3 seconds. This test is


It is the IP address used by a single PC Android simulator to access the local PC. It is equivalent to 127.0.0.1. Next we will focus on this. We should test the server and customer service.

The client is controlled under the same network segment, for example:


Router ing: we all know that when our devices connect to the hot spots shared by the vro, an IP address is automatically assigned.

192.168.23.1, I connected the hotspot of the router with my mobile phone and assigned an IP Address: 192.168.23.0 ~ 255 the fourth field is 0 ~ Not 1 ~ Between 255

Number, because the IP address cannot conflict, and port 4632 should not conflict with any port on the server. Try to set ports as uncommon. view the specific Port

Cmd command: x:> netstat-ano

Local Server Settings: If there is no server, we can actually get a local area network by ourselves. Anyone who has played CS knows that, hey, it's usually better to take notes.

This will split up a hot spot. If you connect your PC to wifi, you can also share the hot spot. Then other players can play together as long as they connect to the hot spot. Let's talk about it below.

Share hotspot command lines in your notebook. If you have any tools, you can share them directly.

Notebook hotspot sharing CMD command:

X:> netsh wlan set hostednetwork mode = allow ssid = hotspot account key = key

Press enter. No; No. is required.

Next, we are typing:

X:> netsh wlan start hostednetwork

Do not add; No.. Press enter directly. If exceptions or failures occur

Check service management, and press & hold the field key (menu key) + R key to enter services. msc

Find the WLAN option and enable it. For other errors, find the WLAN firewall settings, complete the settings, and restart the PC.


The next step is to use the shared hotspot to conduct a LAN test, using Win7 PC as the case


For example, I used my win7 notebook to share a hotspot named Fuck. its IP address is 192.168.23.1.

The specific port viewing method is as follows (without a network ):

Press and hold the field key (menu key) + R key to Enter cmd in the run, execute ipconfig without adding; No., press Enter. If the red box is the hot IP address of the PC server


It does not affect the app, because it was tested by me and has not been shared with anyone. It is subject to your own record, just for demonstration.

ThenIn the client IQClient code segment, replace the IP address with 192.168.23.1 and it will be OK. You do not need to modify the port, and then execute to enable the server code class,

Code and result Diagram for enabling the server:

Package engineer. jsp. server. start;/*** @ author Engineer-Jsp * @ date 2014.08.18 * IQ startup service class */import engineer. jsp. server. control. IQserver; public class StartServerSocket {public static void main (String [] args) {new IQserver () ;}/ *** run iqtest. Run the current class first, if you need to update and modify the code, first stop the process and refresh the project. If you run the current startup class multiple times, the following exception will be thrown * java.net. bindException: Address already in use: JVM_Bind * because it is still executed in the PC thread, run the CMD command of the End Process: ① netstat-ano ② find the pid of the thread, CMD command: taskkill/pid xxxx/f: * Right-click the selected project on a single machine and refresh it. When running the current class, note that the ports of the server and client must be consistent */

The white flag is the process of binding the port after the server is Enabled:



Next, you need to install the client APP on the handheld device, connect the hotspot named Fuck, log on to the test account, and test the account from your mysql data.

Library, my test account table is as follows:



PC simulator friend list:




Android tablet client, PC simulator client, PC java Server, start testing:

The device is corrupted... Don't laugh... O



In the chat diagram, the small emotions in the middle account can receive messages from Engineer-Jsp, while the Engineer-Jsp cannot receive the small sentiment information because there is still a BUG that has not been solved, because

IsThe main purpose is to conduct LAN testing, so this is not important at present, and the impact is not great

, With database: http://download.csdn.net/detail/jspping/8067509


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.