Http会话多次通信模拟

客户端代码:
public class TcpClient {

    public static void main(String[] args) throws IOException {
        Socket socket= new Socket(服务端ip地址,端口号);

        OutputStream  os= socket.getOutputStream();//获取输出流
        System.out.println("建立连接");
        os.write("你好服务器".getBytes());
        InputStream is = socket.getInputStream();//获取输入流读取服务端的返回信息
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        String read=new String(bytes,0,len);
        System.out.println(read);
        clientTcp(socket,os,is,read);
    }

    private static void clientTcp(Socket socket, OutputStream os, InputStream is, String read) throws IOException {
        Scanner scanner =null;
        if (read!=null && read.equals("end")){
            scanner.close();
            socket.close();
        }else{
            System.out.println("aa: ");
            scanner = new Scanner(System.in);
            String next=null;
            if (scanner.hasNext() ){
                next = scanner.next();
            }
            os.write(("aa: "+next).getBytes());
            byte[] bytes1 = new byte[1024];
            int leng = is.read(bytes1);
            read = new String(bytes1, 0, leng);
            System.out.println(read);
            clientTcp(socket,os,is,read);
        }
    }
}
服务端代码:
public class TcpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serevr = new ServerSocket(8081);
        Socket socket = serevr.accept();//监听8081端口
        //服务端本身并没有io流,只能使用客户端的流读取和回复
        InputStream is  = socket.getInputStream();//读取客户端信息
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        String read = new String(bytes, 0, len);
        System.out.println(read);
        OutputStream os = socket.getOutputStream();//获取输出流
        os.write("连接成功".getBytes());
        ServerTcp(read, serevr, socket, is, os);
    }
    private static void ServerTcp(String read, ServerSocket serevr, Socket socket,InputStream is,OutputStream os) throws IOException {

             Scanner scanner =null;
            if (read!=null && read.equals("end")){
                scanner.close();
                socket.close();
                serevr.close();
            }else{

                byte[] bytes1 = new byte[1024];
                int read1 = is.read(bytes1);
                read=new String(bytes1,0,read1);
                System.out.println(read);

                System.out.println("bb: ");
                scanner = new Scanner(System.in);
                String next=null;
                if (scanner.hasNext() ) {
                    next = scanner.next();
                }
                os.write(("bb: "+next).getBytes());
                ServerTcp(read,serevr,socket,is,os);
            }

        }
}

功能不是很完善只能实现a方与b方交替发送消息。代码亲测有效

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值