Re: udp multithreading



palmis wrote:
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
.



Relevant Pages

  • Re: Socket Creation
    ... drivers until it reaches the TCP/IP driver. ... When a packet is received at a NIC the NIC hardware does some trivial ... specifications of a previously-created socket, ... may be an incomplete datagram - just a "fragment". ...
    (microsoft.public.win32.programmer.kernel)
  • Re: udp multithreading
    ... I have to monitor many systems in the same moment, ... The udp receiver, receives the messages of the last system selected to ... // Crea un datagram Packet ...
    (comp.lang.java.programmer)
  • Re: Can recvfrom() return more than 1 packet?
    ... There is only a "whole IP packet" if the whole datagram fits in one ... recvfrom() on a SOCK_DGRAM socket is the ... SOCK_DGRAM socket causes you to receive datagrams. ...
    (comp.unix.programmer)
  • Re: Socket Buffers
    ... Also note that, even if a packet has been received, the ioctlsocket can give ... an indication of '0' for the length when the socket is a datagram socket ... > Paul T. ...
    (microsoft.public.windowsce.app.development)
  • Re: Socket Creation
    ... An application or service "opens a socket" and this tells the kernel-mode TCP/IP driver that the application is "listening" for datagrams on a "listener" IP port or that it will expect a reply of some sort in the future. ... If the packet destination address is the address of the NIC, then the NIC "indicates" the received packet up a "stack" of filter drivers until it reaches the TCP/IP driver. ... In this situation the TCP/IP driver will "reassemble" the fragments from multiple packets until a complete datagram is available. ...
    (microsoft.public.win32.programmer.kernel)