计算机网络技术与JAVA网络编程UDP编程-----JAVA入门基础教程-----计算机网络经典

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;

public class UDP
{
    public static void main(String[] args)
    {
        DatagramSocket datagramSocket = null;
        try
        {
            datagramSocket = new DatagramSocket();
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 9090;
            byte[] bytes = "我是发送端".getBytes("utf-8");
            DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,inetAddress,port);
            datagramSocket.send(datagramPacket);
        }
        catch (SocketException e)
        {
            throw new RuntimeException(e);
        }
        catch (UnknownHostException e)
        {
            throw new RuntimeException(e);
        }
        catch (UnsupportedEncodingException e)
        {
            throw new RuntimeException(e);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
        finally
        {
            if (datagramSocket != null)
            {
                datagramSocket.close();
            }
        }
    }
    @Test
    public void Receiver()
    {
        try
        {
            int port = 9090;
            DatagramSocket datagramSocket = new DatagramSocket(port);
            byte[] bytes = new byte[1024 * 64];
            DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
            datagramSocket.receive(datagramPacket);
            System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        }
        catch (SocketException e)
        {
            throw new RuntimeException(e);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;

public class UDP
{
public static void main(String[] args)
{
DatagramSocket datagramSocket = null;
try
{
datagramSocket = new DatagramSocket();
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 9090;
byte[] bytes = "我是发送端".getBytes("utf-8");
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,inetAddress,port);
datagramSocket.send(datagramPacket);
}
catch (SocketException e)
{
throw new RuntimeException(e);
}
catch (UnknownHostException e)
{
throw new RuntimeException(e);
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
finally
{
if (datagramSocket != null)
{
datagramSocket.close();
}
}
}
@Test
public void Receiver()
{
try
{
int port = 9090;
DatagramSocket datagramSocket = new DatagramSocket(port);
byte[] bytes = new byte[1024 * 64];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
}
catch (SocketException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

你可能感兴趣的:(计算机网络经典,JAVA随手写,java,intellij-idea,数据结构,eclipse,算法)