IP

ip

//获得本机地址
InetAddress address1 = InetAddress.getByName("127.0.0.1");
//获得网络地址
InetAddress address = InetAddress.getByName("www.baidu.com");
System.out.println(address.getAddress());
System.out.println(address.getHostName());
System.out.println(address.getHostAddress());

端口

InetSocketAddress socketAddress = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress.getAddress());

TCP实现聊天

方法:将读取的数据(字节数据)存入字节数组中byte[],然后用输出流将其输出byteArrayOutputStream.write(byte,0,len) len是一共有多少个字节(java中的中文字符是3字节,英文字符是1字节),因为byteArrayOutputStream输出管道流以IO流的方式来完成对字节数组的内容的读写,让sys可以正常输出

服务器端

ServerSocket server =null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream bo = null;

try {
    //创建端口为9095的服务器
    server = new ServerSocket(9095);
    //监听客户端连接
    socket = server.accept();
    //创建输入流
    is = socket.getInputStream();

    //创建输出管道流(向我们的IDEA输出),注意,如果服务器已经关闭但是对方还在发消息则会报错
    bo = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    //判断获得的字节数据长度,若大于0,则写入管道流中
    while ((len = is.read(buffer)) > 0){
       	//将buffer中从0-到len的数据写入输出流
        bo.write(buffer,0,len);
    }
    //将得到的字节数据从输出管道流中输出
    System.out.println(bo.toString());

} catch (Exception e) {
    e.printStackTrace();
}finally {
    try {
        bo.close();
        is.close();
        socket.close();
        server.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

客户端

Socket socket = null;

OutputStream os = null;

try {
    //创建socket,用于连接连接目标IP和对应的端口
    socket = new Socket("127.0.0.1", 9095);
    //创建输出流
    os = socket.getOutputStream();
    //将字符串变为字节输出
    os.write("您好,这里是服务器".getBytes());

} catch (Exception e) {
    e.printStackTrace();
}finally {
    try {
        os.close();
        socket.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

文件传输

要传输什么就用什么输入输出流

服务器

ServerSocket serve = null;;
Socket socket = null;;
InputStream is = null;
byte[] bytes = new byte[1024];
FileOutputStream fileOutputStream = null;

try {
    serve = new ServerSocket(9095);
    socket = serve.accept();
    is = socket.getInputStream();

    fileOutputStream = new FileOutputStream("永恩1.jpg");
    int len = is.read(bytes);
    //写入输出流中
    fileOutputStream.write(bytes,0,len);


} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        is.close();
        socket.close();
        serve.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }finally {
        try {
            fileOutputStream.close();
            is.close();
            socket.close();
            serve.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

客户端

Socket socket = null;
OutputStream os = null;
FileInputStream fileInputStream = null;
byte[] bytes = new byte[1024];

try {
    socket = new Socket("127.0.0.1", 9095);
    os = socket.getOutputStream();

    //创建文件输入流,从src同级目录中提取永恩.jpg
    fileInputStream = new FileInputStream("永恩.jpg");
    int len = fileInputStream.read(bytes);
    os.write(bytes,0,len);


} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        os.close();
        fileInputStream.close();
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
posted @   Geek李  阅读(443)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示