zoukankan      html  css  js  c++  java
  • TCP编程

    Server.java

    package com.zby;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
        private static boolean running = true;
    
        public static void main(String[] args) throws Exception {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("LocalSocketAddress:" + serverSocket.getLocalSocketAddress());
            System.out.println("----------------------------------------------------------");
            while (running) {
                Socket socket = serverSocket.accept();
                System.out.println("connected!");
                System.out.println("RemoteSocketAddress:" + socket.getRemoteSocketAddress());
    
                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                String request = dataInputStream.readUTF();
                System.out.println("request:" + request);
    
                String response = "Hello,Client!";
                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataOutputStream.writeUTF(response);
                System.out.println("response:" + response);
    
                System.out.println("disConnected!");
                System.out.println();
                socket.close();
            }
            serverSocket.close();
        }
    
    }

    Client.java

    package com.zby;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    
    public class Client {
    
        public static void main(String[] args) throws Exception {
            Socket socket = new Socket(InetAddress.getLocalHost(), 8888, InetAddress.getLocalHost(), 7777);
            System.out.println("connected!");
            System.out.println("LocalSocketAddress:" + socket.getLocalSocketAddress());
            System.out.println("RemoteSocketAddress:" + socket.getRemoteSocketAddress());
            System.out.println();
    
            String request = "Hello,Server!";
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.writeUTF(request);
            System.out.println("request:" + request);
    
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            String response = dataInputStream.readUTF();
            System.out.println("response:" + response);
            System.out.println("disConnected!");
            socket.close();
        }
    
    }
  • 相关阅读:
    centos7安装YouCompleteMe,vim打造成C++的IDE
    java循环定时器@Scheduled的使用
    js使用“toFixed( )”保留小数点后两位
    linux小本
    解决Maven资源过滤问题
    在Maven普通项目上添加Web app的支持
    项目中添加lib依赖
    回顾Servlet
    Tomcat 中文乱码
    Initialization failed for 'https://start.spring.io' Please check URL
  • 原文地址:https://www.cnblogs.com/zby9527/p/7651671.html
Copyright © 2011-2022 走看看