Get the App
SLTechnology News&Howtos  ›  Development  › 

How to introduce the source code related to Java Socket programming

Shulou Source: shulou.com Published: 2022-06-02 12:40:22 09月29日 Update

Today, I will talk to you about the introduction of the source code related to Java Socket programming, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

There are many problems in Java Socket programming, so let's take a detailed look at how to make better use of this code. Next let's take a look at the relevant source code introduction.

Source code introduction:

MyServer.java import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class MyServer extends Thread {private int serverPort; private int maxConnection; public MyServer (int serverPort, int maxConnection) {this.serverPort = serverPort; this.maxConnection = maxConnection;} @ Override public void run () {ServerSocket serverSocket = null; Socket socket = null; try {serverSocket = new ServerSocket (serverPort, maxConnection); socket = serverSocket.accept () New ServerProcess (socket). Start ();} catch (IOException e) {e.printStackTrace (); System.exit (1);} finally {if (serverSocket! = null) {try {serverSocket.close ();} catch (IOException e) {e.printStackTrace (); System.exit (1);} private class ServerProcess extends Thread {private Socket socket; public ServerProcess (Socket socket) {this.socket = socket } @ Override public void run () {InputStream stream = null; byte buffer [] = new byte [1024]; int length; StringBuffer recvMessage = new StringBuffer (); try {stream = socket.getInputStream (); while (true) {length = stream.read (buffer); if (length > 0) {recvMessage.append (new String (buffer, 0, length));} else {break;} System.out.println (recvMessage.toString ()) } catch (IOException e) {e.printStackTrace ();} MyClient.java import java.io.IOException; import java.io.OutputStream; import java.net.Socket; public class MyClient {private String serverAddress; private int serverPort; public MyClient (String serverAddress, int serverPort) {this.serverAddress = serverAddress; this.serverPort = serverPort;} public boolean sendMessage (String message) {Socket socket = null; OutputStream stream = null; try {socket = new Socket (serverAddress, serverPort) Stream = socket.getOutputStream (); stream.write (message.getBytes ()); return true;} catch (IOException e) {e.printStackTrace (); return false;} finally {try {if (socket! = null) {socket.close ();} if (stream! = null) {stream.close ();}} catch (IOException e) {e.printStackTrace () } Main.java public class Main {public static void main (String [] args) {MyServer server = new MyServer (6001, 1); server.start (); MyClient client = new MyClient ("localhost", 6001); client.sendMessage ("Hello tomorrowns!");}}

The result of Java Socket programming

Hello Tomorrow!! After reading the above, do you have any further understanding of the introduction of the source code related to Java Socket programming? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Source code programming content and then constantly code more knowledge articles results industry information information channels problems channels into one development support related run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi MariaDB macOS Microsoft