udp multithreading

P

palmis

I want to send monitoring messages using udp protocol.
I have to monitor many systems in the same moment, so I have to send
monitoring messages in the same moment. but I have a problem:
The udp receiver, receives the messages of the last system selected to
monitor and not of the all systems monitored.

why?
I have to use thread?

thanks
Palmis
This is the code

import java.net.*;

/**
* Questa classe effettua la connessione UDP per l'invio dei messaggi
ciclici di
* monitoring.
* @author Palmisano Francesca.
*/
public class SenderUdp {

// Numero di porta dell'host al quale connettersi. Il valore e'
prelevato
// dal file di configurazione.
public static String port_string = "";

// Indirizzo Ip dell'host al quale connettersi. Il valore e' prelevato
dal
// file di configurazione.
public static String address_string = "";

/**
* Questo metodo e' il costruttore della classe.
*/
public SenderUdp() {
}

/**
* Questo metodo effettua la connessione UDP.
* @param messageSend rappresenta il messaggio in input da inviare.
*/
public static void connection(byte[] messageSend) {
try {
// Crea un datagram Packet
InetAddress address = InetAddress.getByName(address_string);
int port = Integer.parseInt(port_string);
DatagramPacket dp = new DatagramPacket(messageSend,
messageSend.length, address, port);

// Crea un datagram Socket
DatagramSocket ds = new DatagramSocket();

// Invia al server il datagram packet
ds.send(dp);
return;
} catch (Exception e) {
e.printStackTrace();
}
}// Fine metodo connection()
}
 
P

ponce

maybe because transmission over UDP is not reliable... and hence some
packets are lost
 
S

Steve Horsley

palmis said:
I want to send monitoring messages using udp protocol.
I have to monitor many systems in the same moment, so I have to send
monitoring messages in the same moment. but I have a problem:
The udp receiver, receives the messages of the last system selected to
monitor and not of the all systems monitored.

why?
I have to use thread?

thanks
Palmis
This is the code

import java.net.*;

/**
* Questa classe effettua la connessione UDP per l'invio dei messaggi
ciclici di
* monitoring.
* @author Palmisano Francesca.
*/
public class SenderUdp {

// Numero di porta dell'host al quale connettersi. Il valore e'
prelevato
// dal file di configurazione.
public static String port_string = "";

// Indirizzo Ip dell'host al quale connettersi. Il valore e' prelevato
dal
// file di configurazione.
public static String address_string = "";

/**
* Questo metodo e' il costruttore della classe.
*/
public SenderUdp() {
}

/**
* Questo metodo effettua la connessione UDP.
* @param messageSend rappresenta il messaggio in input da inviare.
*/
public static void connection(byte[] messageSend) {
try {
// Crea un datagram Packet
InetAddress address = InetAddress.getByName(address_string);
int port = Integer.parseInt(port_string);
DatagramPacket dp = new DatagramPacket(messageSend,
messageSend.length, address, port);

// Crea un datagram Socket
DatagramSocket ds = new DatagramSocket();

// Invia al server il datagram packet
ds.send(dp);
return;
} catch (Exception e) {
e.printStackTrace();
}
}// Fine metodo connection()
}

It looks like you are trying to create a new socket for each
remote host, or even each packet sent - I'm not sure which. You
shouldn't. Just create one socket for sending to all the remote
hosts. Ican't remember whether you create each DatagramPacket
with a different address/port, or whether you use sendTo() now,
but either way you only need one socket. And the same socket can
be used to receive packets from all remotes. Just check where
each packet came from.

Steve
 
E

EJP

Nothing wrong with your sending code apart from what Steve wrote, but
what does your receiving code do?
 
P

palmis

thank you.
I have solved my problem.
It's true! In the code posted there isn't any error.
I wrong to create new messages monitoring when chenge the subsystem
monitored.

Palmis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top