Black Horse programmer _javase_ Network programming

Source: Internet
Author: User

    • Network model:
    1. OSI model
    2. TCP/IP model

  • Network communication elements
    IP Address
    Port
    Communication protocols

  • Communication process
    1. Find the other IP address
    2. Data to be sent to the other specified application, in order to identify these applications, with a unique number to identify, these digital symbols are the port
    3. Define communication rules, TCP or UDP, or other

  • Application Layer Protocol
    Http,ftp,smtp

  • Resolve Host Address:
    InetAddress ia = inetaddress.getbyname ("www.baidu.com");
    String IP = ia.gethostname ();

  • TCP and UDP
    Features of 1.UDP:
    Encapsulates data and source addresses and destination addresses into packages, without the need to establish a connection,
    The size limit for each packet is 64k
    Because it is a non-connected form oriented, it is an unreliable protocol (the address does not necessarily exist, the port does not necessarily open, if the purpose is unreachable, the packet will be discarded)
    No need to establish a connection, fast speed (commonly used: QQ chat, Lingbo, network Video)
    Features of 2.TCP:
    The connection must be established to form the transmitted data channel
    Connection is suitable for transmission of big data in connection after establishment
    The establishment of the connection requires three handshake, which is a reliable protocol (can be detected immediately after disconnection)
    must establish a connection first, the efficiency will be relatively low

  • Socket
    A socket is a mechanism for network services.
    Both ends of the communication are Scoket
    Network communication is actually the communication between sockets
    Data communication between sockets
    Data is transmitted via IO between scoket

  • Network address: 192.168.1.0

  • Broadcast address: 192.168.1.255

  • TCP Transport:
    Service side: ServerSocket
    Has client: Socket
    One server can have multiple clients
    The getInputStream and Getoutputstream methods of the Scoket object can be used to obtain the communication IO flow between the scoket.

    Package network programming. UDP network programming; import Java.net.datagrampacket;import Java.net.datagramsocket;public class Udprtest {/** * @param args * @throws Exception  */public static void Main (string[] args) throws Exception {//Create UDP service Datagramsocket ds = new Datagramsocket ( 10005);//The port number that the receiver listens to//defines the data format byte[] buf = new byte[1024];D atagrampacket dp = new Datagrampacket (buf, buf.length);// Receive data while (true) {ds.receive (DP); String IP = dp.getaddress (). gethostaddress (); String data = new String (Dp.getdata (), 0,dp.getlength ()); int port = Dp.getport (); System.out.println (ip+ ":" +port+ ":" +data);}}}

    Package network programming. UDP network programming; import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;public class udpnettest {public static void main (string[] args) throws exception{//create UDP service Datagramsocket ds = new Datagramsocket (8889) //UDP the port number of the sender//create the data to be sent byte[] buf = "Hello world! ". GetBytes ();D atagrampacket dp = new Datagrampacket (buf, Buf.length, Inetaddress.getbyname (" localhost "), 10005);// Address to each other and port number received by the other//send while (true) {Thread.Sleep (+);d S.send (DP);}}

    Package network programming. Multithreading and UDP implement chat function; import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.net.datagrampacket;import java.net.datagramsocket;import java.net.inetaddress;/** * Chat room: A thread control receiving data, a line programmed to send data * Since the data is received and sent inconsistent, all to define two run methods, so to define two * @author Administrator * */public class Chatroom {public static void main (string[] args ) throws Exception{datagramsocket Sendds = new Datagramsocket ();D atagramsocket Recds = new Datagramsocket (9990); Send send = new Send (SENDDS); Receiver RE = new receiver (RECDS), new Thread (send), start (); new Thread (re). Start ();}} Class Send implements Runnable{private Datagramsocket ds; Send (Datagramsocket ds) {this.ds = ds;} public void Run () {try {bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); String line = null;while (line = Br.readline ())!=null) {//while loop, which guarantees that this thread does not die under certain circumstances if ("886". Equals (line)) break;byte[] Buff = line.getbytes ();D atagrampacket dp =new datagrampacket (Buff, buff.length, Inetaddress.getbyname ("192.168.0.255" ), 9990);d S.send (dp);}} catch (Exception e) {throw new RuntimeException ("Send-side exception");}}} Class Receiver implements Runnable{private Datagramsocket ds; Receiver (Datagramsocket ds) {this.ds = ds;} public void Run () {try {while (true) {//while loop, ensure this thread does not die byte[] buf = new byte[1024];D atagrampacket dp = new Datagrampacket (bu F, Buf.length);d s.receive (DP); String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); String data = new String (Dp.getdata (), 0, Dp.getdata (). length); System.out.println (ip+ ":" +port+ "said:" +data);}} catch (Exception e) {throw new RuntimeException ("Receive failed");}}}



Black Horse programmer _javase_ 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.