javasocket服务器[通俗易懂]

Java (85) 2023-05-30 12:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说javasocket服务器[通俗易懂],希望能够帮助你!!!。

import java.net.*;
import java.io.*;
public class a {
    public static void main(String[] args) {
        try {//建立socket
            ServerSocket s1 = new ServerSocket(13555);//注:如果服务器在内网需要端口映射
            Socket x = s1.accept();
            System.out.println("服务已开启");
            //读取客户端的信息
            BufferedReader br = new BufferedReader(new InputStreamReader(x.getInputStream()));
            String mess = br.readLine();
            //打印出来
            System.out.println(mess);
            //回复
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(x.getOutputStream()));
            bw.write(mess+"123");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("can't");
        }
    }
}

发表回复