How can i send image using UDP in Matlab code ?
조회 수: 10 (최근 30일)
이전 댓글 표시
I want to create UDP communication between Java and Matlab. Matlab is us a server and while Java is the client. The client asks to the server to image. How can I write a code in Matlab to send the image to the client?
댓글 수: 0
채택된 답변
Sebastian Castro
2018년 11월 4일
If you have Instrument Control Toolbox, you can use this functionality:
- Sebastian
댓글 수: 14
Walter Roberson
2018년 11월 5일
Your Java side is expecting to read a single datagram of maximum length 1024. Your image content is likely to be larger than that. On the MATLAB side you are not creating any output buffer so you will get the default which is 512. The maximum output buffer size for udp objects is 4096.
Are you expecting to send very small images? If not then you need to create a protocol on top of udp to send all of the bytes in multiple packets, taking into account that the packets could arrive out of order. https://www.mathworks.com/help/instrument/outputbuffersize.html
추가 답변 (1개)
Walter Roberson
2018년 11월 5일
runudp.m :
%this is my code to send the image in Matlab
Sender = 'localhost'; % LocalHost for testing on the same computer
portSender = 33781;
ipReceiver = 'localhost'; % LocalHost for testing on the same computer
portReceiver = 33780;
BS = 1024;
udpReceiver = udp(Sender,portSender, 'LocalPort', portReceiver, 'OutputBufferSize', BS);
fopen(udpReceiver);
image = imread('peppers.png');
fwrite(udpReceiver , image(1:BS), 'uint8')
fclose(udpReceiver);
delete(udpReceiver);
xferudp.java :
import java.io.IOException;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
import java.awt.FlowLayout;
import java.io.ByteArrayInputStream;
import javax.swing.ImageIcon;
class xferudp {
//this java class code to receive the image and display
public static void main(String args[]) throws IOException {
InetAddress IPAddress = InetAddress.getByName("localhost"); // LocalHost for testing on the same computer
DatagramSocket clientSocket = new DatagramSocket(33781, IPAddress);
byte[] imageBuffer = new byte[1024 ] ;
DatagramPacket p=new
DatagramPacket(imageBuffer,imageBuffer.length);
System.out.println("UDP S: Receiving...");
clientSocket.receive(p);
byte[] buffer = p.getData();
BufferedImage img =ImageIO.read(new ByteArrayInputStream(buffer));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
To use,
!javac -d . -classpath . xferudp.java
!sudo java xferudp
This gets as far as
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
at xferudp.main(xferudp.java:35)
which might have something to do with the fact that we did not a valid image over to be decoded.
The write error was definitely tied to writing too much for the socket. Remember, there is no automatic segmentation for UDP, and no automatic reconstruction by order -- every UDP packet is independent of each other.
댓글 수: 5
Mishal Malkani
2020년 7월 5일
Can you please guide me about sending a file of more than 65KB from Client to Server in UDP communication in Matlab?I am unable to read the bytes of loaded image and have no idea about sending it from client to server!!
참고 항목
카테고리
Help Center 및 File Exchange에서 Java Client Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!