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();
            }
        }
    }

}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值