Java Network Programming Note 4

Source: Internet
Author: User

Java Network Programming Note 4

Socket and ServerSocket. network programming can be divided into creating a Socket, opening the input stream and output stream connecting to the Socket, programming the Socket, and disabling the Socket.

Create Socket

 

The working process of a fully functional Socket includes the following four steps:

1. Create a Socket.

2. Open the input/output stream connected to the Socket.

3. Perform read/write operations on the Socket according to certain protocols.

4. Disable Socket

 

In the java.net package, the Socket and ServerSocket classes are defined. The Socket class represents the two ends of the client or server Socket connection.

The constructor in class Socket is as follows:

Socket (inetAddress address, int port)

Socket (inetAddress address, int port, boolean stream)

Socket (String host, int port)

Socket (String host, int port, boolean stream)

Socket (sockeibd impl)

Socket (String host, int port, inetAddress localAddr, int localport)

Socket (inetAddress address, int port, inetAddress localAddr, int localport)

The parameter address indicates the IP address, host indicates the host name, port indicates the port number, stream indicates whether the Socket is a stream or a datagram, localPort indicates the port number of the local host, and localAddr indicates the local host and its address, impl is the parent class of Socket.

The ServerSocket construction method is as follows:

ServerSocket (int port)

ServerSocket (int port, int backlog)

ServerSocket (int port, int backlog, InetAddress bindAddr)

The bindAddr parameter indicates the local address.

 

Both the Socket and ServerSocket class libraries are located in the java.net package. ServerSocket is used on the server side and used when a network connection is established. After the connection is successful, a Socket instance is generated at both ends. Through this Socket instance, you can complete the required session.

Common Socket methods include the Accept method, which is mainly used to generate "blocking" until a connection is received and a client's Socket object instance is returned. The getInputStream method is used to obtain network connection input and return an instance of an InputStream object. GetOutputStream method to obtain the OutputStream instance.

 

Refer to client-server communication:

Server:

 

public class Server {public static void main(String[] args) {ServerSocket ss;try {ss = new ServerSocket(30000);while(true){Socket socket=ss.accept();OutputStream outputStream=socket.getOutputStream();InputStream inputStream=socket.getInputStream();BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));String request=br.readLine();System.out.println(request);socket.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

Client:

 

 

public class Client {public static void main(String[] args) {try {Socket socket=new Socket(192.168.120.112,30000);OutputStream outputStream=socket.getOutputStream();InputStream inputStream=socket.getInputStream();PrintStream printStream=new PrintStream(outputStream);printStream.print(Client--->);socket.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


 

 

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.