Java Socket Programming

Source: Internet
Author: User

For Java socket programming, there are two concepts, one is ServerSocket, the other is the socket, the server client can communicate through the socket connection. First, ServerSocket on the server to listen to a port, when it is found that the client has a socket attempt to connect ServerSocket, it will accept the request, while establishing a socket on the service side to communicate with, so that the client and the server has a socket.

The communication between the sockets is actually the client's output stream to the socket, and the server can read the corresponding content from the output stream. and the socket and socket are two-way connectivity, so the client can also go to the corresponding socket output stream to write something, and then the service side of the corresponding socket input stream can read the corresponding content. Not much to say, on the example:

Service side:

public class Server {

public static void Main (String args[]) throws IOException {
int port = 8090;
Define a ServerSocket listener on port 8090
ServerSocket Server = new ServerSocket (port);
The server tries to receive connection requests from other sockets, the server's accept method is blocked, and the service does not receive the information sent by the client, it will listen until the output stream is closed
Socket socket = server.accept ();
Gets the inputstream of the socket and reads the information sent from the client.
Reader reader = new InputStreamReader (Socket.getinputstream ());
Char chars[] = new CHAR[64];
int Len;
StringBuilder sb = new StringBuilder ();
while ((Len=reader.read (chars))! =-1) {
Sb.append (New String (chars, 0, Len));
}
System.out.println ("From client:" + SB);
Reader.close ();
Socket.close ();
Server.close ();
}
}

Client:

public class Client {
public static void Main (String args[]) throws Exception {
String host = "127.0.0.1"; Service-side IP address to connect to
int port = 8090; The listener port for the server to which you want to connect
Establish a connection to the server
Socket client = new socket (host, port);
Once the connection is established, the data can be written to the server.
Writer writer = new OutputStreamWriter (Client.getoutputstream ());
Writer.write ("I am Chinese, I love my Motherland");
Writer.flush ();//Remember to flush after you finish writing
Writer.close ();
Client.close ();
}
}

After the service starts, the client runs the Server console window and prints the message sent by the client:

From client: I am Chinese!

Java Socket Programming

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.