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

What is the method of server debugging and management for Java remote configuration

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

Share

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

This article mainly explains "what is the method of server debugging and management of Java remote configuration". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of server debugging and management for Java remote configuration?"

1,Socket, ServerSocket:

Socket is located in java.net. Package, this is a very important class for network communication, no matter which language, there will be a fixed application of the Socket class, the only difference is in the presentation and organization, Socket Chinese calls it a socket, javaAPI can see the corresponding introduction to a ServerSocket to construct a pair of communication sockets as follows

ServerSocket sr=new ServerSocket (port); Socket sc=new Socket ("ip", port)

ServerSocket's accpet method is very important, because when a service socket is established, it blocks waiting for a socket request until a connection is established.

Some computer network books have this definition of socket = ip+port; A socket is made up of an ip address and a port, and the underlying implementation in network communication is also true. If you want a process to communicate, you must set up a working port for it, and then you can communicate on that port after connecting the port.

2, InputStream, OutputStream

When a socket connection is successful, the input and output stream based on this socket can be obtained. All data sending and receiving are inseparable from the output input stream. We can choose the type of data you want to input or output through stream encapsulation.

Look at the following example:

This is a complete server-side example. Please read the notes carefully.

/ / Server.java

Import java.net.*

Import java.io.*

Public class Server {

Public static void main (String args [])

{

ServerSocket server=null;// defines service sockets

Socket you=null

String s=null

DataOutputStream out=null;// definition flow

DataInputStream in=null

Try {

Server=new ServerSocket (4331); / * create a server socket here

The construction method of a service socket only needs a service port, which is very simple * /

} catch (Exception e) {System.out.println (e.getMessage ());}

/ / catch exception

Try {

System.out.println ("wait.")

You=server.accept (); / / blocking here waiting for the customer to intervene

Out=new DataOutputStream (you.getOutputStream ()); / / get the output stream

In=new DataInputStream (you.getInputStream ()); / / get the output stream

While (true) / / Loop read

{

S=in.readUTF ()

Int m=Integer.parseInt (s)

Out.writeUTF ("Hello i am server!"); / / send messages

Out.writeUTF ("X 2:" + s)

System.out.println ("Server receive:" + s)

Thread.sleep (300); / / f prevents group matches without this statement or takes up a lot of cpu and memory

}

} catch (Exception e) {System.out.println ("Client leave!");}

}

}

The above is a simple server-side program that shows how to implement the server construction of the socket, and the following is a client of Java remote configuration

Import java.io.*

Import java.net.*

Public class Client {

Public static void main (String args [])

{

String s=null

Socket mysocket;// creates client sockets

DataInputStream in=null

DataOutputStream out=null

Try {

Mysocket=new Socket ("127.0.0.1", 4331); /

* notice how the instantiation construction method is different from the server * /

System.out.println ("localport:" + mysocket.getLocalPort ())

System.out.println ("localAddress:" + mysocket.

GetLocalAddress ()

System.out.println ("localSocketAddress:" + mysocket.

GetLocalSocketAddress ()

System.out.println ("Chanel:" + mysocket.getChannel ())

/ / the above code shows the specific communication address port

In=new DataInputStream (mysocket.getInputStream ())

Out=new DataOutputStream (mysocket.getOutputStream ())

For (int kumb1witk)

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