Java中网络编程ServerSocket的用法

Java中网络编程ServerSocket的用法
Tcp/Ip参考模型    传输控制协议 
应用层---->传输层----->网络层---->物理层+数据链路层

端口号范围是0-65536 ,一般0-1023是系统上的,已经存在被占用的情况,所以我们一般不用0-1023,我们用户用的都是1024-65535之间的端口号,

netstat -ano | findstr 端口号   这个命令一般是查看当前端口号是否被占用

 

tasklisk | finder 端口号 这么命令是查看端口号对应的经常名字 

服务器ServerSocket

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) {
        ServerSocket ss = null;
        DataInputStream dis = null;
        DataOutputStream dos = null;
        Scanner input = new Scanner(System.in);
        try {
            ss = new ServerSocket(2018);
            // Socket s = ss.accept(); 单线程
            // dis = new DataInputStream(s.getInputStream());
            // dos = new DataOutputStream(s.getOutputStream());
            // while(true){
            // String mes = dis.readUTF();
            // System.out.println("客户端说:" + mes);
            //
            // System.out.println("回复客户端:");
            // String replay = input.nextLine();
            // dos.writeUTF(replay);
            // }
            //
            while (true) {
                new Thread(new ReciveClient(ss.accept())).start();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                dos.close();
                ss.close();
                dis.close();
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

class ReciveClient implements Runnable {
    DataInputStream dis = null;
    DataOutputStream dos = null;
    Scanner input = new Scanner(System.in);
    
    private Socket socket;

    public ReciveClient(Socket socket) {
        super();
        this.socket = socket;
    }

    public ReciveClient() {
        super();
    }

    @Override
    public void run() {

        // System.out.println(this.socket.getInetAddress().getHostAddress());
        try {
            dis = new DataInputStream(socket.getInputStream());

            dos = new DataOutputStream(socket.getOutputStream());
            while (true) {
                String mes = dis.readUTF();
                System.out.println(socket.getInetAddress().getHostAddress()+"客户端说" + mes);

                System.out.println("回复客户端:"+socket.getInetAddress().getHostAddress()+" :");
                String replay = input.nextLine();
                dos.writeUTF(replay);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

客户端 Socket

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Socket client = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        Scanner sc = new Scanner(System.in);
        
        try {
            client = new Socket("10.25.101.23",6789);
            dos = new DataOutputStream(client.getOutputStream());
            dis = new DataInputStream(client.getInputStream());
            while(true){
                System.out.println("你说:");
                String mes = sc.nextLine();
                dos.writeUTF(mes);
    
                String replay = dis.readUTF();
                System.out.println("服务器回复说:" + replay);
            }
            
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                dis.close();
                client.close();
                dos.close();
                sc.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
 

你可能感兴趣的:(Java高级)