Java Network Programming Note 4

Source: Internet
Author: User

Socket and ServerSocket, network programming can be divided into creating sockets, opening the input stream and output stream connecting the socket, programming the socket, closing the socket.

Create socket


The working process of a fully functional socket consists of the following four steps:

1. Create socket.

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

3, according to a certain protocol to the socket read/write operation.

4. Close socket


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

Class sockets are constructed in the following way:

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 (Socketimp impl)

Socket (String host,int port,inetaddress localaddr,int localport)

Socket (inetaddress address,int port,inetaddress localaddr,int localport)

The address of the parameter indicates the IP addresses, host indicates the hostname, port indicates the port number, stream is used to indicate whether the socket is a stream or datagram, LocalPort represents the port number of the local host, LOCALADDR is local and its address, Impl is the parent class of the socket.

The construction method in ServerSocket is as follows:

ServerSocket (int port)

ServerSocket (int port,int backlog)

ServerSocket (int port,int backlog,inetaddress bindaddr)

The parameter bindaddr represents the native address.


Socket and ServerSocket class libraries are located in the java.net package, ServerSocket for the server side, the socket is used when establishing a network connection. When the connection succeeds, a socket instance is generated at both ends, and through the socket instance, the required session can be completed.

The usual method of socket is 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, which is used to get the network connection input, returns an instance of a InputStream object. Getoutputstream method, gets the OutputStream instance.


See Client and server communication:

Service side:

<span style= "FONT-SIZE:18PX;" >public class Server {public static void main (string[] args) {ServerSocket Ss;try {ss = new ServerSocket (30000), while (t Rue) {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 ();}}} </span>

Client:

<span style= "FONT-SIZE:18PX;" >public class Client {public static void main (string[] args) {try {socket socket=new socket ("192.168.120.112", 30000); O Utputstream Outputstream=socket.getoutputstream (); InputStream Inputstream=socket.getinputstream (); PrintStream printstream=new PrintStream (outputstream);p rintstream.print ("Client--->"); Socket.close ();} catch (Unknownhostexception e) {//Todo auto-generated catch Blocke.printstacktrace ()} catch (IOException e) {//Todo Aut O-generated catch Blocke.printstacktrace ();}}} </span>



Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/42651911 Emotional Control _  



Java Network Programming Note 4

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.