Socket Android Phone client and PC server LAN inline test, the author uses the PC servers, Android tablet client, PC Simulator client,
A while ago to deepen The memory of the socket communication and the depth of knowledge, I imitated some elements of QQ, but also borrowed some other cattle source ideas, wrote
one called IQ. android Chat software, the software is roughly composed of Android client, JAVA PC server, MySQL database, the client and server are
It's more used. read/write IO stream, scoket, thread, etc., this blog mainly to introduce the test environment, do not talk about the source code implementation, that is, how to use write good
programs to apply and test, I read a lot of blogs, in this respect is very general, so it is not conducive to the learning of beginners, so I will be very detailed today to say again,
be interested in learning the socket can be followed together, do not talk nonsense, read the blog bar
First of all we have to know that in a LAN IP is able to access each other, that is, the same network segment, it may be difficult to understand, I have some core code
In combination with the code demo, let's see:
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 blocking method//means that the method is called and waits for a client's request until a client initiates and requests to connect to the same port,//and then accept () Returns a socketsocket socket = ssocket.accept () corresponding to the customer;
The above code is the PC server named Iqserver Class code snippet, ServerSocket create object
ssocket=new ServerSocket (4632);
Listen to the port and listen for the SOCKET from this port
public class Iqclient {private Context context;public Socket socket;public iqclient (context context) {this.context= Context } //Login public boolean sendlogininfo (Object obj) {Boolean b=false; try {socket=new socket (); Try{//host: The IP address for the server port: 3000ms for the port number of the server for the connection delay setting Socket.connect ("10.0.0.2", 4632), 3000);} catch (Sockettimeoutexception e) {//connection server timeout return false;
The above code snippet is the client iqclient code snippet, we can see that my current test IP is 10.0.0.2, port 4632, 3 seconds delay, this test is
is the single PC Android emulator to access the local PC's IP, which equals 127.0.0.1, the following starts to say the focus, we test should be the server and customer service
End control in the same network segment, such as:
Router mapping: We all know that when our device connects to the hotspot shared by the router, it will be automatically assigned an IP, for example the router is
192.168.23.1, I used a mobile phone connected to the router hotspot, assigned to a ip:192.168.23.0~255 fourth field is not 1 between the 0~255
Number, because the IP address can not conflict, there is port 4632 do not conflict with any port on the server, port as far as possible to set not for the common, specific port view
cmd command:x:> Netstat-ano
Local server self-setting: If there is no server, in fact, we can get a local area network, played the CS know, hey, usually better performance notes
This will be a hot spot, pc words plugged in WiFi can also share hot spots, and then other players as long as the connection to this hotspot can play together at the same time, the following said
Notebook sharing hotspot command line, there are tools can be directly shared, without knocking command, save things
Notebook hotspot share cmd command:
X:> Netsh wlan set hostednetwork mode=allow ssid= hotspot account key= key
After hitting the carriage return, no need to add;
Then on the knock:
X:> Netsh wlan start hostednetwork
No, no, no, direct enter, if there are some exceptions, or failure prompts
Please check the service management, press and hold the field key (menu key) +r key in the operation input services.msc
Find WLAN this one, turn on, other errors, please find the WLAN firewall settings, set up, restart the PC
The next step is to use the shared hotspot to conduct LAN testing to Win7 PC as a case
For example, I now use my win7 notebook to share a hot spot called fuck, its IP is: 192.168.23.1
The specific port view method is as follows (without net):
Press and hold the field key (menu key) +r key in the run input cmd, do not add ipconfig, enter, such as the red box is the PC server hotspot IP
Erase is not affected, because this is my test, and did not share the hotspot, with your own prevail, just to show you see
Then in the above client iqclient code snippet, the IP is replaced with 192.168.23.1 OK, the port does not modify, and then execute the server code class,
Server open code and result diagram:
Package engineer.jsp.server.start;/** * @author engineer-jsp * @date 2014.08.18 * IQ start service class */import Engineer.jsp.server.Co Ntrol. Iqserver;public class Startserversocket {public static void main (string[] args) {new Iqserver ();}} /** * Perform IQ tests, first execute the current class, if you need to update and modify the code, first to end the process, refresh the project, run the current startup class multiple times will throw the following exception *java.net.bindexception:address already in use: Jvm_bind * Because it is also executed in the PC thread, to end the cmd command of the process: ①netstat-ano② find the thread in the PID, cmd command: Taskkill /pid xxxx/f can * then the single-machine selected item Right-click Refresh, run the current class, You also need to be aware that the server and client ports are consistent */
The white flag section is the process by which the server binds the port after it is opened:
The next thing you need to do is to install the written client app on a handheld device, and then connect to a hotspot named Fuck, log in to your test account, and test your MySQL data from your account.
Library, my test account list is as follows:
PC simulator Buddy list:
Android tablet client, PC emulator client, PC Java server, start testing:
Equipment comparison Bad ... Don't laugh, guys. O (∩_∩) O, a long time ago bought the book, Tablet is also
Chat diagram, in the account small sentiment can receive from engineer-jsp message, and engineer-jsp not get the information of small sentiment, is because there is still a bug not solved, because
to be The main purpose is to conduct LAN testing, so this is not important at present, the impact is not very big
Local area network testing to this end, there are questions can leave a message, we communicate and discuss together, see the first time reply, because usually more busy
The following talk about the application of the network domain, we all know that each time the IP is not fixed, which requires the domain name and IP binding, and then through the DNS to resolve the domain name to get IP, or with the server
Fixed static IP is also possible, that will involve access rights and security issues, the general database has a similar to the red and white list such restrictions, so that we can not access account information, that is, the accounts
households without method Login, it is necessary to the database on the line IP White list settings, the permissions of the host set to the% number can be resolved, and then on the server, with the JVM to start our service, with
Java Life order to start, a lot of blogs have similar explanations, we can check more information, understand
I do not know that I speak in detail, anyway, do not understand the proposed, we discuss together
Source, attached database: http://download.csdn.net/detail/jspping/8067509
Socket Android Phone client and PC server-side LAN inline test