TCP 网络编程

TCP 网络编程

package com.sq;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

/**
 * 实现 TCP 的网络编程
 * 
 * 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
 */
public class TCPTest1 {

    // 客户端
    @Test
    public void client() {

        Socket socket = null;
        OutputStream os = null;
        try {
            // 1.创建 Socket 对象,指明服务器端的 ip 和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 8899);

            // 2.获取一个输出流,用于输出数据
            os = socket.getOutputStream();

            // 3.写出数据的操作
            // 发送给 server ,server 显示内容
            os.write("你好,我是客户端mm".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.资源的关闭
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    // 服务端
    @Test
    public void server() {

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1.创建服务器端的 ServerSocket,指明自己的端口号
            ss = new ServerSocket(8899);

            // 2.调用 accept() 表示接收来自于客户端 socket
            socket = ss.accept();

            // 3.获取输入流
            is = socket.getInputStream();

            // 不建议这样写,可能会有乱码
            // byte[] buffer = new byte[1024];
            // int len;
            // while((len = is.read(buffer)) != -1){
            // String str = new String(buffer,0,len);
            // System.out.println(str);
            // }

            // 4.读取输入流中的数据
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            System.out.println(baos.toString());

            // 收到了来自于:127.0.0.1的数据
            System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 5.关闭资源
            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 (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

package com.sq;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

/**
 * 实现 TCP 的网络编程
 * 
 * 例题2:客户端发送文件给服务端,服务端将文件保存在本地。
 */
public class TCPTest2 {

    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;

        try {
            // 1.
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);

            // 2.
            os = socket.getOutputStream();

            // 3.
            fis = new FileInputStream(new File("mikey.jpg"));

            // 4.
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 5.
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;

        try {
            // 1.
            ss = new ServerSocket(9090);

            // 2.
            socket = ss.accept();

            // 3.
            is = socket.getInputStream();

            // 4.
            fos = new FileOutputStream(new File("mikey2.jpg"));

            // 5.
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 6.
            if (fos != null) {
                try {
                    fos.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 (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

package com.sq;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

/**
 * 实现 TCP 的网络编程
 * 
 * 例题3:从客户端发送文件给服务端,服务端保存到本地。
 * 
 * ......并返回“发送成功”给客户端。并关闭相应的连接。
 */
public class TCPTest3 {

    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;

        try {
            // 1.
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);

            // 2.
            os = socket.getOutputStream();

            // 3.
            fis = new FileInputStream(new File("mikey.jpg"));

            // 4.
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }

            // 关闭数据的输出
            socket.shutdownOutput();

            // 5.接收到来自于服务器端的数据,并显示到控制台上
            InputStream is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] bufferr = new byte[20];
            int len1;
            while ((len1 = is.read(bufferr)) != -1) {
                baos.write(buffer, 0, len1);
            }

            System.out.println(baos.toString());

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

    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;

        try {
            // 1.
            ss = new ServerSocket(9090);

            // 2.
            socket = ss.accept();

            // 3.
            is = socket.getInputStream();

            // 4.
            fos = new FileOutputStream(new File("mikey3.jpg"));

            // 5.
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }

            System.out.println("图片传输完成");

            // 6.服务器端给与客户端反馈
            os = socket.getOutputStream();
            os.write("你好,美女,照片我已收到,非常漂亮!".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 7.
            if (fos != null) {
                try {
                    fos.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 (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
/**
 * 课后练习:
 * 
 * 1.服务端读取图片并发送给客户端,客户端保存图片到本地
 * 
 * 2.客户端给服务端发送文本,服务端将文本转成大写在返回给客户端。
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值