socket编程(java实现)

socket编程

socket,又称套接字,是在不同的进程间进行网络通讯的一种协议、约定或者说是规范。
对于socket编程,它更多的时候像是基于TCP/UDP等协议做的一层封装或者说抽象,是一套系统所提供的用于进行网络通信相关编程的接口。

socket编程基本流程

在这里插入图片描述

socket编程(java实现)

服务端使用ServerSocket绑定IP和端口,使用Accept监听端口是否有客户端发送连接请求,一旦有客户端发送连接请求,
服务端就回送连接信息,正式建立连接。Server端和Client端都可以通过Send,Write等方法与对方通信。

服务器:

public class TcpSocketServer {

  public static void main(String[] args) {
        try {
            // 创建服务端socket 绑定端口
            ServerSocket serverSocket = new ServerSocket();
            //绑定ip
            serverSocket = new ServerSocket(8088, 10, InetAddress.getByName("192.168.0.110"));
            // 创建客户端socket 用户下面接收客户端socket对象
            Socket socket = new Socket();
            System.out.println("等待客户端连接...");
            //循环监听等待客户端的连接
            while(true){
                // 监听客户端  没有接受到数据才会停在此处 接受到往下执行
                socket = serverSocket.accept();
               //发送内容实现线程的创建
                ServerThread thread = new ServerThread(socket);  
                thread.start();
                //获取客户端的ip
                InetAddress address=socket.getInetAddress();
                System.out.println("当前链接的客户端的IP:"+address.getHostAddress());
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
}
    
}



public class ServerThread extends Thread{

    private Socket socket = null;

    public ServerThread(Socket socket) {

        this.socket = socket;
    }
    public void run() {
        InputStream is=null;
        InputStreamReader isr=null;
        BufferedReader br=null;
        OutputStream os=null;
        PrintWriter pw=null;
        try {
            is = socket.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String info = null;
            while((info=br.readLine())!=null){
                System.out.println("客户端:"+info);
            }
            //非关闭连接 仅关闭一方的发送状况
            socket.shutdownInput();
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("服务器欢迎你1");
            pw.flush();
        } catch (Exception e) {
            // TODO: handle exception
        } finally{
            //关闭资源
            try {
                if(pw!=null)
                    pw.close();
                if(os!=null)
                    os.close();
                if(br!=null)
                    br.close();
                if(isr!=null)
                    isr.close();
                if(is!=null)
                    is.close();
                if(socket!=null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

客户端:

public class TcpSocketClient {
    public static void client() throws InterruptedException {
        try {
            // 和服务器创建连接
            Socket socket = new Socket("192.168.0.111", 8088);
            // 要发送给服务器的信息
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.write("状态已改变");
            //flush方法是用于将输出流缓冲的数据全部写到目的地。
            //所以一定要在关闭close之前进行flush处理,即使PrintWriter有自动的flush清空功能
            pw.flush();
            socket.shutdownOutput();
            // 从服务器接收的信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while ((info = br.readLine()) != null) {
                System.out.println("我是客户端,服务器返回信息:" + info);
            }
            br.close();
            is.close();
            os.close();
            pw.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码源自:https://blog.csdn.net/weixin_43784880/article/details/105963960

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值