Java网络编程(TCP通信程序)实例

(1)服务端

public class Server {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            //创建一个服务端的套接字的对象,并指定端口号
            ServerSocket sever = new ServerSocket(8080);//8080表示程序运行的唯一标识
            //调用accept()方法等待客户端的连接
            System.out.println("已启动");
            while (true){
                Socket accept = sever.accept();
                System.out.println("有客户端连接");//运行该方法时,会进入阻塞状态,等待客户端连接后继续往下连接

                //接收客户端的消息 获取输出流
                InputStream in = accept.getInputStream();
                byte[] b=new byte[2048];
                int len=in.read(b);
                System.out.println("客户端:"+new String(b,0,len));

                //给客户端发消息 获取输出流
                OutputStream out = accept.getOutputStream();

                System.out.println("请输入消息:");
                String mes = scanner.nextLine();
                out.write(mes.getBytes());

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(2)客户端

public class Client {
    public static void main(String[] args) {
        //创建一个客户端套接字对象,并且指定要连接的服务端的IP和端口号
        try {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                Socket s = new Socket("localhost", 8080);
                //给服务端发信息 输出流
                OutputStream os = s.getOutputStream();
                System.out.println("输入消息");
                String mes = scanner.nextLine();
                os.write(mes.getBytes());

                //接收客户端发来的消息 输入流
                InputStream in = s.getInputStream();
                byte[] b = new byte[2048];
                int len=in.read(b);
                System.out.println("客户端:"+new String (b,0,len));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
(1)服务端 public class Server { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); //创建一个服务端的套接字的对象,并指定端口号 ServerSocket sever = new ServerSocket(8080);//8080表示程序运行的唯一标识 //调用accept()方法等待客户端的连接 System.out.println("已启动"); while (true){ Socket accept = sever.accept(); System.out.println("有客户端连接");//运行该方法时,会进入阻塞状态,等待客户端连接后继续往下连接 //接收客户端的消息 获取输出流 InputStream in = accept.getInputStream(); byte[] b=new byte[2048]; int len=in.read(b); System.out.println("客户端:"+new String(b,0,len)); //给客户端发消息 获取输出流 OutputStream out = accept.getOutputStream(); System.out.println("请输入消息:"); String mes = scanner.nextLine(); out.write(mes.getBytes()); } } catch (IOException e) { e.printStackTrace(); } } } (2)客户端 public class Client { public static void main(String[] args) { //创建一个客户端套接字对象,并且指定要连接的服务端的IP和端口号 try { Scanner scanner = new Scanner(System.in); while (true) { Socket s = new Socket("localhost", 8080); //给服务端发信息 输出流 OutputStream os = s.getOutputStream(); System.out.println("输入消息"); String mes = scanner.nextLine(); os.write(mes.getBytes()); //接收客户端发来的消息 输入流 InputStream in = s.getInputStream(); byte[] b = new byte[2048]; int len=in.read(b); System.out.println("客户端:"+new String (b,0,len)); } } catch (IOException e) { e.printStackTrace(); } } }
经验分享 程序员 微信小程序 职场和发展