20145331 Java programming 10th Week of study summary

Source: Internet
Author: User
Tags server port

20145331 "Java Program Design" 10th Week study summary textbook Learning content Summary Network programming

Network programming is the transfer of data between two or more than two devices, such as a computer. What the programmer is doing is sending the data to the specified location, or receiving the specified data, which is the narrow network programming category. When sending and receiving data, most programming languages design specialized APIs to implement these functions, and programmers only need to invoke them.

Network Programming Steps:

1, Establish network connection Client network programming the first step is to establish a network connection. When establishing a network connection, you need to specify the IP address and port number of the server to which you are connected, and after completion, a virtual connection is formed, and subsequent operations can be exchanged through the connection. 2. After the Exchange data connection is established, the data can be exchanged through this connection. Exchange data in strict accordance with the request response model, the client sends a request data to the server, the server feedback a response data to the client, if the client does not send the request, the server side will not respond. Depending on your logic needs, you can exchange data multiple times, but you still have to follow the request response model. 3. Close network connection after the data exchange is complete, close the network connection, release the port, memory and other system resources, and end the network programming.

Network communication mode

There are two main ways of network communication:

            TCP(传输控制协议)方式            UDP(用户数据报协议)方式
Java Network Programming Technology

1. The basic API related to network programming is located in the java.net package, which contains a basic network programming implementation, which is the basis of network programming. The package contains both the underlying network programming classes and the encapsulated specialized processing classes for web-related processing.

2, InetAddress class: The function of this class is to represent an IP address, and the IP address and domain name related operation methods are included inside the class. With regard to the use of this class, the use of the class is demonstrated below through a basic code example.

TCP Programming

1. The Java.net.Socket class represents the client connection, and the Java.net.ServerSocket class represents the server-side connection. In network programming, the details of the underlying network communication have been implemented in a relatively high package, so when the programmer actually programming, only need to specify the IP address and port number to establish a connection.

2, in the client network programming, the first need to establish a connection, in the Java API with the Java.net.Socket class of objects representing network connections, so establish a client network connection, that is, the creation of the socket type object, which represents the network connection, the example is as follows:

Socket socket1 = new Socket(“192.168.1.103”,10000);Socket socket2 = new Socket(“www.sohu.com”,80);
Network protocol:

1, network protocol refers to the network transmission of data format provisions. For the network programming beginners, there is no need to understand the TCP/IP protocol cluster, so for beginners to read the big "TCP/IP protocol" is not a very appropriate thing, because in-depth understanding of TCP/IP protocol is the network programming stage, but also in-depth network programming at the bottom of the things to do.

2, for the general network programming, more is concerned about the network transmission of logical data content, that is more of the application layer on the network protocol, so the subsequent content is based on the actual application of data to introduce the concept of network protocol.

3, the network protocol design is completed, in the network programming, it is necessary according to the design of the protocol format, the corresponding coding in the program, the client program and the server-side program needs to deal with the code as follows.

客户端程序需要完成的处理为:1、 客户端发送协议格式的生成2、 服务器端反馈数据格式的解析服务器端程序需要完成的处理为:1、 服务器端反馈协议格式的生成2、 客户端发送协议格式的解析
Problems in teaching materials learning and the solving process
Package Tcp;import Java.io.*;import java.net.*;p ublic class Mulsocketclient {public static void main (string[] args) {               Socket socket = NULL;               InputStream is = null;               OutputStream OS = null;               Server-side IP address String serverip = "127.0.0.1";               Server port port number int port = 10000;               Send content String data[] ={"First", "Second", "third"};                        try {//Establish connection socket = new socket (serverip,port);                        Initialize stream OS = Socket.getoutputstream ();                        is = Socket.getinputstream ();                        Byte[] B = new byte[1024]; for (int i = 0;i < data.length;i++) {//Send data os.write (DAT                                 A[i].getbytes ());             Receive data int n = is.read (b);                    Output Feedback Data System.out.println ("Server Feedback:" + new String (b,0,n)); }} catch (Exception e) {e.printstacktrace ();//print exception information}f                                 inally{try {//close stream and connect is.close ();                                 Os.close ();                        Socket.close (); } catch (Exception e2) {}}}}

The result of the program is:

java.net.SocketException: Software caused connection abort: recv failed            at java.net.SocketInputStream.socketRead0(Native Method)            at java.net.SocketInputStream.read(SocketInputStream.java:129)            at java.net.SocketInputStream.read(SocketInputStream.java:90)            at tcp.MulSocketClient.main(MulSocketClient.java:30)服务器反馈:First
Learning Progress /Cumulative) new/cumulative)
lines of code (newBlog volume (Learning time (new/cumulative) Important growth
Goal 5000 rows 30 Articles 400 hours
Nineth Week 292 2/2 20
Resources

Java Study Notes (8th edition)

• Learning guide for Java Learning Notes (8th Edition)

20145331 Java programming 10th Week of study summary

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.