欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java网络编程学习笔记

程序员文章站 2022-07-11 20:32:21
...

InetAddress

知道有这么一个类InetAddress
此类表示Internet协议(IP)地址。
IP地址是由IP使用的32位或128位无符号数字,构建UDP和TCP协议的低级协议

UDP 发送

UDP 发送

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 20:47
 */
public class UdpSend {
    public static void main(String[] args) throws Exception{
        //1.创建UDP服务,通过DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        //2.确定数据,封装成数据包
        byte[] buf = "udp is not reliable data transfer protocol".getBytes();
        DatagramPacket dp = new DatagramPacket(buf,buf.length, InetAddress.getByName("192.168.1.210"),9999);
        //3.通过socket服务,将数据包发出去
        ds.send(dp);
        //4.关闭资源
        ds.close();
    }
}

UDP接收

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 20:53
 */
public class UdpReceiver {

    public static void main(String[] args) throws Exception{
        //1.创建udp socket服务
        DatagramSocket ds = new DatagramSocket(9999);
        //2.定义数据包,用于存储数据
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf,buf.length);
        //3.通过服务的receiver方法将数据存入数据包中
        ds.receive(dp);
        //4.通过数据包的方法获取其中的数据
        String ip = dp.getAddress().getHostAddress();
        String data = new String(dp.getData(),0,dp.getLength());
        int port = dp.getPort();
        System.out.println("ip:"+ip+" data:"+data+" port:"+port);
        //5.关闭资源
    }
}

结果
Java网络编程学习笔记

UDP控制台输入单边通讯

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 21:10
 */
public class UdpSend2 {
    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while( (line=br.readLine())!=null){
            if("886".equals(line)){
                break;
            }
            byte[] buf = line.getBytes();
            DatagramPacket dp = new DatagramPacket(buf,buf.length, InetAddress.getByName("192.168.1.210"),10000);
            ds.send(dp);
        }
        ds.close();
    }
}

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 21:10
 */
public class UdpReceiver2 {
    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket(10000);
        while(true){
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            String ip = dp.getAddress().getHostAddress();
            String data = new String(dp.getData(),0,dp.getLength());
            int port = dp.getPort();
            System.out.println("ip:"+ip+" data:"+data+" port:"+port);
        }
    }
}

结果
Java网络编程学习笔记
Java网络编程学习笔记

ChatDemo.java

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 21:29
 */
public class ChatDemo {
    public static void main(String[] args) throws Exception{
        DatagramSocket sendSocket = new DatagramSocket();
        DatagramSocket receSocket = new DatagramSocket(9999);
        new Thread(new Send(sendSocket)).start();
        new Thread(new Receiver(receSocket)).start();
    }
}

class Send implements Runnable{
    private DatagramSocket ds;
    public Send(DatagramSocket socket){
        this.ds =socket;
    }

    @Override
    public void run() {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while((line=br.readLine())!=null){
                if("bye".equals(line)){
                    break;
                }
                byte[] buf = line.getBytes();
                DatagramPacket dp = new DatagramPacket(buf,buf.length, InetAddress.getByName("192.168.1.210"),9999);
                ds.send(dp);
            }
        }catch (Exception e){
            throw new RuntimeException("发送失败");
        }
    }
}

class Receiver implements Runnable{
    private DatagramSocket ds;
    public Receiver(DatagramSocket socket){
        this.ds =socket;
    }

    @Override
    public void run() {
        try{
            while(true){
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacket(buf,buf.length);
                ds.receive(dp);
                String ip = dp.getAddress().getHostAddress();
                String data = new String(dp.getData(),0,dp.getLength());
                System.out.println(ip+":"+data);
            }
        }catch (Exception e){
            throw new RuntimeException("接收失败");
        }
    }
}

Java网络编程学习笔记

TCP 发送

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 22:04
 */
public class TCPServer {
    public static void main(String[] args) throws Exception{
        ServerSocket ss = new ServerSocket(9999);
        Socket s = ss.accept();
        InputStream in = s.getInputStream();
        byte[] buf = new byte[1024];
        int len = in.read(buf);
        String ip = s.getInetAddress().getHostAddress();
        System.out.println(ip+"......connected");
        System.out.println(new String(buf,0,len));
        s.close();
        ss.close();
    }
}

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 21:59
 */
public class TCPClient {
    public static void main(String[] args) throws Exception{
        Socket s = new Socket("192.168.1.210",9999);
        OutputStream out = s.getOutputStream();
        out.write("tcp is reliable transfer protocol".getBytes());
        s.close();
    }
}

结果
Java网络编程学习笔记

TCP Sokect一来一回

package blog.fundmental.javaNet.demo;

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

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 22:12
 */
public class TCPServer2 {
    public static void main(String[] args) throws Exception{
        ServerSocket ss = new ServerSocket(9999);
        Socket s = ss.accept();
        InputStream in = s.getInputStream();
        String ip = s.getInetAddress().getHostAddress();
        System.out.println(ip+"...connected");
        byte[] buf = new byte[1024];
        int len = in.read(buf);
        System.out.println(new String(buf,0,len));

        OutputStream out = s.getOutputStream();
        out.write("服务端对客户端说,你为啥不知道呀".getBytes());
        s.close();
        ss.close();
    }
}

package blog.fundmental.javaNet.demo;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author *cruder
 * @version 1.0
 * @since 2020/12/1 22:09
 */
public class TcpClient2 {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("192.168.1.210",9999);
        OutputStream out = s.getOutputStream();
        out.write("客户端对服务端说什么也不知道".getBytes());

        InputStream in = s.getInputStream();
        byte[] buf = new byte[1024];
        int len = in.read(buf);
        System.out.println(new String(buf,0,len));
        s.close();
    }
}

结果
Java网络编程学习笔记
Java网络编程学习笔记

相关标签: Java基础