Network Programming Learning __ Programming

Source: Internet
Author: User
Tags socket stub

-----------Android Training, Java training, Java Learning Technology blog, looking forward to communicating with you. ------------

I. Overview of network programming

A) Find the other IP: the identity of the device in the network, not easy to remember, the available host name, the native loopback address 127.0.0.1

b) data to be sent to the other specified application, in order to identify these applications, so that the network applications are identified with a number, in order to facilitate the name of this number, called the port. Logical port.

I. The logical address used to identify the process, and the identity of the different processes

II. Valid port: 0~65535 where 0~1024 system uses or retains ports

c) Define the communication rules, which are called protocols, and international organizations define common protocols, TCP/IP protocol

I. UDP

1. Encapsulate the data and the source and destination into a packet, no need to establish a connection

2. Limit the size of each packet within 64K

3. Due to no connection, it is an unreliable agreement

4. Do not establish a connection, fast.

II. TCP

1. Establish a connection to form a channel for transmitting data

2. Large data transfer in the connection

3. Complete the connection via three handshake is a reliable protocol

4. The connection must be established and the efficiency will be slightly lower.

Two. Socket

A) socket is a mechanism for network services

b) sockets on both ends of the communication

c) network communication is actually the communication between sockets

d) data is transmitted via IO between two sockets

e) Send a piece of text data via UDP transmission

I. Establish Udpsocket service Datagramsocket ds = new Datagramsocket ();

II. Provide data and encapsulate the data in a packet byte[] buf = "udp ge". GetBytes ();D atagrampacket dp= newdatagrampacket (Buf,buf.length,ineta Ddress.getbyname ("192.168.1.254"), 10000);

III. Send the packet to Ds.send (DP) via the socket service's sending function;

Iv. closing Resource ds.close ();

f) Receiving end

I. Define the Udpsocket service, usually listen to a port, in fact, to the receiving network application to define the digital ID. It is easy to identify what data comes over the application can handle.

II. Define a packet because the received byte data is stored because there are more features in the packet object that can extract different data information from the byte data.

III. The received data is stored in a defined packet via the socket service's Receive method

Iv. take these different data out and print them on the console using the unique features of the packet object

V. Closing RESOURCES

Three. TCP Transmission

A) sockets and ServerSocket

b) Establish client socket and server-side ServerSocket

c) After the connection is established, the data is transmitted via the IO stream in the socket

d) Close the socket

e) Similarly, the client and server side are two separate applications.

Four. UDP Chat Example:

Package com.jwd.black.chat;

import java.net.*;

importjava.io.*;

Publicclass Receimplements Runnable {

Private Datagramsocketds;

Public Rece (Datagramsocketds) {

this. DS =ds;

}

@Override

publicvoid Run () {

TODO Auto-generatedmethod stub

Try {

while (true) {

byte []BUF = newbyte[1024];

Datagrampacket DP = new datagrampacket (buf,buf.length);

Ds.receive (DP);

String IP = dp.getaddress (). gethostaddress ();

String data = new string (Dp.getdata (), 0,dp.getlength ());

System. out. println (IP + ":" + data);

}

} catch (Exceptione) {

thrownew RuntimeException ("Receive stream failed");

}

}

}

Package com.jwd.black.chat;

import java.net.*;

Publicclass Chatdemo {

publicstaticvoid Main (String[]args) throws Exception {

TODO Auto-generatedmethod stub

Datagramsockets =newdatagramsocket ();

DATAGRAMSOCKETR =newdatagramsocket (10002);

New Thread (new Send (s)). Start ();

New Thread (new rece (R)). Start ();

}

}

Package com.jwd.black.chat;

import java.io.*;

import Java.net.DatagramPacket;

import java.net.*;

Publicclass Sendimplements Runnable {

Private Datagramsocketds;

Public Send (Datagramsocketds) {

this. DS =ds;

}

@Override

publicvoid Run () {

TODO Auto-generatedmethod stub

Try {

BufferedReader bufr = new bufferedreader (new InputStreamReader (System. in));

String line = null;

while (Line =bufr.readline ())! =null) {

if ("886". Equals (line))

break;

byte []BUF = Line.getbytes ();

Datagrampacket DP = new datagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.128"), 10002);

Ds.send (DP);

}

}catch(Exceptione) {

thrownew RuntimeException ("sender failed");

}

}

}

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.