Java Introductory notes 9_socket

Source: Internet
Author: User
1. Client/server Networking
Java through the socket to complete all of its network at the bottom of the communication, the socket is a communication channel, through which it can send data through a specific port and receive. The socket in Java can be divided into two main categories:
(1) Datagram Sockets: Data packet socket;
(2) Stream Sockets: streaming socket;
1.2 Datagram Socket
Datagram socket uses UDP for data communication, so it does not guarantee that the data will reach its destination, but because it does not require a dedicated network link, it requires a relatively small amount of resources.
Datagram sends data as packets, but it does not guarantee that the packets will arrive in a specific order, so the packets often need information that contains the serial number, and the receiver can decide whether all packages have been received based on the information of the serial number and reorganize the packages in the normal order.
Java supports datagram sockets through two classes of Datagramsocket and Datagrampacket. Datagramsocket implements the basic functionality of the datagram socket, while Datagrampacket provides some support for the package.
Several important methods of Datagramsocket:
(1) datagramsocket (): Random binding of a valid port;
(2) datagramsocket (int port): binding the specified port;
(3) Void send (Datagrampacket p): Send a datagram, because the destination of the address information is included in the datagram, so do not need to provide address information in this function;
(4) Synchronized void receive (Datagrampacket p): Receive packet, thread safety;
(5) synchronized void Close (): closing socket;
In Datagramsocket, ServerSocket and clientsocket are not distinguished, and if they are to be differentiated, the client is sent and the server is received.
There are several important ways to Datagrampacket:
(1) Datagrampacket (byte ibuf[], int ilength): for receiving datagrams;
(2) Datagrampacket (byte ibuf[], int ilength, inetaddressiaddr, int iport): datagram for sending;
(3) byte[] GetData ()
(4) int getlength ()
The following is an example of a complete datagram socket:
Receive-side, server-side code:
Import java.io.*;
Import java.net.*;
Class FortuneServer1 extends Thread
{
Datagramsocket ServerSocket;
Public FortuneServer1 ()
{
Super ("FortuneServer1");
Try
{
ServerSocket = new Datagramsocket (1114);
System.out.println ("Fortuneserver up and Running ...");
}

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.