[Dark horse programmer Java training and Android training] network programming video learning

Source: Internet
Author: User

/*
Requirement: establish communication between computers through the network.
Steps:
1. First, find the computer of the other party through the IP address,
2. Find the application to receive through the port number,
3. Finally, you can establish communication.
*/

1. Basic network programming knowledge
Local Loop IP Address: 127.0.0.1;
The port number ranges from 0 ~ 65535,0 ~ 1023 used for well-known network services and applications;
TCP: Transmission Control Protocol, connection-oriented reliable and error-free communication protocol;
UDP: user data packet protocol, non-connection, unguaranteed communication protocol, fast speed
Data frame format: [protocol type | source IP address | destination IP address | source port | destination port | frame number | frame data]

2. Socket data transmission process
The application generates a socket;
Use a program to send socket information to the driver;
The application sends data to the socket;
The driver extracts data from the socket and sends it through the NIC.

3. Socket data receiving process
The application generates a socket;
The application sends the socket information to the driver;
The driver transmits data packets sent from the NIC to the corresponding socket;
The application extracts data from the socket.

4. datagramsocket class
Three constructors;
Close () method, send (datagrampacketp), receive (datagrampacket P)
 
5. datagrampacket class
Two constructors;
Getinetaddress (), getport ();
Getdata (), getlength ()

6. inetaddress class
Getbyname ();
Gethostaddress ();

7. simple UDP Program

Import java.net .*;
Class udpsendtest
{
Public static void main (string [] ARGs)
{
Try
{
Datagramsocket DST = new datagramsocket ();
DST. Send (New datagrampacket ("A". getbytes (), "A". getbytes (). length, inetaddress. getbyname ("192.168.1.100"), 3000 ));
DST. Close ();
}
Catch (Exception e)
{
}
}
}

Import java.net .*;
Class UDPRecvTest
{
Public static void main (String [] args)
{
Try
{
DatagramSocket dst = new DatagramSocket (3000 );
Byte [] buf = new byte [1, 1024];
DatagramPacket dpt = new DatagramPacket (buf, 1024 );
Dst. receive (dpt );
System. out. println (new String (dpt. getData (), 0, dpt. getLength ()));
}
Catch (Exception e)
{
}
}
}

8. Interaction between the TCP client and the TCP Server
The server program creates a ServerSocket and calls accept () to wait for the client to connect;
The client creates a Socket and requests to connect to the server;
The server receives client connection requests and creates a new Socket;
The two sockets that have established the connection are in a separate thread.

9. ServerSocket
Four Constructors
Close () method and accept () method

10. Socket
Five Constructors
GetInputStream () method and getOutputStream () method

11. Simple Tcp Program

Import java.net .*;
Import java. io .*;
Class TcpServer
{
Public static void main (String [] args)
{
Try
{
ServerSocket ss = new ServerSocket (8001 );
Socket s = ss. accept ();
InputStream ips = s. getInputStream ();
OutputStream ops = s. getOutputStream ();
Ops. write ("Hello World". getBytes ());
BufferedReader br = new BufferedReader (new InputStreamReader (ips ));
System. out. println (br. readLine ());

Br. close ();
Ops. close ();
S. close ();
Ss. close ();
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}

 

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.