Basic Java Tutorial-network programming (VII)

Source: Internet
Author: User

Seven, network programming

7.1. IP protocol

The most important contribution is the IP address

7.2. TCP and UDP protocols

TCP (FAST) reliable transmission, must establish a connection when sending (three-time Handshake protocol)

UDP (slow) unreliable transmission, no need to establish a connection when sending

7.3. TCP TCP server and TCP Client

Note that to start the Serverfirst, start the Client again

The port number is the application's entry, there are two bytes, so each server can run up to 65536 applications, and TCP port and UDP Port is not the same , when the port is defined, Try to define more than 1024x768.

A particularly typical port number,http;

TCP Serverimport Java.net.*;import java.io.*; public class TCPServer {public static void main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (6666); while (true) {Socket s = ss.accept ();//blocking function, wait until there is access, will continue to execute SYSTEM.OUT.PRINTLN ("a client connect!");D Atainputstream dis = new DataInputStream (S.getinputstream ()); System.out.println (Dis.readutf ());d is.close (); S.close (); }}

TCP Clientimport Java.net.*;import java.io.*; public class TCPClient {public static void main (string[] args) throws Exception {socket s = new socket ("127.0.0.1", 6666); OutputStream OS = S.getoutputstream ();D ataoutputstream dos = new DataOutputStream (OS); Thread.Sleep;d os.writeutf ("Hello server!"); Dos.flush ();d os.close (); S.close ();}}

  

7.4. UDP server and client (UDP can only transfer byte arrays)

UDP   Serverimport java.net.*; public class Testudpserver{public static void Main (String args[]) throws exception{ byte buf[] = new byte[1024];D atagrampacket dp = new Datagrampacket (buf, buf.length);D atagramsocket ds = new Datagramsocket (5678); while (true) {ds.receive (DP); System.out.println (New String (Buf,0,dp.getlength ()));//string Constructor}}}

   

UDP Clientimport java.net.*; public class Testudpclient{public static void Main (String args[]) throws exception{byte[] buf = (new String ("Hello")). Getb Ytes ();D atagrampacket dp = new Datagrampacket (buf, Buf.length,    new Inetsocketaddress ("127.0.0.1", 5678)   );D Atagramsocket ds = new Datagramsocket (9999)//client own port because UDP is not a connection-oriented Ds.send (DP);d s.close ();}}

  

TESTUDP Clientimport Java.net.*;import java.io.*; public class Testudpclient{public static void Main (String args[]) throws exception{long n = 10000L; Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();D ataoutputstream dos = new DataOutputStream (BAOs); Dos.writelong (n); byte[] buf = Baos.tobytearray ();//convert Long data into a character array, input System.out.println (buf.length); Datagrampacket DP = new Datagrampacket (buf, Buf.length,    new Inetsocketaddress ("127.0.0.1", 5678)   

  

TESTUDP Serverimport Java.net.*;import java.io.*; public class Testudpserver{public static void Main (String args[]) throws Exception{byte buf[] = new byte[1024];D Atagrampac Ket DP = new Datagrampacket (buf, buf.length);D atagramsocket ds = new Datagramsocket (5678); while (true) {ds.receive (DP); Bytearrayinputstream Bais = new Bytearrayinputstream (buf);D atainputstream dis = new DataInputStream (Bais); System.out.println (Dis.readlong ());//Read the character array as Long}}}

  

Basic Java Tutorial-network programming (VII)

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.