作者在 2010-05-19 14:35:56 发布以下内容
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpClient {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket();
String strInfo = "d:/Input.txt";//将要转到服务端的信息
DatagramPacket dp = new DatagramPacket(strInfo.getBytes(), strInfo
.length(), InetAddress.getByName("127.0.0.1"), 3000);
ds.send(dp);//传送并接接信息
byte[] len = new byte[1024];//创建一个数据缓冲区大小了1024
dp = new DatagramPacket(len, 1024);//将得到的数据放到dp中
ds.receive(dp);//接收数据
String n = new String(dp.getData(), 0, dp.getLength());//将接收到的数据放到字符串中
System.out.println("你好!" + n);//输出
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上面是客户端代码
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpServer {
public static void main(String[] args) {
DatagramSocket ds = null;
DatagramPacket dp = null;
String m = null;
try {
ds = new DatagramSocket(3000);//创建服务器端
byte[] len = new byte[1024];//创建一个缓冲区
dp = new DatagramPacket(len, 1024);//将数据加到dp中
ds.receive(dp);//接收dp
String n = new String(dp.getData(), 0, dp.getLength());//将接收到的数据放到n中
FileReader fr = new FileReader(n);//读取文件
BufferedReader dr = new BufferedReader(fr);
InetAddress ia = dp.getAddress();//得到客户端的地址
int iport = dp.getPort();//客户端的端口
StringBuffer sb = new StringBuffer();
m = dr.readLine();
while (m != null) {
sb.append(m);//将读取的文件放到sb中
sb.append("\n");//在每一行结束的时候加一个换行
m = dr.readLine();
}
m = sb.toString();//将sb转为String类型的
dp = new DatagramPacket(m.getBytes(), m.length(), ia, iport);//数据在转回到客户端
ds.send(dp);//发送
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
m = "文件不存在 \n ";
InetAddress ia = dp.getAddress();
int iport = dp.getPort();
dp = new DatagramPacket(m.getBytes(), m.length(), ia, iport);
try {
ds.send(dp);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpServer {
public static void main(String[] args) {
DatagramSocket ds = null;
DatagramPacket dp = null;
String m = null;
try {
ds = new DatagramSocket(3000);//创建服务器端
byte[] len = new byte[1024];//创建一个缓冲区
dp = new DatagramPacket(len, 1024);//将数据加到dp中
ds.receive(dp);//接收dp
String n = new String(dp.getData(), 0, dp.getLength());//将接收到的数据放到n中
FileReader fr = new FileReader(n);//读取文件
BufferedReader dr = new BufferedReader(fr);
InetAddress ia = dp.getAddress();//得到客户端的地址
int iport = dp.getPort();//客户端的端口
StringBuffer sb = new StringBuffer();
m = dr.readLine();
while (m != null) {
sb.append(m);//将读取的文件放到sb中
sb.append("\n");//在每一行结束的时候加一个换行
m = dr.readLine();
}
m = sb.toString();//将sb转为String类型的
dp = new DatagramPacket(m.getBytes(), m.length(), ia, iport);//数据在转回到客户端
ds.send(dp);//发送
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
m = "文件不存在 \n ";
InetAddress ia = dp.getAddress();
int iport = dp.getPort();
dp = new DatagramPacket(m.getBytes(), m.length(), ia, iport);
try {
ds.send(dp);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// e.printStackTrace();
}
}
}