Itat,复赛有道题就是如题的要求。借鉴了别人的代码,自己做了一下。现在将我的
代码贴出,试试。这是我在javaeye的第二篇博客。呵呵……
服务端代码:
package net.udp.myudp;
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import javax.imageio.stream.FileImageInputStream;
public class Server extends Thread
{
DatagramSocket ds = null;
final int len = 128;
byte [] data = new byte[len];
public Server()
{
try
{
ds = new DatagramSocket(1987);
}
catch (SocketException e)
{
// e.printStackTrace();
}
}
public void getImgae(String path)
{
try
{
data = null;
data = new byte[len];
// System.out.println(new File(path).exists());
FileImageInputStream fileImg = new FileImageInputStream(new File(
path));
data = new byte[(int) fileImg.length()];
fileImg.read(data, 0, (int) fileImg.length());
}
catch (Exception e)
{
// e.printStackTrace();
}
}
public void run()
{
while (true)
{
DatagramPacket dp = new DatagramPacket(data, data.length);
try
{
System.out.println("-----------------------------------");
ds.receive(dp);
String path = new String(dp.getData()).trim();
System.out.println("已经接收到客户端信息:" + path+" ["+dp.getAddress()+":"+dp.getPort()+"]");
getImgae(path);
// System.out.println("图片长度:"+data.length);
DatagramPacket willsend = new DatagramPacket(data, data.length,
dp.getAddress(), dp.getPort());
ds.send(willsend);
// System.out.println("已将图片发送至客户端");
System.out.println("-----------------------------------\n\n\n");
}
catch (IOException e)
{
// e.printStackTrace();
}
}
}
public static void main(String [] args)
{
new Server().start();
}
}
客户端代码:
package net.udp.myudp;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Client extends JFrame
{
DatagramSocket ds = null;
final int len = 512;
BufferedImage buImg = null;
TextField txtpath = new TextField(20);
byte [] data = null;
JPanel up, down;
public Client()
{
super("获取图片_客户端");
txtpath.setText("c:\\a.jpg");
JButton btn = new JButton("点击获取图片");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
send();
}
});
up = new JPanel();
this.add(up, BorderLayout.CENTER);
down = new JPanel();
this.add(down, BorderLayout.SOUTH);
down.add(txtpath);
down.add(btn);
this.setSize(650, 550);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getRootPane().setDefaultButton(btn);
try
{
ds = new DatagramSocket(1012);
}
catch (SocketException e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
super.paint(g);
if (buImg != null)
{
// buImg.setRGB(280,190,200);
// System.out.println("正在绘制啊");
// g.setColor(Color.BLUE);
g.drawImage(buImg, 20, 30, up);
// g.drawString("This is the String",200,160);
}
System.out.println("绘制完成");
}
public void send()
{
data = txtpath.getText().trim().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(data, data.length,
InetAddress.getByName("172.17.76.108"), 1987);
ds.send(dp);
data = new byte[35000];
DatagramPacket rec = new DatagramPacket(data, data.length);
ds.receive(rec);
System.out.println("rec.length=" + rec.getLength());
System.out.println("rec.getData().length=" + rec.getData().length);
InputStream strImg = new ByteArrayInputStream(rec.getData(), 0, rec
.getData().length);
buImg = ImageIO.read(strImg);
System.out.println("buImg=" + buImg);
repaint();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{
new Client();
}
}
先把服务端运行起来,等待客户端连接。客户端运行起来后,输入一个存在的图片路径,点击按钮。即可获取从服务端发送过来的图片,并在绘制在窗体上。
效果如图:
但是有个问题,第一次点击按钮时,没有图片显示,第二次点击才会显示。第三次又没有,第四次又出现……如此循环,不知何故,望知情告知为谢!