JAVA#TCP_2

客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送“已接收”给客户端
 public void Client_1(){
        Socket socket= null;
        OutputStream outputStream=null;
        InputStream inputStream=null;
        try {
            socket=new Socket(InetAddress.getByName("175.159.70.105"),9191);//创建socket对象,通过构造器指明服务器的IP地址,端口号
            outputStream=socket.getOutputStream();//发送数据
            outputStream.write("客户端".getBytes());
            socket.shutdownOutput();//显示告诉服务端发送完毕
            //
            inputStream=socket.getInputStream();
            byte[] b=new byte[15];
            int len;
            while ((len=inputStream.read(b))!=-1){
                String s=new String(b,0,len);
                System.out.println(s);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try{
                    socket.close();
                }catch (IOException E){
                    E.printStackTrace();
                }

            }

        }

    }
    @Test
    public void server_1(){
        ServerSocket serverSocket=null;
        Socket s=null;
        InputStream is=null;
        OutputStream outputStream=null;
        try {
            serverSocket=new ServerSocket(9191);
            s=serverSocket.accept();
            is=s.getInputStream();
            byte[] b=new byte[15];
            int len;
            while((len=is.read(b))!=-1){
                String str=new String(b,0,len);
                System.out.println(str);
            }
            //
            outputStream=s.getOutputStream();
            outputStream.write("已接收".getBytes());
        }catch (IOException E){
            E.printStackTrace();
        }finally {
            if(outputStream!=null){
                try {
                    outputStream.close();
                }catch (IOException E){
                    E.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                }catch (IOException E){
                    E.printStackTrace();
                }
            }
            if(s!=null){
                try {
                    s.close();
                }catch (IOException E){
                    E.printStackTrace();
                }
            }
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                }catch (IOException E){
                    E.printStackTrace();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Iverson941112/article/details/86480289