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 realize chat function by Java Network programming TCP

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

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Java network programming TCP how to achieve chat function", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java network programming TCP how to achieve chat function" article.

Client: package com.kuang.lesson02;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;// client public class TcpClientDemo01 {public static void main (String [] args) {Socket socket = null; OutputStream os = null Try {/ / 1, to know the address of the server, port number InetAddress serverIP = InetAddress.getByName ("127.0.0.1"); int port = 9999; / / 2, create a socket connection socket = new Socket (serverIP, port); / / 3, send the message IO stream os = socket.getOutputStream () Os.write ("Hello, Java" .getBytes ());} catch (Exception e) {e.printStackTrace ();} finally {if (os! = null) {try {os.close ();} catch (IOException e) {e.printStackTrace () }} if (socket! = null) {try {socket.close ();} catch (IOException e) {e.printStackTrace ();} server: package com.kuang.lesson02;import java.io.ByteArrayOutputStream Import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;// server public class TcpServerDemo01 {public static void main (String [] args) {ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try {/ / 1, I need to have an address serverSocket = new ServerSocket (9999) While (true) {/ / 2, wait for the client to connect to socket = serverSocket.accept (); / / 3, read the message from the client is = socket.getInputStream (); / / Pipeline flow baos = new ByteArrayOutputStream (); byte [] buffer = new byte [1024] Int len; while ((len = is.read (buffer))! =-1) {baos.write (buffer, 0, len);} System.out.println (baos.toString ());}} catch (Exception e) {e.printStackTrace () } finally {/ / close resource if (baos! = null) {try {baos.close ();} catch (IOException e) {e.printStackTrace () }} if (is! = null) {try {is.close ();} catch (IOException e) {e.printStackTrace () }} if (socket! = null) {try {socket.close ();} catch (IOException e) {e.printStackTrace () }} if (serverSocket! = null) {try {serverSocket.close ();} catch (IOException e) {e.printStackTrace ();} run result:

1. First run the server and wait for the message to be received. You can find that the server has been running.

2. Then run the client and send a message, and you can find the end of the client.

3. Go back to the server and find that the server has received the message sent by the client.

4. Because it is a circular operation, as long as the client sends a message, the server can receive it and send the message multiple times.

The above is about "Java network programming TCP how to achieve chat function" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please pay attention to the industry information channel.

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

Internet Technology

Wechat

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

12
Report