Current location - Quotes Website - Collection of slogans - How to solve the problem of Java Socket communication with its own port
How to solve the problem of Java Socket communication with its own port
There are many problems in the use of Java Socket communication, which is very important in port programming. Let's take a look at how Java Socket communication can make better use of related codes, and I hope everyone can help.

In fact, the simple understanding of network programming is that two computers communicate with each other. It is much easier for programmers to master a programming interface and use a programming model. Java SDK provides some relatively simple APIs to accomplish these tasks, and Java socket communication is one of them. For Java, these APIs exist in this package, so you can prepare for network programming by importing this package.

The basic model of network programming is the client-server model. Simply put, two processes communicate with each other, and then one process must provide a fixed location, while the other process only needs to know the fixed location and establish a connection between them, and then complete data communication. Here, the fixed location is usually called the server, and the connection is usually called that the client can enter the network programming based on this simple model.

There are many APIs in Java that support this model. Here I just want to introduce the programming interface of Java Socket communication. For Java, the programming interface of Socket has been simplified. First, let's discuss how to establish a service provider that provides fixed locations. Java provides a server socket to support it. In fact, when you create a strength object of this class and provide a port and resource, you establish a fixed location where other computers can access your server socket server = new server socket (); It should be noted here that the allocation of ports must be unique, because ports are used to uniquely identify the unique service of each computer. In addition, the port before ~ has been reserved by Tcp/ Ip, so the port you assign can only be the latter. Well, we have a fixed position. Now we only need a connection line, which is requested by the client first, so Java also provides a Socket object to support it. As long as the client creates an instance object of Java Socket communication to support it, the Socket client can do it.

= new Socket(inet address getLocalHost()); The client must know the IP address of the server, and Java also provides the related class InetAddress. An instance of this object must provide its static method through its static method, which mainly provides a method to obtain the local IP and InetAddress directly through the name or IP.

Well, the above method can basically establish a connection for two computers to communicate with each other, but how is the data transmitted? In fact, I/O operations are always closely related to network programming, because the underlying network continues data. Unless the remote call is executed to deal with the core of the problem, data interaction still depends on IO operations, so java io must be imported, which is not complicated. It provides readers and writers for byte streams and Unicode, and then provides buffers for reading and writing data.

buffered reader in = new buffered reader(new InputStreamReader

(server getInputStream());

PrintWriter out = new PrintWriter(server get output stream());

The above two sentences are to convert the original byte stream into Unicode, which can be manipulated (that is, characters) to establish a buffer and improve efficiency. The original byte stream comes from two methods of Java socket communication, getInputStream () and getOutputStream (), which are used to obtain input and output respectively. Now that we have the basic model and basic operating tools, we can make a simple Java socket routine.

Service provider:

Import Java io *;;

Import *;

Public class MyServer {

Public static void main(String[] args) throws IOException{

server socket server = new server socket();

socket client = server accept();

BufferedReader in = new buffered reader (new input stream

reader(client getInputStream());

PrintWriter out = new PrintWriter(client get output stream());

while(true){

string str = in readLine();

System output println (str);

Out println (received);

out flush();

If (string equals (end))

Break;

}

Client shutdown ();

}

}

Lishi Xinzhi/Article/program/Java/hx/20 13 1 1/26805