java UDP example

Send out UDP pockets

import java.io.*;
import java.net.*;

public class UDPSend {
public static void main(String args[]) {
try {
String host = "www.java2s.com";
int port = 90;

byte[] message = "Java Source and Support".getBytes();

// Get the internet address of the specified host
InetAddress address = InetAddress.getByName(host);

// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(message, message.length,
address, port);

// Create a datagram socket, send the packet through it, close it.
DatagramSocket dsocket = new DatagramSocket();
dsocket.send(packet);
dsocket.close();
} catch (Exception e) {
System.err.println(e);
}
}
}

Receive UDP pockets

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

public class UDPReceive {
public static void main(String args[]) {
try {
int port = 90;

// Create a socket to listen on the port.
DatagramSocket dsocket = new DatagramSocket(port);

// Create a buffer to read datagrams into. If a
// packet is larger than this buffer, the
// excess will simply be discarded!
byte[] buffer = new byte[2048];

// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

// Now loop forever, waiting to receive packets and printing them.
while (true) {
// Wait to receive a datagram
dsocket.receive(packet);

// Convert the contents to a string, and display them
String msg = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ": "
+ msg);

// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
}
}
}


Using Datagrams to Get the Date

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

public class GetDate {
final static int PortDayTime = 13; // well-known daytime port

public static void main(String args[]) throws Exception {
byte msg[] = new byte[256];
DatagramSocket dgSocket = new DatagramSocket();
InetAddress destination = InetAddress.getByName("web.mit.edu");
DatagramPacket datagram = new DatagramPacket(msg, msg.length,
destination, PortDayTime);
dgSocket.send(datagram);
datagram = new DatagramPacket(msg, msg.length);
dgSocket.receive(datagram);
String received = new String(datagram.getData());
System.out.println("The time in Cambridge is now: " + received);
dgSocket.close();
}
}



An echo server using UDP sockets

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

public class UdpEchoServer {
static final int BUFFERSIZE = 256;

public static void main(String[] args) {
DatagramSocket sock;
DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE],
BUFFERSIZE);
try {
sock = new DatagramSocket(7);
} catch (SocketException e) {
System.out.println(e);
return;
}
// echo back everything
while (true) {
try {
sock.receive(pack);
sock.send(pack);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值