网络编程

网络编程

ip地址

需导入import java.net.InetAddress;这个包

try{
    //查面本机地城
	InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
    System.out.printIn(inetAddress1);
    InetAddress inetAddress3 = InetAddress.getByName("localhost");
    System.out.printIn(inetAddress3);
    InetAddress inetAddress4 = InetAddress.getLocalHost();
    System.out.printIn(inetAddress4);
//查询网站ip地址
    InetAddress inetAddress2 = InetAddress.getByName( "www,baidu,com");
    System.out.println(inetAddress2);
//常用方法
//System.out.println(inetAddress2.getAddress());
    System.out.println(inetAddress2.getCanonicalHostName()); //规范的名字
    System.out.println(inetAddress2.getHostAddress()); //ip
    System.out.println(inetAddress2.getHostName()); //域名,或者自己电脑的名字
}catch(UnknownHostException e){
    e.printStackTrace();
}

端口

InetSocketAddress socketAddress = new InetSocketAddress("127,0.0.1",8080);
InetSocketAddress socketAddress2 = new InetSocketAddress( "localhost", 8080);
System.out.println(socketAddress);
System.out.printIn(socketAddress2);
System.out.printIn(socketAddress.getAddress());
System.out.println(socketAddress.getHostName()); //地址
System.out.println(socketAddress.getPort()); //端口

TCP实现聊天

客户端

package com.zzy;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
// 客户端
public class tcpClient{
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try{
            //1,些知道服务器的地址,端口号
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2,创建一个socket连接
            socket = new Socket(serverIP,port);
            // 3,发这消息 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();
                }
            }
        }
    }
}

服务端

package com.zzy;
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;
// 服务端
public class tcpServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //建立一个地址
            serverSocket = new ServerSocket(9999);
            while (true) {
                //2. 等待客户端连接过水
                socket = serverSocket.accept();
                //3.读收客广端的消息
                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 (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 (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

UDP发送包

发送端

package com.zzy.udp;

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

public class udpClient {
    public static void main(String[] args) throws Exception{
        //建立一个Socket
        DatagramSocket socket = new DatagramSocket();
        //建立一个包
        String msg = "这是一个包";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        //封装包,(数据,数据长度,发送地址与端口)
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
        //发送包
        socket.send(packet);
        //关闭流
        socket.close();
    }
}

接收端

package com.zzy.udp;

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

public class udpReceive {
    public static void main(String[] args) throws Exception{
    //开放端口
    DatagramSocket socket = new DatagramSocket(9090);
    //接收数据包
    byte[] buffer = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
    socket.receive(packet);//阻塞接收
    //输出数据包
    System.out.println(packet.getAddress().getHostAddress());
    System.out.println(new String(packet.getData(),0,packet.getLength()));
    //关闭流
    socket.close();
    }
}

URL

例:www.baidu.com

协议://ip地址:端口/项目名/资源

posted @   蝶梦生  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示