Java and UDP - 03/29/04 04:43 AM
Hi there,
i'm writing a program for a course in the University and i have to use the protocol UDP. The general idea is that there is a server in the University that is listening in a specific port, let's say server_port, and i have to send over the internet packets using the UDP. Then the server sends back to me a packet in a specific port of my system.
The program seems to be working for the sending part but when it comes to receive a packet i have an IOException.
What i do follows
- create 2 buffers for the datagram packet that follows
byte[] packet_send = new byte[1024];
byte[] packet_receive = new byte[1024];
- take the address of the server
ia = InetAddress.getByName("SERVER_ADDRESS");
- create a datagram packet for sending
DP_send = new DatagramPacket(packet_send, packet_send.length, ia, server_port);
- create a datagram packet for receiving
DP_receive = new DatagramPacket (packet_receive, packet_receive.length);
- set parametres for the packet_receive
DP_receive.setAddress(client_address);
DP_receive.setPort(client_port);
- create a datagram socket in the specified port of my system
dr = new DatagramSocket(client_port);
- send a packet
dr.send(DP_send);
- set the time to wait for the receiving packet
dr.setSoTimeout(1000);
- wait for a packet
dr.receive(DP_receive);
After all these, in the last line i get an IOException. I don't know why...
Another thing. The packet i'm sending must have as data something specified, like E2345. I tried to set the data of the packet by using
packet_send = data.getBytes();
where the "data" is the string "E2345", but nothing happens.
Does anyone have an idea of how i can send and receive these packets?
Thanks a lot.
i'm writing a program for a course in the University and i have to use the protocol UDP. The general idea is that there is a server in the University that is listening in a specific port, let's say server_port, and i have to send over the internet packets using the UDP. Then the server sends back to me a packet in a specific port of my system.
The program seems to be working for the sending part but when it comes to receive a packet i have an IOException.
What i do follows
- create 2 buffers for the datagram packet that follows
byte[] packet_send = new byte[1024];
byte[] packet_receive = new byte[1024];
- take the address of the server
ia = InetAddress.getByName("SERVER_ADDRESS");
- create a datagram packet for sending
DP_send = new DatagramPacket(packet_send, packet_send.length, ia, server_port);
- create a datagram packet for receiving
DP_receive = new DatagramPacket (packet_receive, packet_receive.length);
- set parametres for the packet_receive
DP_receive.setAddress(client_address);
DP_receive.setPort(client_port);
- create a datagram socket in the specified port of my system
dr = new DatagramSocket(client_port);
- send a packet
dr.send(DP_send);
- set the time to wait for the receiving packet
dr.setSoTimeout(1000);
- wait for a packet
dr.receive(DP_receive);
After all these, in the last line i get an IOException. I don't know why...
Another thing. The packet i'm sending must have as data something specified, like E2345. I tried to set the data of the packet by using
packet_send = data.getBytes();
where the "data" is the string "E2345", but nothing happens.
Does anyone have an idea of how i can send and receive these packets?
Thanks a lot.