TCP网络编程

1.建立一个客户端和一个服务端(对应client()和server())

2.生成socket需要服务端的端口号和ip地址造出服务端的ip地址和端口号.

这里因为是同一台主机,服务端和输出端的IP地址相同,通过inetAddress.getLocalHost()获取IP地址,端口号可显示指定

3.运用socket,getInputStream/getOutputStream获取socket的输出/输入流

4.创建读取本地文件的流(FileInputStream/FileOutputStream)用于读取/输出本地文件

5.进行读取/写入操作

6.关流

7.用Serversocket.accept方法监听与它进行连接的客户端,并获取socket对象;生成ServerSocket对象时需要显示指定服务端的端口号。

8.读取,写入操作的流程同上。

特别注意:若客户端与输入端均采用了read()操作,需要在输入端显示指定socket.shutdownOutput/shutdownInput,因为read是阻塞方法,如不显示指定,则不会结束,因此客户端和服务端都会停留在while循环中进入死循环

例1.客户端和服务端之间收发信息

客户端-->服务端:“你好”

服务端-->客户端:“收到”

public class TCPTest {
    //客户端发送给服务端一句话,并显示在控制台上
    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        ByteArrayOutputStream baos = null;
        try {
            //这里需要指明服务端的IP地址,这里IP地址已查询出,端口号假设是8899
            InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
            //根据ip地址和端口号创建socket对象来完成网络编程,服务端的端口号要与socket中一致
            socket = new Socket(inetAddress, 8899);
            //获取socket的字节输出流
            os = socket.getOutputStream();
            //写入操作
            //注意write要传入byte[]数组,使用getBytes将String转化为byte数组
            os.write("你好".getBytes());
            socket.shutdownOutput();//很重要,必须要写,如果不写,服务端的read方法将不会终止,会一直在while循环中
            InputStream is1 = socket.getInputStream();
            int len;
            byte [] b=new byte[5];
             baos=new ByteArrayOutputStream();//防乱码
            while ((len=is1.read(b))!=-1){
                baos.write(b,0,len);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
            //关流和socket
        } finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    //服务端
    @Test
    public void server(){
        //服务端的socket套接字
        ServerSocket serverSocket = null;
        //客户端的socket套接字
        Socket socket= null;
        InputStream is = null;
        ByteArrayOutputStream baos= null;
        OutputStream os2=null;
        try {
            //创建服务端Socket对象,并指明端口号
            serverSocket = new ServerSocket(8899);
            //监听与socket建立连接的对象,并接受它
            socket = serverSocket.accept();
            //获取输入流
            is = socket.getInputStream();
            //输出操作
            //使用byte可能会存在将一个汉字分成两部分输出的情况故采用yteArrayOutputStream
            baos = new ByteArrayOutputStream();
            int len;
            byte [] b=new byte[5];
            while ((len=is.read(b))!=-1){
                baos.write(b,0,len);
            }
            //输出接收的内容
            System.out.println(baos.toString());
            //将“收到”写入输出流中,返回给客户端
            os2 = socket.getOutputStream();
            os2.write("收到!".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
            //关流和socket
        } finally {
            if (baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    os2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

例2.客户端给服务端发送图片

public class TCPTest2 {
    //客户端向服务端传输一个文件
    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            //通过服务端ip地址和端口号,生成socket对象
            InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress,8091);
            os = socket.getOutputStream();
            //读入图片
            fis = new FileInputStream(new File("C:\\Users\\X\\Desktop\\tt\\111.png"));
            int len;
            byte [] b=new byte[1024];
            while ((len=fis.read(b))!=-1){
                os.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //关流和socket
        } finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //服务端
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos= null;
        try {
            serverSocket = new ServerSocket(8091);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            //将客户端发来的图片写到另一个文件里
            fos = new FileOutputStream(new File("C:\\Users\\X\\Desktop\\tt\\222.png"));
            int len;
            byte [] b=new byte[1024];
            while ((len=is.read(b))!=-1){
                fos.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值