socket

https://www.cnblogs.com/yiwangzhibujian/p/7107785.html  

 

 

package socket;  

  

import java.io.InputStream;  

import java.net.ServerSocket;  

import java.net.Socket;  

  

/** 

 * Created by Administrator on 2018/3/12. 

 */  

public class SocketServer {  

    public static void main(String[] args) throws Exception {  

        // 监听指定的端口  

        int port = 55533;  

        ServerSocket server = new ServerSocket(port);  

  

        // server将一直等待连接的到来  

        System.out.println("server将一直等待连接的到来");  

        Socket socket = server.accept();  

        // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取  

        InputStream inputStream = socket.getInputStream();  

        byte[] bytes = new byte[1024];  

        int len;  

        StringBuilder sb = new StringBuilder();  

        while ((len = inputStream.read(bytes)) != -1) {  

            //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8  

            sb.append(new String(bytes, 0, len,"UTF-8"));  

        }  

        System.out.println("get message from client: " + sb);  

        inputStream.close();  

        socket.close();  

        server.close();  

    }  

}  

  

  

package socket;  

  

import java.io.OutputStream;  

import java.net.Socket;  

  

/** 

 * Created by Administrator on 2018/3/12. 

 */  

public class SocketClient {  

    public static void main(String args[]) throws Exception {  

        // 要连接的服务端IP地址和端口  

        String host = "127.0.0.1";  

        int port = 55533;  

        // 与服务端建立连接  

        Socket socket = new Socket(host, port);  

        // 建立连接后获得输出流  

        OutputStream outputStream = socket.getOutputStream();  

        String message="你好  yiwangzhibujian";  

        socket.getOutputStream().write(message.getBytes("UTF-8"));  

        outputStream.close();  

        socket.close();  

    }  

}  

  

  

  

    public static  void server5()throws Exception {  

// 监听指定的端口  

        int port = 55533;  

        ServerSocket server = new ServerSocket(port);  

        // server将一直等待连接的到来  

        System.out.println("server将一直等待连接的到来");  

  

        //如果使用多线程,那就需要线程池,防止并发过高时创建过多线程耗尽资源  

        ExecutorService threadPool = Executors.newFixedThreadPool(3);  

  

        while (true) {  

            Socket socket = server.accept();  

  

            Runnable runnable=()->{  

                try {  

                    // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取  

                    InputStream inputStream = socket.getInputStream();  

                    byte[] bytes = new byte[1024];  

                    int len;  

                    StringBuilder sb = new StringBuilder();  

                    while ((len = inputStream.read(bytes)) != -1) {  

                        // 注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8  

                        sb.append(new String(bytes, 0, len, "UTF-8"));  

                    }  

                    Thread.sleep(5000);  

                    System.out.println("get message from client: " + sb);  

                    inputStream.close();  

                    socket.close();  

                } catch (Exception e) {  

                    e.printStackTrace();  

                }  

            };  

            threadPool.submit(runnable);  

        }  

    }  

  

  

  

  

    public static  void clent3()throws Exception {  

        // 要连接的服务端IP地址和端口  

        String host = "127.0.0.1";  

        int port = 55533;  

        // 与服务端建立连接  

        Socket socket = new Socket(host, port);  

        // 建立连接后获得输出流  

        OutputStream outputStream = socket.getOutputStream();  

        String message = "你好  yiwangzhibujian";  

        //首先需要计算得知消息的长度  

        byte[] sendBytes = message.getBytes("UTF-8");  

        //然后将消息的长度优先发送出去  

        outputStream.write(sendBytes.length >> 8);  

        outputStream.write(sendBytes.length);  

        //然后将消息再次发送出去  

        outputStream.write(sendBytes);  

        outputStream.flush();  

        //==========此处重复发送一次,实际项目中为多个命名,此处只为展示用法  

        message = "第二条消息";  

        sendBytes = message.getBytes("UTF-8");  

        outputStream.write(sendBytes.length >> 8);  

        outputStream.write(sendBytes.length);  

        outputStream.write(sendBytes);  

        outputStream.flush();  

        //==========此处重复发送一次,实际项目中为多个命名,此处只为展示用法  

        message = "the third message!";  

        sendBytes = message.getBytes("UTF-8");  

        outputStream.write(sendBytes.length >> 8);  

        outputStream.write(sendBytes.length);  

        outputStream.write(sendBytes);  

  

        outputStream.close();  

        socket.close();  

    }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值