Dark Horse programmer _ Network Programming

Source: Internet
Author: User
Network Programming implementation

There are two ways to implement network programming: TCP and UDP.

Q: What is the difference between TCP and UDP:

A:

1) TCP protocol: it is a reliable connection. A connection is established before the datagram is sent. The three-way handshake principle can be used to ensure that the data can be completely transmitted to the destination, so the transmission speed is also slow.

2) UDP protocol: it is an unreliable connection. No connection is established before data packets are sent, and data reporting does not reach the destination.

(1) UDP:
A. encapsulate data and source and target data packets without establishing a connection
B. The size of each data packet is limited to 64 KB.
C. Because there is no connection, It is an unreliable Protocol
D. fast connection is not required
(2) TCP:
A. Establish a connection to form a channel for data transmission
B. Transfer large amounts of data in the connection
C. Using a three-way handshake to complete a connection is a reliable protocol.
D. You must establish a connection, which is less efficient.

TCP Program

Implementation of TCP programs in Java:

|-Use the serversocket class to represent the server, public class serversocket extends object

.

|-Use the socket class to represent the client and public class socket extends object.

 

2. UDP Transmission

(1) Establish the sender rampacket P and receiver receive (receiver rampacket P)
(2) mongorampacket, which is used to deliver the service without a connection package. constructors include mongorampacket (byte [] Buf, int length, inetaddress address, int port) and so on.
(3) communication implementation (sender ):
A. Create a udpsocket service, for example, datagramsocket DS = new datagramsocket () object.
B. Provide data and encapsulate the data into data packets. Convert the data into byte arrays and then use mongorampacket for encapsulation. For example, byte [] Buf = "UDP is sending ". getbyte (); datagrampacket dp = new datagrampacket (BUF, Buf. length, inetaddress. getbyname ("192.168.0.100"), 10000)
C. Send data packets through the sending function of the socket service, DS. Send (DP );
D. Close resources, such as DS. Close ();
(4) communication implementation (receiver ):
A. Create a udpsocket service, such as datagramsocket DS = new datagramsocket (10000 );
B. Create a data packet. Because the received data is a byte, more functions of the data packet object are used to extract the information of the byte data, for example, byte [] Buf = new byte [1024]; datagrampacket dp = new datagrampacket (BUF, Buf. length );
C. Obtain the received data to mongorampacket through the Receiving Method of the socket service, DS. Receive (DP );
D. Use the mongorampacket function to interpret the data stream and output it to the desired location, for example, string IP = DP. getaddress (). gethostaddress (); string data = new string (DP. getdata (), 0, DP. getlength ());
E. Close resources, DS. Close ();

3. TCP transmission

(1) Establish a client using socket and seversocket to establish a server
(2) After a connection is established, data is transmitted through the IO stream in the socket
(3) Disable socket
(4) communication implementation (client ):
A. Establish a socket service. TCP is connection-oriented. When establishing a socket service, you must specify the host and port to be connected to establish a connection. For example, socket SOC = new socket ("192.168.0.100 ", 10003 );
B. Create an IO Stream object for obtaining or writing the socket input/output stream, for example, outputstream out = S. getoutputstream; inputstream in = new inputstream ();
C. Write Data to the output stream, and the data can be output from the socket output stream, out. write ("tcpmsg is sending ". getbyte (); Use the input stream to read the data of the socket input stream, inputstream in = S. getinputstream (); byte [] Buf = new byte [1024]; int Len = in. read (BUF); string STR = new string (BUF, 0, Len );
D. Close the resource. Soc. Close ();
(5) communication implementation (server ):
A. Create the seversocket service and listen to a port. seversocket Ss = newseversocket (10003 );
B. Obtain the connected client object through the accept method, socket S = ss. Accept ();
C. If the client sends data, the server uses the client object obtained in the previous step to read the data. inputstream in = S. getinputstream (); byte [] Buf = new byte [1024]; int Len = in. read (BUF); string STR = new string (BUF, 0, Len );
D. Close the client and release resources. S. Close ()
E. Close the server (if you want to wait for the connection from the next client, do not close the server), ss. Close ();

TCP transmission

Socket and serversocket

Create a client and a server

After the connection is established, data is transmitted through the IO stream in the socket

Disable socket


Likewise, the client and the server are two independent applications.

 

 Basic Idea (client)

The client must specify the IP address and port of the server to try to establish a connection. If the connection fails, the connection may be abnormal.

The connection is successful, indicating that the client and the server have established a channel, so data can be transmitted through the IO stream, while the socket object already provides the input stream and output stream object, through getinputstream (), getoutputstream.

Close the socket after communication with the server.


Establish an object through socket and specify the server host and port to be connected.
Socket S = new socket ("192.168.1.1", 9999 );
Outputstream out = S. getoutputstream ();
Out. Write ("hello". getbytes ());
S. Close ();

 

Basic Idea (server)

The server must specify the port from which the data to be processed enters.

When a client accesses the client, you must specify the client. You can use accept () to obtain the client object that has been connected and transmit data with the client through the IO stream.

When the access to the client ends, close the client.


To create a server, you must listen to a port.
Serversocket Ss = new serversocket (9999 );
Socket S = ss. Accept ();
Inputstream in = S. getinputstream ();
Byte [] Buf = new byte [1, 1024];
Int num = in. Read (BUF );
String STR = new string (BUF, 0, num );
System. Out. println (S. getinetaddress (). tostring () + ":" + Str );
S. Close ();
SS. Close ();

Dark Horse programmer _ Network Programming

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.