TCP

tcp编程

定义一个客户端

//客户端
public class Socke {
    public static void main(String[] args){
        Socket socket = null;
        OutputStream os=null;
        try {
            //拿到一个socket,因为服务端定义了一个serverSocket
            //InetAddress.getByName拿到ip地址(不一定是自己电脑的)
            //127.0.0.1是自己电脑的ip
            //Socket构造方法中传参为ip和端口号
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9595);
            String str="你好,服务端";
             os = socket.getOutputStream();
            os.write(str.getBytes());

        } catch (IOException e) {
            e.printStackTrace();

        }finally {

            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           if(socket!=null){
               try {
                   socket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }
}

定义一个服务端

//服务端
public class ServerSocke {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket=null;
        InputStream is=null;
        ByteArrayOutputStream bos=null;
        try {
            //给服务器一个地址(端口号),用于客户端访问
            serverSocket = new ServerSocket(9595);
             socket = serverSocket.accept();//等待客户端连接(阻塞)socket就是客户端的socket
             is = socket.getInputStream();//输入流

            bos = new ByteArrayOutputStream();//管道流
            byte[] buffer = new byte[1024];//这样用比较浪费资源,可能用不到1024
            int len;
            while ((len=is.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
            System.out.println(bos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭资源时,先开后关
            //判断为空,保证不出现空指针异常
            if (bos!=null) {
                try {
                    bos.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();
                }
            }
        }
    }
}
//注意 :先运行服务端,后运行客户端
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值