Java:1.获取IP地址;2.TCP实现聊天;3.TCP实现文件上传;4.UDP实现消息发送。

1.获取IP地址

import java.net.InetAddress;
import java.net.UnknownHostException;

//IP测试
public class InetAddressTest {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            //查询本机IP地址
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress2 = InetAddress.getByName("localhost");
            System.out.println(inetAddress2);
            InetAddress inetAddress3 = InetAddress.getLocalHost();
            System.out.println(inetAddress3);
            //查询百度的Ip地址
            InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress4);
        } catch (UnknownHostException e) {
    
    
            e.printStackTrace();
        }
    }
}

2.TCP实现聊天

服务器端:

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

public class TcpServerDemo01 {
    
    
    public static void main(String[] args) {
    
    
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
    
    
            //服务器端口
            serverSocket = new ServerSocket(8080);
            //等待客户端连接
            socket = serverSocket.accept();
            //读取客户端的消息
            is = socket.getInputStream();
            //管道流
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
    
    
                baos.write(buffer, 0, len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //关闭资源
            if (is != null) {
    
    
                try {
    
    
                    is.close();
                } catch (IOException e1) {
    
    
                    e1.printStackTrace();
                }
            }
            if (baos != null) {
    
    
                try {
    
    
                    baos.close();
                } catch (IOException e1) {
    
    
                    e1.printStackTrace();
                }
            }
            if (serverSocket != null) {
    
    
                try {
    
    
                    serverSocket.close();
                } catch (IOException e1) {
    
    
                    e1.printStackTrace();
                }
            }
            if (socket != null) {
    
    
                try {
    
    
                    socket.close();
                } catch (IOException e1) {
    
    
                    e1.printStackTrace();
                }
            }
        }
    }
}

客户端:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClientDemo01 {
    
    
    public static void main(String[] args) {
    
    
        Socket socket = null;
        OutputStream os = null;
        try {
    
    
            //需要知道服务器的地址和端口号
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");
            int port = 8080;
            //创建Socket连接
            socket = new Socket(serverIp, port);
            //发送消息,IO流
            os = socket.getOutputStream();
            os.write("你好!".getBytes());
        } catch (Exception 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();
                }
            }
        }
    }
}

运行时先启动服务器端,再启动客户端。

3.TCP实现文件上传

服务器端:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo02 {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //创建服务
        ServerSocket serverSocket = new ServerSocket(8080);
        //监听客户端的连接
        Socket socket = serverSocket.accept();
        //获取输入流
        InputStream is = socket.getInputStream();
        //文件输出
        FileOutputStream fos = new FileOutputStream(new File("D:\\Test\\receive.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
    
    
            fos.write(buffer, 0, len);
        }
        //通知客户端已接收完毕
        OutputStream os = socket.getOutputStream();
        os.write("服务器已接收完毕".getBytes());
        //关闭资源
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}

客户端:

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

public class TcpClientDemo02 {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //创建Socket连接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8080);
        //创建输出流
        OutputStream os = socket.getOutputStream();
        //读取文件
        FileInputStream fis = new FileInputStream(new File("D:\\Test\\海绵宝宝.jpg"));
        //写出文件
        byte[] buffer1 = new byte[1024];
        int len1;
        while ((len1 = fis.read(buffer1)) != -1) {
    
    
            os.write(buffer1, 0, len1);
        }
        //通知服务器已传输完毕
        socket.shutdownOutput();
        //确定服务器接收完毕后断开连接
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[1024];
        int len2;
        while ((len2 = is.read(buffer2)) != -1) {
    
    
            baos.write(buffer2, 0, len2);
        }
        System.out.println(baos.toString());
        //关闭资源
        baos.close();
        is.close();
        fis.close();
        os.close();
        socket.close();

    }
}

运行时先启动服务器端,再启动客户端。

4.UDP实现消息发送

发送方

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class UdpSendDemo01 {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //建立Socket
        DatagramSocket socket = new DatagramSocket();
        //写入数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
    
    
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            //打包数据
            DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 8080));
            //发送数据
            socket.send(packet);
            if (data.equals("bye")) {
    
    
                break;
            }

        }
        //关闭资源
        socket.close();
    }
}

接收方:

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpReciveDemo01 {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //开放端口
        DatagramSocket socket = new DatagramSocket(8080);
        //接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        //阻塞接受
        socket.receive(packet);

        System.out.println(new String(packet.getData(), 0, packet.getLength()));
        //资源关闭
        socket.close();
    }
}

运行时先启动接收方,再启动发送方。