网络编程

 

 

 

 

 

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

public class UDP_Send {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine()) != null) {
            if ("886".equals(line)) {
                break;
            }
            byte[] bytes = line.getBytes();
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.244.1"), 10086);
            ds.send(dp);

        }
        ds.close();
    }
}

 

 

 

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

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

 

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

public class TCP_Send {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.244.1",10086);
        OutputStream os = socket.getOutputStream();
        os.write("TCP".getBytes());
        socket.close();
    }
}

 

 

 

 

 

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

public class TCP_Recieve {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10086);
        Socket s = ss.accept();
        InputStream is = s.getInputStream();
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        String data = new String(bytes,0,len);
        System.out.println(data);
        ss.close();
    }
}

 

posted @ 2022-02-02 22:20  香草ovo  阅读(137)  评论(0编辑  收藏  举报