First, let us take a look at the simplest socket client and server examples:
Client
public class MyClient { public static void main(String[] args) { ObjectOutputStream oos = null; ByteArrayOutputStream bos = null; Socket client = null; try { People p = new People("2","yangyu","4","5","6"); oos = new ObjectOutputStream(bos = new ByteArrayOutputStream()); //初始化object输出流 oos.writeObject(p); //将People对象写入输出流 byte[] bytes = bos.toByteArray(); //获取People对象的byte数组(也就是序列化People) client = new Socket("127.0.0.1",20007); //连接127.0.0.1的20007端口 client.setSoTimeout(10000); //设置超时时间 client.getOutputStream().write(bytes); //向server发送byte[]数组 byte[] bytes1 = IOUtils.readFully(client.getInputStream(),18,false); //获取server返回数据 System.out.println(new String(bytes1)); System.out.println(bytes1.length); } catch (Exception e) { e.printStackTrace(); }finally { try { oos.close(); bos.close(); System.out.println(client.isClosed()); client.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Server
public class MyServer { public static void main(String[] args) { ServerSocket server = null; Socket client = null; ObjectInputStream ois = null; ByteArrayInputStream bis = null; try { server = new ServerSocket(20007); //启动Socket server,监听20007端口 client = server.accept(); //阻塞并等待接收客户端发送数据并生成client byte[] bytes = IOUtils.readFully(client.getInputStream(),-1,false);//获取客户端发送过来的数据 bis = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bis); People people = (People) ois.readObject();//反序列化 System.out.println("people name:"+people.getName()); String res = "消息已经收到"; client.getOutputStream().write(res.getBytes());//向客户端发送数据 } catch (Exception e) { e.printStackTrace(); }finally { try { bis.close(); ois.close(); client.close(); server.close(); } catch (IOException e) { e.printStackTrace(); } } } }
The above is a Client and a Server, the simplest example, but embodies socket programming.
If you need the Server to always listen to the port, you only need to loop (server.accept() will block waiting for requests). As for high-concurrency responses, let the Server handle the data business to the thread pool. .

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)
