Java Network programming

Source: Internet
Author: User
Tags readline stub

Computer network     refers to the different geographical location of multiple computers with independent functions and their external devices, through the communication lines connected, in the network operating system, network management software and network communication Protocol management and coordination, to achieve the sharing of resources and information transmission of computer systems. Network programming     data exchange between programs running on different computers that are used to implement network interconnection.   Three elements of network programming: IP, Port, protocol.  ip:* Unique identification of each device in the network * Each network terminal has a separate address in the network, and we use this address to transmit data over the network. * ipconfig: view native ip192.168.12.42* ping: Test connection 192.168.40.62* Local loop address: 127.0.0.1 255.255.255.255 is a broadcast address * ipv4:4 bytes, 4 0-255. About 4.2 billion, 3 billion are in North America, Asia 400 million. At the beginning of 2011 was exhausted. * Ipv6:8 Group, 4 16 binary numbers per group. * 1a2b:0000:aaaa:0000:0000:0000:aabb:1f2f* 1a2b::aaaa:0000:0000:0000:aabb:1f2f* 1a2b:0000:aaaa::aabb:1f2f* 1a2b:0 000:aaaa::0000:aabb:1f2f* 1a2b:0000:aaaa:0000::aabb:1f2finteaddress1 Getlocalhost ()//Get this machine, parameter is string.2 getByName () Get the specified computer information. 3 gethostname ()//Get the name. 4 gethostaddress ()//Get the address. 4 Getallbyname ()//Get the specified computer all information port: * Each program unique identification on the device * Each network program needs to bind a port number, when transmitting data in addition to determine which machine to send, but also to the specific program to send. * Port number range from 0-65535* to write a network application will need to bind a port number, try to use more than 1024, 1024 or less is basically a system program occupied. * Common Ports     * mysql:3306    * oracle:1521    * web:80    * tomcat:8080 Protocol:  Rules, standards, or conventions established for the exchange of data in a computer networkThe collection.  UDP    * For no connection, data insecure, fast. The client and server are not differentiated.     * Datagram packet send, size limit (max 64KB)  tcp * for connection (three-time handshake), data security, speed slightly lower. Divided into client and server.       * Three-time handshake: The client initiates a request to the server, the server responds to the request, transmits data       * Sends data ignoring size     & nbsp * Socket socket, based on Io stream. Server: 1 Establish the ServerSocket object. 2 Wait for the client to connect. (Accpet (), returns the socket object). 3 establish the input stream and output stream. (getInputStream and Getoutputstream) 4 close the resource. Client: 1 establishes the socket object. (host address, port number) 2 establish the input and output streams with the server. (getInputStream and Getoutoutstream) 3 close the resource. * Client
public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
Socket clinet=new socket ("192.168.3.123", 9989);
InputStream In=clinet.getinputstream ();
OutputStream Out=clinet.getoutputstream ();
Byte[]by=new byte[1024];
StringBuffer sb=new StringBuffer ();
BufferedReader br=new BufferedReader (New InputStreamReader (in));
int Len;
while ((Len=in.read (by))!=-1) {

String St=new string (By,0,len);
Sb.append (ST);

}
System.out.println (Br.readline ());
System.out.println (Sb.tostring ());
Clinet.close ();

} catch (Unknownhostexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
* Service Side
public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
ServerSocket server=new ServerSocket (9989);
Socket clinetsocket=server.accept ();
InputStream In=clinetsocket.getinputstream ();
OutputStream Ou=clinetsocket.getoutputstream ();
String str= "Zzzzz";
Ou.write (Str.getbytes ());
BufferedReader br=new BufferedReader (New InputStreamReader (in));
System.out.println (Br.readline ());
Server.close ();
Clinetsocket.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

Java 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.