Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to how to write and use the key code of Java SocketAPI

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Java SocketAPI key code how to write and use introduction, in view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

How can Java SocketAPI be used normally? This problem requires us to master the relevant code. The so-called socket, also known as a "socket", is used to describe the IP address and port, and is a handle to a communication chain. Applications usually send requests to or respond to network requests through "sockets".

Take J2SDK-1.3 as an example, the Java SocketAPI and ServerSocket class libraries are located in the java.net package. ServerSocket is used on the server side, and Socket is used when establishing network connections. When the connection is successful, an Socket instance is generated at both ends of the application, and the instance is manipulated to complete the required session. For a network connection, sockets are equal, there is no difference, and there are no different levels on the server side or on the client side. Both Socket and ServerSocket do their work through the SocketImpl class and its subclasses.

Important Java SocketAPI:

Java.net.Socket inherits from java.lang.Object and has eight constructors, but there are not many methods. Here are the three most frequently used methods, and others can be found in the JDK-1.3 documentation.

The Accept method is used to generate "blocking" until a connection is received and an instance of the client's Socket object is returned. "blocking" is a term that causes a program to run temporarily "stay" at this place until a session is generated, and then the program continues; usually "blocking" is caused by a loop.

The getInputStream method takes the network connection input and returns an instance of the IutputStream object.

The other end of the getOutputStream method connection gets the input and returns an instance of the OutputStream object.

Note: both the getInputStream and getOutputStream methods produce an IOException that must be captured because the stream object they return is usually used by another stream object.

How to develop a program of Server-Client model

Development principles:

The server uses ServerSocket to listen on the specified port, and the port can be specified at will (since the port below 1024 is usually reserved and cannot be used at will in some operating systems, it is recommended to use a port greater than 1024). Wait for the customer connection request, and the session occurs after the client connection is completed. After the session is completed, close the connection.

The client uses Socket to make a connection request to a port of a server on the network, once the connection is successful, open the session; after the session is completed, close the Socket. The client does not need to specify an open port, and usually temporarily and dynamically assigns a port of more than 1024.

{set up a server}

Import java.net.*

Import java.io.*

Public class Server

{

Private ServerSocket ss

Private Socket socket

Private BufferedReader in

Private PrintWriter out

Public Server ()

{

Try

{

Ss = new ServerSocket (10000)

While (true)

{

Socket = ss.accept ()

In = new BufferedReader (new InputStreamReader

(socket.getInputStream ())

Out = new PrintWriter (socket.getOutputStream (), true)

String line = in.readLine ()

Out.println ("you input is:" + line)

Out.close ()

In.close ()

Socket.close ()

}

Ss.close ()

}

Catch (IOException e)

{}

}

Public static void main (String [] args)

{

New Server ()

}

}

This program sets up a server that has been listening to port 10000, waiting for users to connect. Return a piece of information to the client after the connection is established, and then end the session. This program can only accept one customer connection at a time.

{establish client}

Import java.io.*

Import java.net.*

Public class Client

{

Socket socket

BufferedReader in

PrintWriter out

Public Client ()

{

Try

{

Socket = new Socket ("xxx.xxx.xxx.xxx", 10000)

In = new BufferedReader (new InputStreamReader

(socket.getInputStream ())

Out = new PrintWriter (socket.getOutputStream (), true)

BufferedReader line = new BufferedReader (new

InputStreamReader (System.in))

Out.println (line.readLine ())

Line.close ()

Out.close ()

In.close ()

Socket.close ()

}

Catch (IOException e)

{}

}

Public static void main (String [] args)

{

New Client ()

}

}

The client connects to the server with the address xxx.xxx.xxx.xxx on port 10000, enters a line of information from the keyboard, sends it to the server, and then accepts the return message from the server to end the session.

On how to write Java SocketAPI key code and the use of introduction questions to share here, I hope the above content can be of some help to you, if you still have a lot of doubts have not been solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report