Java Network Programming (II)

Source: Internet
Author: User

1.Socket and ServerSocket class

(1) Sockets use TCP to provide a mechanism for communication between two computers. The client program creates a socket and tries to connect to the server's socket. When the connection is established, the server creates a socket object. The client and server can now communicate by writing and reading to the socket object. The Java.net.Socket class represents a socket, and the Java.net.ServerSocket class provides a mechanism for the server program to listen to the client and establish a connection with them. The following steps occur when a TCP connection is established between two computers using sockets:

    • The server instantiates a ServerSocket object that communicates through the port on the server.
    • The server calls the Accept () method of the ServerSocket class, and the method waits until the client connects to the given port on the server.
    • When the server is waiting, a client instantiates a socket object, specifying the server name and port number to request a connection.
    • The constructor of the socket class attempts to connect the client to the specified server and port number. If the communication is established, a socket object is created on the client to communicate with the server.
    • On the server side, the Accept () method returns a new socket reference to the server that is connected to the client's socket.

After the connection is established, the communication is made by using the I/O stream. Each socket has an output stream and an input stream. The client's output is streamed to the server-side input stream, and the client's input is streamed to the server-side output stream.

(2)

Method of Socket Class

The Java.net.Socket class represents a socket that both the client and the server use to communicate with each other. The client wants to get a socket object by instantiating it, and the server obtains a socket object through the Accept () method's return value.

The socket class has five constructor methods.

2.DatagramSocket class and Datagramsocket class

(1) In Java through two classes to implement the UDP protocol top-level datagram: Datagrampacket object is a data container, Datagramsocket is used to send and accept datagrampacket socket. With the UDP communication mechanism, when sending the message, the data is first packaged, and then the packaged packets are sent to the destination. When you receive a message, you first receive a datagram from someone else, and then you view the contents of the datagram.

(2) Datagrampacket class

To send or receive datagrams, you need to package the data with the Datagrampacket class, which is to create an object, called a packet, with the Datagrampacket class.

Important Construction methods:

Datagrampacket (byte[] buf,int length) constructs a packet object to receive a packet of length

Datagrampacket (byte[] buf,int length,inetaddress address,int port) constructs a packet that is used to send the length of a packet to the specified port number on the specified host

Datagrampacket (byte[] buf,int offset,int length)

Datagrampacket (byte[] buf,int offset,int length,inetaddress address,int Port)

Datagrampacket (byte[] buf,int offset,int length,socketaddress address)

Datagrampacket (byte[] buf,int length,socketaddress address)

Common methods:

InetAddress getaddress () returns the IP address of a machine that will be sent to or received from that host

Byte[] GetData () returns the data buffer

int GetLength () returns the data length of the packet that will be sent or received

SocketAddress getsocketaddress () gets the socketaddress (usually the IP address + port number) of the remote host to which this packet is sent or emitted by this datagram

void Setaddress (InetAddress iaddr) sets the IP address of the machine to which this datagram will be sent

void SetData (byte[] buf) sets the data buffer for this package

Datagramsocket class

The Datagramsocket class is the socket used to send and receive packets, to send packaged packets to a destination, or to receive packets from a destination

Important method of construction

Datagramsocket (int port) creates a datagram socket and binds it to the specified port on the local host

Common methods

void receive (Datagrampacket p) Receive datagram packets from this socket

void Send (Datagrampacket p) sends packets from this socket

(3) Code implementation

//package "Hello" into a packet, send to destination host "www.baidu.com", port number 2016bytebuff[] = "Hello". GetByte (); InetAddress destaddress= Inetaddress.getbyname ("www.baidu.com"); Datagrampacket Datapacket=NewDatagrampacket (buff,buff.length,destaddress,2016); Datagramsocket Sendsocket=NewDatagramsocket ();  Sendsocket.send (Datapacket); //receive packets that are sent to the native port number No. 2016byteBuff[] =New byte[8192]; Datagrampacket Receivepacket=NewDatagrampacket (buff,buff.length); Datagramsocket Receivesocket=NewDatagramsocket (2016);  Receivesocket.receive (Receivepacket); intLength =receivepacket.getlength (); String message=NewString (Receivepacket.getdata (), 0, length); SYSTEM.OUT.PRINTLN (message);

Java Network Programming (II)

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.