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();
}
}
}
分享到:
相关推荐
socket使用udp方式发送和接收图片,包含图片测试
在Qt5上利用UDP传输图片并显示,包括发送端和接收端
socket,tcp、udp接收工具;作为接收端;不能作为发送端;小工具;
通过j2se upd网络编程,发送报文。我这里只是客户端,修改数据库连接,主要是把数据库里面的图片(base64编码处理),拆成包,发送给服务端。
该文件主要以udp进行发送图片,并接收图片显示在graphView上。此发送与接收都在一个程序之中,相当于自发自收,如果只需要发送或者接收,把对应接收或发送部分注释即可。
本资源内有两个C#窗体项目,一个是图像发送端,一个是图像接收端。发送端使用EmguCV读取摄像头图像,压缩为JPEG格式后使用UDP发送至接收端,接收端接收图像并进行显示。注释丰富,另外还有详细的介绍博客
UDP传输的优点是快捷,因为它是没有安全保证的。有一定的丢失率。而且,由于各包的传输路径不同,后发送的包可能先于先发送的包到达。所以,如果发送大量的数据,就需要进行检错。当然,还得保证快捷的传输。 我就是...
qt实现udp通讯,不仅可以发送接收文字,还能收发图片,代码简单易懂适合新手。本例子中收发软件都是同一个,用时记得改下代码里的ip和端口
C#的Socket实现UDP协议通信 CSharp
对于客户端,UDP不用建立连接,只管发送不管接收到没有,所以可以直接对服务器的IP地址和端口号发送信息,然后等待应答。 注意传输的数据是二进制流数据,所以要找方法把需要传输的数据编码成二进制码流,传过去之后...
"55 aa 99 02 + 服务器的设备名称",表示服务器接收到广播后,响应客户端请求,把此包指定IP发送客户端(此指定IP地址可以UDP广播信息包中获取) * 3. "55 aa 99 03",表示客户端接收到服务器的响应,接着向服务器...
场景介绍如果需要使用UDP传输较大数据,例如传输10M的图片,这突破了UDP的设计原则。UDP的设计是基于”datagram”,也就是它假设你发送的每个数据包都能包含在单一的包内。并且设定UDP数据包的最大长度受基础网络...
自己写的一个简单的基于Qt的UDP收发程序 每个程序即可以作为发送者也可以作为接收者 仅供入门学习QtUdpSocket编程
里面有两个c++的QT程序,一个作为发送端,一个作为接收端,可以发送文字图片,摄像头的信息,对二维码进行识别
在发送端,UDP传送数据的速度仅仅是受应用程序生成数据的速度、计算机的能力和传输带宽的限制;在接收端,UDP把每个消息段放在队列中,应用程序每次从队列中读一个消息段。 由于传输数据不建立连接,因此也就不需要...
接下来,我们将使用Python编写发送端程序,该程序将通过UDP协议向ESP8266发送图片数据TJpg_Decoder库解码。为了实现这一目标,我们需要使用socket库来创建UDP套接字,打包和解包数据。此外,我们还需要了解ILI9431...
QT通过UDP分包传输大图像,发送端和接收端将图像分开传输和接收(测试可传6M图片)
软件可接收Socket TCP和UDP指令,通过指令控制播放器播放视频等操作,如:播放,暂停,音量加减,视频切换,图片切换,上一张,下一张,自动播放等。软件本身无任何控制,所以操作均通过网络指令实现。 用途: 可...
方法就是给数据包编号,按照包的顺序接收并存储,接收端接收到数据包后发送确认信息给发送端,发送端接收确认数据以后再继续发送下一个包,如果接收端收到的数据包的编号不是期望的编号,则要求发送端重新发送。...
- 发送端可以支持任意类型的文件(即可以发送图片(pic_test.jpeg)/视频文件(turn-on-off.mp4)等等) 在此原代码基础上实现Go-Back-N算法和Selective Acknowledgements(选择重传)。 ## 测试 TestHarness.py提供...