Java学习笔记-基础语法ⅩⅠ-UDP、TCP

网络编程

三要素:IP地址、端口、协议

IP地址:使用ipconfig查看,如果装了VM的话,会有VMnet1、VMnet8和WLAN,net1不能从虚拟机到主机,net8不能从主机到虚拟机,net0即桥接可以当成物理主机,我们只需要使用WLAN即可

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

public class Demo {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getByName("xx.xx.xx.xx");
        byte[] address = inetAddress.getAddress();
        String hostAddress = inetAddress.getHostAddress();
        String hostName = inetAddress.getHostName();
        System.out.println(hostName);
        System.out.println(hostAddress);
//        System.out.println(address);
        for(byte b:address){
            System.out.print(b);
        }
    }
}

使用UDP进行发送数据与接收数据

// 发送数据
import java.io.IOException;
import java.net.*;

public class SendDemo {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();
        byte [] bys = "Hello World!".getBytes();
//        xx.xx.xx.xx
        InetAddress inetAddress = InetAddress.getByName("xx.xx.xx.xx");
        DatagramPacket dp = new DatagramPacket(bys,0,bys.length,inetAddress,10086);
        ds.send(dp);
        ds.close();
    }
}
// 接收数据
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Receive_demo {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        byte [] bys = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bys,bys.length);
        ds.receive(dp);
        byte[] datas = dp.getData();
        String s = new String(datas,0,dp.getLength());
        System.out.println(s);
        ds.close();
    }
}

自己在用windows去发内容给linux时,没问题,但是对计网的IPv4又忘了,明明windows下net8显示末8位是1,但是到了虚拟机上末8位却是128,现在不太清楚,等寒假再复习一下计网

UDP通信程序练习:

  • UDP发送数据:数据来自于键盘录入,直到输入的数据是886,发送数据结束
  • UDP接收数据,因为接收端不知道发送端什么时候发送停止,故采用死循环接收
// 循环发送端
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Udp_send {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket();
        String nextLine;
        while(!"886".equals(nextLine=sc.nextLine())){
            byte [] bys = nextLine.getBytes();
            InetAddress inetAddress = InetAddress.getByName("10.82.210.42");
            DatagramPacket dp = new DatagramPacket(bys,0,bys.length, inetAddress,10085);
            ds.send(dp);

        }
        ds.close();
    }
}
// x
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Udp_receive {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10085);
        while(true){

            byte [] bys = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bys,bys.length);
            ds.receive(dp);
            byte[] data = dp.getData();
            String s = new String(data,0,dp.getLength());
            System.out.println(s);

        }

    }
}

TCP通信

tcp在通信的两端各建立一个socket对象,Java为客户端提供了socket类,为服务器端提供了ServerSocket类

// send
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TCP_send {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("10.99.161.223",10000);
        OutputStream os = s.getOutputStream();
        byte[] bytes = "hello tcp".getBytes(StandardCharsets.UTF_8);
        os.write(bytes);
        os.close();
    }
}
// receive
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCP_receive {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10000);
        Socket accept = ss.accept();
        InputStream inputStream = accept.getInputStream();
        byte [] bys = new byte[1024];
        int len = inputStream.read(bys);
        String data = new String(bys,0,len);
        System.out.println(data);
        ss.close();

    }
}

双向奔赴:客户端发送数据,并且从服务端接收数据;服务端接收数据,然后发送反馈

// 发送
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Send_demo {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("10.99.161.223",10001);
        OutputStream os = s.getOutputStream();
        os.write("我正在发送数据".getBytes(StandardCharsets.UTF_8));
        InputStream is = s.getInputStream();
        byte [] bys = new byte[1024];
        int len = is.read(bys);
        System.out.println(new String(bys,0,len));
        s.close();
    }
}
// j
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Receive_demo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10001);
        Socket sc = ss.accept();
        InputStream is = sc.getInputStream();
        byte [] bys = new byte[1024];
        int len = is.read(bys);
        System.out.println(new String(bys,0,len));
        OutputStream os = sc.getOutputStream();
        os.write("我正在接收数据".getBytes(StandardCharsets.UTF_8));
        ss.close();
    }
}

发送时,终止发送使用shutdownOutput语句

// 接收
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class receive_demo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10000);
        Socket accept = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("test.java")));
        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bwServer.write("文件上传成功!");
        bwServer.newLine();
        bwServer.flush();
        ss.close();
    }
}
// 发送
import java.io.*;
import java.net.Socket;

public class send_demo {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("192.168.124.12",10000);
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\liupeng\\Desktop\\code\\java_project\\Net_demo\\src\\demo\\Animal.java"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        // 使用shutdown语句来终止发送
        s.shutdownOutput();
        // 接收反馈
        BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        line = brClient.readLine();
        System.out.println(line);
        br.close();
        s.close();
    }
}

最后还可以进行多线程上传文件

posted on 2022-01-24 21:34  lpzju  阅读(58)  评论(0编辑  收藏  举报

导航