zoukankan      html  css  js  c++  java
  • 37.3 net--TcpDemo1 大小写转换

    需求:使用TCP协议发送数据,并将接收到的数据转换成大写返回

    启动方式:先打开服务端,再打开客户端

    客户端

    package day35_net_网络编程.tcp传输;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    /*
         需求:使用TCP协议发送数据,并将接收到的数据转换成大写返回
    
         客户端发出数据
         服务端接收数据
         服务端转换数据
         服务端发出数据
         客户端接收转换后的数据
    
     */
    public class Cast_ClientDemo {
        public static void main(String[] args) throws IOException {
            Socket s = new Socket(InetAddress.getByName("MININT-79LU01A"),8880);
    
            //发出数据
            OutputStream os = s.getOutputStream();
            byte[] bts = "hello ,tcp cast".getBytes();
            os.write(bts);
    
            //接收转换后的数据
            InputStream is = s.getInputStream();
            byte[] upbts = new byte[1024];
            int len = is.read(upbts);
    
            String upstr = new String(upbts,0,len);
            System.out.println(upstr);
    
    
            //释放资源(Socket)
            s.close();
    
        }
    }

    服务端

    package day35_net_网络编程.tcp传输;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    /*
    服务端接收数据
    服务端转换数据
    服务端发出数据*/
    public class Cast_ServerDemo {
        public static void main(String[] args) throws IOException {
            ServerSocket ss = new ServerSocket(8880);
    
            Socket s = ss.accept();//阻塞
    
            //接收数据
            InputStream is = s.getInputStream();
            byte[] bts = new byte[1024];
            int len = is.read(bts);
    
            String str = new String(bts,0,len);
            System.out.println(str);
    
            //转换数据
            String upstr = str.toUpperCase();
    
            //发出数据
            OutputStream os = s.getOutputStream();
            os.write(upstr.getBytes());
    
    
            //释放资源(Socket)
            s.close();
        }
    }

    输出

  • 相关阅读:
    CodeForces gym Nasta Rabbara lct
    bzoj 4025 二分图 lct
    CodeForces 785E Anton and Permutation
    bzoj 3669 魔法森林
    模板汇总——快读 fread
    bzoj2049 Cave 洞穴勘测 lct
    bzoj 2002 弹飞绵羊 lct裸题
    HDU 6394 Tree 分块 || lct
    HDU 6364 Ringland
    nyoj221_Tree_subsequent_traversal
  • 原文地址:https://www.cnblogs.com/longesang/p/11375712.html
Copyright © 2011-2022 走看看