Dark Horse programmer-java Network Programming

Source: Internet
Author: User

Java Network Programming (so-called socket programming) has sockets between the two endpoints of communication, and data is transmitted through IO between the two sockets, network Models: 1. OSI reference model (application layer> presentation layer> Session Layer> transport layer> network layer> data link layer> physical layer ), 2.TCP/ IP Reference Model (Application Layer-> transport layer-> Internet layer-> host-to-network layer)
Network communication elements: 1. IP address 2. port Number 3. the transmission protocol, which is mainly divided into two categories (UDP and TCP). They are all Internet transmission protocols, the difference mainly lies in data security and integrity:
UDP is a connectionless protocol. Compared with TCP, UDP is an insecure data transmission protocol. The verification information in UDP packets is much less than that in TCP packets. Therefore, the verification of data security and integrity is not as secure as the TCP protocol. for the transmission of large data volumes over TCP, each UDP packet is limited to 64 kB because it is an unreliable protocol, therefore, no connection is required, so the speed is faster.
TCP is a connection-oriented protocol, so it is a secure data transmission protocol. When a data transmission error is found during verification, data will be re-transmitted immediately, to ensure data correctness and integrity, it is a secure data transmission protocol. In addition, TCP can transmit large data volumes. Because it is a reliable protocol, a connection must be established, therefore, the efficiency is lower than that of UDP.

Simple Network Transmission over UDP:
Sending code

Java code
Import java.net. DatagramPacket;
Import java.net. DatagramSocket;
Import java.net. InetAddress;
 
Public class UdpSend {
Public static void main (String [] args) throws Exception {
// 1. Create a udp Service and use the DatagramScoket object
DatagramSocket ds = new DatagramSocket ();
 
// 2. Determine the data and encapsulate it into a data packet, including DatagramPacket (byte [] buf, int length, InetAddress
// Address, int port)
Byte [] buf = "data sent in udp form". getBytes ();
DatagramPacket dp = new DatagramPacket (buf, buf. length, InetAddress
. GetByName ("127.0.0.1"), 10000 );
 
// 3. Use the socket service to send existing data packets.
Ds. send (dp );
 
// 4. Close the resource
Ds. close ();
}
}
 
Acceptor code
 
Java code
Import java.net. DatagramPacket;
Import java.net. DatagramSocket;
 
Public class UdpReceive {
Public static void main (String [] args) throws Exception {
// 1. Create a udp socket and a port
DatagramSocket ds = new DatagramSocket (10000 );
 
// 2. Define data packets for data storage
Byte [] buf = new byte [1, 1024];
DatagramPacket dp = new DatagramPacket (buf, buf. length );
 
// 3. Store the received data into the data packet through the recieve method of the service
Ds. receive (dp); // blocking method
 
// 4. Obtain the data in the data packet
String ip = dp. getAddress (). getHostAddress ();
String data = new String (dp. getData (), 0, dp. getLength ());
Int port = dp. getPort ();
System. out. println (ip + ":" + data + ":" + port );

// 5. Close the resource
Ds. close ();
}
}
 
Simple Network Transmission over TCP:
Server code

Java code
Import java. io. InputStream;
Import java. io. OutputStream;
Import java.net. ServerSocket;
Import java.net. Socket;
 
Public class TcpServer {
Public static void main (String [] args) throws Exception {
// Create a server socket and listen to a port
ServerSocket ss = new ServerSocket (9999 );

// Obtain the client object connected by using the accept Method
Socket s = ss. accept ();
String ip = s. getInetAddress (). getHostAddress ();
System. out. println (ip + "... connected ");

// Obtain the data sent by the client. Use the read stream of the client object to read the data.
InputStream in = s. getInputStream ();

Byte [] buf = new byte [1, 1024];
Int len = in. read (buf );

System. out. println (new String (buf, 0, len ));

S. close ();
Ss. close ();
}
}

Client code

Java code
Import java. io. InputStream;
Import java. io. OutputStream;
Import java.net. ServerSocket;
Import java.net. Socket;
 
Public class TcpClient {
Public static void main (String [] args) throws Exception {
// Create a client socket service and specify the target host and Port
Socket s = new Socket ("127.0.0.1", 9999 );

// To send data, the output stream in the socket stream should be obtained
OutputStream out = s. getOutputStream ();
Out. write ("data sent in tcp format". getBytes ());
S. close ();
}
}

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.