UDP和TCP的使用 java

本文档展示了如何使用Java实现UDP和TCP通信,包括发送字符串消息、接收数据并写入文件。重点介绍了UDP Sender和UDP Receiver的代码,以及TCP Client和Server的交互过程。
摘要由CSDN通过智能技术生成

UDP通信
发送:

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/*
@author   Nian
@Date     2022/8/10 14:41
@purpose  
          
@Note     
*/
public class UDPSender {
    public static void main(String[] args) {
        try {
//            send();
            send2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void send() throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();
        String message = "哈哈哈哈哈哈哈哈";
        byte[] bytes = message.getBytes();
        int len = bytes.length;
        int port = 10086;
        InetAddress inetAddress = InetAddress.getLocalHost();
        DatagramPacket datagramPacket = new DatagramPacket(bytes,len,inetAddress,port);
        datagramSocket.send(datagramPacket);
        datagramSocket.close();
    }

    public static void send2() throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();
        int port = LocalParameter.PORT;
        String message = "哈哈哈1";
        byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
        int len = bytes.length;
        InetAddress inetAddress = InetAddress.getLocalHost();
        DatagramPacket datagramPacket = new DatagramPacket(bytes,len,inetAddress,port);
        datagramSocket.send(datagramPacket);
    }
}

接收:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/*
@author   Nian
@Date     2022/8/10 14:42
@purpose  UDP receiver
          
@Note     
*/
public class UDPReceiver {
    public static void main(String[] args) {
        String path = "D:" + File.separator + "receive.txt";
        try {
//            receive(path);
            receive2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void receive(String path) throws IOException {
        int port = 10086;
        DatagramSocket datagramSocket = new DatagramSocket(port);
        byte[] bytes = new byte[1024*1024];
        int len = bytes.length;
        DatagramPacket datagramPacket = new DatagramPacket(bytes,len);
        datagramSocket.receive(datagramPacket);
        int length = datagramPacket.getLength();
        String s = new String(bytes,0,length);
        System.out.println(s);

        FileOutputStream fileOutputStream = new FileOutputStream(path,true);
        fileOutputStream.write(bytes,0,length);
        fileOutputStream.close();

        datagramSocket.close();
    }

    public static void receive2() throws Exception{
        DatagramSocket datagramSocket = new DatagramSocket(LocalParameter.PORT);
        byte[] bytes = new byte[1024];
        int len = bytes.length;
        DatagramPacket datagramPacket = new DatagramPacket(bytes,len);
        datagramSocket.receive(datagramPacket);
        int length = datagramPacket.getLength();
        String s = new String(bytes,0,length, StandardCharsets.UTF_8);
        System.out.println(s);
    }

}

TCP通信:
客户端:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

/*
@author   Nian
@Date     2022/8/10 15:17
@purpose  
          
@Note     
*/
public class Client {
    public static void main(String[] args) {
        try {
//            TCPAccept();
            send();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void TCPAccept() throws IOException {
        String message = "hello";
        InetAddress inetAddress = InetAddress.getLocalHost();
        //build Socket by ip and port
        Socket socket = new Socket(inetAddress,LocalParameter.PORT);

        //use socket get OutputStream
        OutputStream outputStream = socket.getOutputStream();
        byte[] bytes = message.getBytes();
        outputStream.write(bytes);

        //get output stream and send message
        InputStream inputStream = socket.getInputStream();
        byte[] bytes1 = new byte[1024];
        int len;
        if((len = inputStream.read(bytes1))!= -1){
            String s = new String(bytes1,0,len, StandardCharsets.UTF_8);
            System.out.println(s);
        }

        outputStream.flush();
        outputStream.close();
    }

    public static void send() throws IOException {
        InetAddress inetAddress = InetAddress.getLocalHost();
        Socket socket = new Socket(inetAddress,LocalParameter.PORT);
        OutputStream outputStream = socket.getOutputStream();
        String s = "请求服务器应答";
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        outputStream.write(bytes);
        socket.close();
    }
}

服务端:

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;

/*
@author   Nian
@Date     2022/8/10 15:18
@purpose  
          
@Note     
*/
public class Server {
    public static void main(String[] args) {
        try {
//            receiveMessage();
            receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void receiveMessage() throws IOException {
        ServerSocket socket = new ServerSocket(LocalParameter.PORT);
        System.out.println("before");
        Socket accept = socket.accept();
        InputStream inputStream = accept.getInputStream();
        byte[] bytes = new byte[1024];
        int len = inputStream.read(bytes);
        String s = new String(bytes,0,len);

        System.out.println(s);
        OutputStream outputStream = accept.getOutputStream();
        String s1 = "通讯完毕";
        outputStream.write(s1.getBytes(StandardCharsets.UTF_8));
//        inputStream.close();
        accept.close();
        socket.close();
    }

    public static void receive() throws IOException {
        ServerSocket serverSocket = new ServerSocket(LocalParameter.PORT);
        Socket accept = serverSocket.accept();
        InputStream inputStream = accept.getInputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes))!= -1){
            String s = new String(bytes,0,len,StandardCharsets.UTF_8);
            System.out.println(s);
        }
        accept.close();
        serverSocket.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

念犯困

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值