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

How to communicate between java programs

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

Share

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

This article will explain in detail how to communicate between java programs. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

What is socket?

Socket, translated into sockets in Chinese. It is an abstraction of endpoints that communicate bidirectionally between application processes on different hosts (or different application processes on the same host) in the network. It is actually the endpoint in the various communication connections between the two networks. Its function is to complete the data transmission between applications.

Socket technology is simply through the socket, the two applications are connected, after the connection can be carried out data transmission. Socket is just a way to make a connection.

The editor believes that the translation of sockets does not directly reflect the characteristics of the technology (too crude), socket can be translated into sockets, but in fact socket technology is more like "sockets" and "plugs" in software. In Chinese, socket is a kind of water pipe docking technology, which uses a socket to connect the water pipes at both ends. This technology is called socket connection, so can you understand what a socket is?

How to use socket?

Different languages implement sockets in different ways, and java has packages that specifically implement sockets. Socket programming can be done only with the corresponding package of import.

After creating a socket connection, you can obtain and send data transferred between socket through the methods of getInputStream () and getOutputStream ().

Next, let's use an example code to introduce how to use socket!

Socket project actual combat

Server.java

Import java.io.*;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;public class Server {public static void main (String [] args) {try {ServerSocket serverSocket = new ServerSocket (8888); / / listen on port 8888 to see if there is a connection request to System.out.println ("open server"); Socket accept = serverSocket.accept () / / get socket connection System.out.println ("connected" + accept); Thread thread1 = new Thread () {/ / use multiple threads to send and receive messages, thread 1 is used to receive messages @ Override public void run () {try {InputStream inputStream = accept.getInputStream () / / get the socket input stream DataInputStream dataInputStream = new DataInputStream (inputStream); / / encapsulate the input stream in DataInputStream while (true) {String msg = dataInputStream.readUTF () / / use readUTF to read the string System.out.println ("listen for client messages:" + msg);}} catch (IOException e) {e.printStackTrace ();} Thread thread2 = new Thread () {/ / Thread 2 is used to send the message @ Override public void run () {try {OutputStream outputStream = accept.getOutputStream (); / / Open the output stream DataOutputStream dataOutputStream = new DataOutputStream (outputStream) / / encapsulate the output stream in DataOutputStream Scanner scanner = new Scanner (System.in); while (true) {String strServer = scanner.next (); dataOutputStream.writeUTF (strServer) / / send the string}} catch (IOException e) {e.printStackTrace ();}; thread1.start (); thread2.start (); thread1.join () using writeUTF System.out.println ("server shutdown"); accept.close (); / close a single socket connection serverSocket.close (); / / close this socket server} catch (Exception e) {System.out.println ("disconnect");}

Client.java

Import java.io.*;import java.net.Socket;import java.util.Scanner;public class Client {public static void main (String [] args) {String host = "127.0.0.1"; / / the ip address of the target computer, where the local address is int port = 8888 / the port boolean flag of the target computer boolean flag = true; try {Socket socket = new Socket (host, port) / / create a destination connection, passing in the ip address and port number System.out.println ("Open client") Thread thread1 = new Thread () {/ / use the multithreaded method to send and receive, thread 1 is responsible for receiving the message @ Override public void run () {try {InputStream inputStream = socket.getInputStream (); / / getting the socket input stream DataInputStream dataInputStream = new DataInputStream (inputStream) / / encapsulate the input stream in DataInputStream while (true) {String msg = dataInputStream.readUTF (); / / use readUTF to read the string System.out.println ("receive server messages:" + msg) }} catch (IOException e) {e.printStackTrace ();} Thread thread2 = new Thread () {/ / Thread 2 is used to send the message @ Override public void run () {try {OutputStream outputStream = socket.getOutputStream (); / / Open the output stream DataOutputStream dataOutputStream = new DataOutputStream (outputStream) / / encapsulate the output stream in DataOutputStream Scanner scanner = new Scanner (System.in); while (true) {String str = scanner.next (); dataOutputStream.writeUTF (str) / / send the string}} catch (IOException e) {e.printStackTrace ();}; thread1.start (); thread2.start (); thread1.join () using writeUTF System.out.println ("client shutdown"); socket.close (); / / close this socket} catch (Exception e) {System.out.println ("disconnect") } this is the end of the article on "how to communicate between java programs". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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: 239

*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