Simple usage of "Android" socket

Source: Internet
Author: User

Original address: http://www.cnblogs.com/harrisonpc/archive/2011/03/31/2001565.html

Sockets are also often referred to as "socket", used to describe IP addresses and ports, nonsense, it is the network communication process in the abstract representation of the endpoint. It is worth mentioning that Java in package java.net provides two classes of sockets and ServerSocket, respectively, to represent the two-way connection of the client and the server side. This is a very good class of two packages, easy to use!

The following will be the first to create a Socketserver class as the service side, the server has implemented a multi-threaded mechanism, can listen to a number of customer requests at a specific port, and once a customer request, servers will always create a service purebred to serve the new customers, and continue to listen. The program accept () is a blocking function, the so-called blocking method is said to be called after the method will wait for the client's request, until a client starts and requests to connect to the same port, and then accept () returns a socket corresponding to the customer. At this point, the client and the service party have established a socket for communication, followed by each socket to open their respective input, output stream.

    • Socketserver class, server implementation:
1 package HA. Socket;
2
3 Import java.io.*;
4 Import java.net.*;
5
6 public class Socketserver {
7
8 ServerSocket Sever;
9
public socketserver (int port) {
try{
Sever = new ServerSocket (port);
}catch (IOException e) {
E.printstacktrace ();
15}
16}
17
public void Beginlisten () {
while (true) {
try{
Final socket socket = sever.accept ();
22
The new Thread (new Runnable () {
public void Run () {
BufferedReader in;
try{
in = new BufferedReader (New InputStreamReader (Socket.getinputstream (), "UTF-8"));
PrintWriter out = new PrintWriter (Socket.getoutputstream ());
while (!socket.isclosed ()) {
String str;
-str = In.readline ();
Out.println ("hello!world!!" + str);
Out.flush ();
if (str = = NULL | | str.equals ("END"))
break;
System.out.println (str);
37}
Socket.close ();
}catch (IOException e) {
E.printstacktrace ();
41}
42}
). Start ();
}catch (IOException e) {
E.printstacktrace ();
46}
47}
48}
49}
    • Socketclient class, client implementation:
1 package HA. Socket;
2
3 Import java.io.*;
4 Import java.net.*;
5
6 public class Socketclient {
7 Static Socket client;
8
9 Public socketclient (String site, int port) {
Ten try{
One client = new Socket (site,port);
System.out.println ("Client is created! Site: "+site+" Port: "+port";
}catch (Unknownhostexception e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
17}
18}
19
public string sendmsg (String msg) {
try{
BufferedReader in = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
PrintWriter out = new PrintWriter (Client.getoutputstream ());
OUT.PRINTLN (msg);
Out.flush ();
In.readline return ();
}catch (IOException e) {
E.printstacktrace ();
29}
Return "";
31}
public void Closesocket () {
try{
Client.close ();
}catch (IOException e) {
E.printstacktrace ();
37}
38}
All public static void main (string[] args) throws exception{
40
41}
42
43}

The next step is to test the socket communication!

Run the Testsocketserver class first, open the server, and listen at Port 12345!

1 package HA. Socket;
2
3 public class Testsocketserver {
4
5 public static void Main (string[] Argvs) {
6 Socketserver Server = new Socketserver (12345);
7 Server.beginlisten ();
8}
7 3

Run the Testsocketclient class again:

1 package HA. Socket;
2
3 public class Testsocketclient {
4
5 public static void main (string[] args) {
6
7 Socketclient client = new Socketclient ("127.0.0.1", 12345);
8 System.out.println (client.sendmsg ("Nimei1"));
9 Client.closesocket ();
10
One socketclient client1 = new Socketclient ("127.0.0.1", 12345);
System.out.println (client1.sendmsg ("nimei1111"));
Client1.closesocket ();
14
Socketclient client11 = new Socketclient ("127.0.0.1", 12345);
System.out.println (client11.sendmsg ("nimei11111111"));
Client11.closesocket ();
18
Socketclient client111 = new Socketclient ("127.0.0.1", 12345);
System.out.println (client111.sendmsg ("nimei11111111111111111"));
Client111.closesocket ();
22
23}
24}

The output results are as follows:

Service side:

Client is created! site:127.0.0.1 port:12345
hello!world!! Nimei1
Client is created! site:127.0.0.1 port:12345
hello!world!! nimei1111
Client is created! site:127.0.0.1 port:12345
hello!world!! nimei11111111
Client is created! site:127.0.0.1 port:12345
hello!world!! nimei11111111111111111

Client:

Nimei1
nimei1111
nimei11111111
nimei11111111111111111

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.