UDP接收发送图片

1、编写客户/服务器程序,客户端Client.java使用DatagramSocket对象将数据包发送到服务器,请求获取服务器端的图像,服务器端Server.java将图像文件包装成数据包,并使用DatagramSocket对象将该数据包发送到客户端。首先将服务器端的程序编译通过,并运行起来,等待客户的请求。

public class UDPServerExample implements Runnable {

private DatagramSocket socket = null;
private DatagramPacket receivePacket = null, sendPacket = null;


public static void main(String args[]) {
new UDPServerExample().start();
}
public void start() {
try {
socket = new DatagramSocket(7000);
new Thread(this).start();//启动线程
} catch (Exception e) {
}
}
public void run() {
try {
while (true) {
byte[] buf = new byte[50];
receivePacket = new DatagramPacket(buf, buf.length);
socket.receive(receivePacket);
if(receivePacket.getLength() > 0) { //接收到请求
System.out.println("客户机的地址:" + receivePacket.getAddress().getHostAddress());
System.out.println("正在等待");
sendData(receivePacket.getAddress(),receivePacket.getPort());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {

}
public void sendData(InetAddress address,int post) {
FileInputStream fis = null;
try {
byte[] buf = new byte[3072];
fis = new FileInputStream(new File("c://1.jpg"));
int read = 0;
while((read=fis.read(buf)) != -1) {
sendPacket = new DatagramPacket(buf, read,address,post);
socket.send(sendPacket);
Thread.sleep(5);
}
// 文件发送完毕,再发送一个数据报通知客户端
if(read == -1) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream pout = new PrintStream(out);
pout.print("end");
buf = out.toByteArray();
sendPacket = new DatagramPacket(buf, buf.length, address, post);
socket.send(sendPacket);
}

} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


2、客户端

public class UDPClientExample extends JFrame implements ActionListener,Runnable {

private static final long serialVersionUID = 1L;
private DatagramSocket socket = null;
private DatagramPacket receivePacket = null, sendPacket = null;
private JButton button = new JButton("获取图像");
private JLabel label = new JLabel();
private String serverIp = "127.0.0.1";
private int serverPort = 7000;

public UDPClientExample() {
this.setTitle("I am a client");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
button.addActionListener(this);
this.add(button, "North");
this.add(label, "Center");
this.setVisible(true);

try {
// 创建一个DatagramSocket,使用随机端口
socket = new DatagramSocket();
} catch (SocketException e1) {
e1.printStackTrace();
}
}

public void start() {
sendMessage(); //发送请求
new Thread(this).start();//启动线程

}
public static void main(String args[]) {
new UDPClientExample();
}

public void actionPerformed(ActionEvent e) {
start();
}
public void sendMessage() {
try {
byte[] buf = new byte[]{'g','e','t'};
sendPacket = new DatagramPacket(buf, buf.length, InetAddress
.getByName(serverIp), serverPort);
socket.send(sendPacket);
buf = null;
button.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
receiveData();
}
public void receiveData() {
try {
byte[] total = new byte[500*1024];
int start = 0;
byte buf[] = new byte[3072];
while (true) {
receivePacket = new DatagramPacket(buf, buf.length);
socket.receive(receivePacket);
ByteArrayInputStream bin = new ByteArrayInputStream(
receivePacket.getData());
BufferedReader read = new BufferedReader(new InputStreamReader(
bin));
String str = read.readLine();
if(str.startsWith("end")) {
break;
}
buf = receivePacket.getData();
// 将buf数组里的数据复制到total数组中
System.arraycopy(buf, 0, total, start, receivePacket.getLength());
start += receivePacket.getLength();
}
ImageIcon icon = new ImageIcon(total);
label.setIcon(icon);
//button.setEnabled(true);

} catch (Exception e) {
e.printStackTrace();
}

}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值