Java Network entry 2 (j2se entry 19)

Source: Internet
Author: User

Create a tcp client

To create a tcp client program:
1). Create a socket
2). Obtain the I/O Stream
3). Read and Write the I/O Stream
4). Disable the I/O Stream.
5). Disable socket

Socket connection = new Socket(127.0.0.1, 7777); 
ObjectInputStream input=new ObjectInputStream(connection.getInputStream());
ObjectOutputStream utput=new ObjectOutputStream(connection.getOutputStream());


Processes input and output streams;
Disable stream and socket.

UDP socket
This information transmission method is equivalent to fax, information packaging, and preparation of paper on the receiving end.
Features:
1) UDP-based connectionless protocol
2) reliable message transmission is not guaranteed
3) they are supported by the initramsocket and initrampacket classes in Java technology.

Datagramsocket (Postman): The socket concept of the corresponding datagram. You do not need to create two sockets. You cannot use an input/output stream.
Letter rampacket): Data packet, which is the unit of data transmission in UDP. The data is stored in a byte array, including the destination address and port and transmitted information (So point-to-point connections are not required ).
Categories of datagrampacket:
For receiving: datagrampacket (byte [] Buf, int length)
Datagrampacket (byte [] Buf, int offset, int length)
Used for sending: datagrampacket (byte [] Buf, int length, inetaddress address, int port)
Datagrampacket (byte [] Buf, int offset, int length, inetaddress address, int port)
Note:Inetaddress is used to encapsulate IP addresses.
No constructor.
Inetaddress. getbyaddress (byte [] ADDR): inetaddress
Inetaddress. getbyname (string host): inetaddress

Create a UDP sender

To create a UDP sender program:

1) Create an ingress rampacket that contains the sent data and the IP address and port number of the receiver.
2) create an ingress ramsocket that contains the sender's IP address and port number.
3). Send data
4). Disable datagramsocket

byte[] buf = new byte[1024];
DatagramSocket datagramSocket = new DatagramSocket(13);// set port
DatagramPackage intputPackage = new DatagramPackage(buf,buf.length);
datagramSocket.receive(inputPackage);
DatagramPackage outputPackage = new DatagramPackage(buf,buf.length,inetAddress,port);
datagramSocket.send(outputPackage);

No stream is created, so you do not need to disconnect it.

Create a UDP Receiver

To create a UDP receiver program:
1) Create an ingress rampacket to store the data sent by the sender and the IP address and port number of the sender.
2) create a mongoramsocket, which specifies the IP address and port number of the receiver.
3). receive data
4). Disable datagramsocket

Byte [] Buf = new byte [1, 1024];
Datagramsocket = new datagramsocket (); // No port is set because the port in the package is
Extends rampackage outputpackage = new extends rampackage (
Buf, Buf. length, serveraddress, SERVERPORT );
Required rampackage inputpackage = new required rampackage (BUF, Buf. Length );
Datagramsocket. Receive (inputpackage );

Related Article

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.