JAVA网络编程之TCP实例

TCP简介

TCP协议是一种处于传输层的协议,主要是通过用户之间的交互并相互确认来建立连接并实现数据传输的。

实现代码:

Client类

public class TcpClient {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream out = null;
        try {
            //获取服务端ip和端口
            InetAddress ip = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //创建连接用于发送信息
            socket = new Socket(ip, port);
            //获取一个输出流并输出消息
            out = socket.getOutputStream();
            out.write("Hello World".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Server类

public class TcpServer {
    public static void main(String[] args) {
        ServerSocket serversocket = null;
        Socket socket = null;
        InputStream in = null;
        ByteArrayOutputStream baos = null;
        try {
            //创建连接用于接收信息
            serversocket = new ServerSocket(9999);
            socket = serversocket.accept();
            //获取一个输入流获取信息
            in = socket.getInputStream();
            //创建一个管道流
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            //将得到的数据写入管道流并输出
            while((len=in.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in!=null){
                try {
                    in.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();
                }
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值