Get the App
SLTechnology News&Howtos  ›  Development  › 

JavaTcp communication client and server instance code

Shulou Source: shulou.com Published: 2022-06-03 11:41:50 11月15日 Update

This article introduces the relevant knowledge of "javaTcp communication client and server instance code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

This paper gives an example of java Tcp communication between client and server. Share with you for your reference, the details are as follows:

Data is sent by the server

Server side:

Import java.io.*;import java.net.*;public class TestSocket {public static void main (String [] args) {try {ServerSocket ss = new ServerSocket (8888); while (true) {Socket s = ss.accept (); OutputStream os = s.getOutputStream (); DataOutputStream dos = new DataOutputStream (os); dos.writeUTF ("hello" + s.getInetAddress () + "port" + s.getPort () + "beybye"); dos.close (); / / os.flush (); os.close (); / / s.close () } catch (IOException e) {e.printStackTrace (); System.out.println ("there is a wrong");}

Client:

Import java.io.*;import java.net.*;public class TestClient {public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 8888); DataInputStream dis = new DataInputStream (s.getInputStream ()); System.out.println (dis.readUTF ()); s.close (); dis.close ();} catch (Exception e) {e.printStackTrace ();}

Both the client side and the server side can send and receive data.

Interactive type

Client end

Import java.io.*;import java.net.*;public class TestClient2 {public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 8886); DataOutputStream dos = new DataOutputStream (s.getOutputStream ()); DataInputStream dis = new DataInputStream (s.getInputStream ()); System.out.println (dis.readUTF ()); dos.writeUTF ("hey"); String str = null; if ((str = dis.readUTF ())! = null) {System.out.println (str) } s.close (); dis.close (); dos.close ();} catch (Exception e) {e.printStackTrace ();}

Server side:

Public class TestServer2 {public static void main (String [] args) {InputStream in = null; OutputStream out = null; try {ServerSocket ss = new ServerSocket (8886); while (true) {Socket s = ss.accept (); in = s.getInputStream (); out = s.getOutputStream (); DataOutputStream dos = new DataOutputStream (s.getOutputStream ()); DataInputStream dis = new DataInputStream (s.getInputStream ()); String str = null; if ((str = dis.readUTF ())! = null) {System.out.println (str) System.out.println ("form" + s.getInetAddress ()); System.out.println ("port" + s.getPort ()); / / dos.writeUTF ("hello" + s.getInetAddress () + "port" + s.getPort () + "beybye");} dos.writeUTF ("hi hello"); dis.close (); dos.close (); s.close ();}} catch (IOException e) {e.printStackTrace (); System.out.println ("there is a wrong");}

This is the end of the content of "javaTcp Communication client and Server instance Code". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Server service client client instance communication code content data more user client knowledge utility achievement next dilemma reality situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn Xiaomi Shulou Tech Info Linux