Sorry for replying to myself... On Thu, Feb 05, 2004 at 10:45:16AM -0800, P Oscar Boykin wrote: > There are APIs that throw errors when the ICMP message comes in. > > For instance, in Java there is the PortUnreachableException: > http://klomp.org/mark/classpath/doc/api/html/java/net/PortUnreachableException.html > > It seems like people are saying that they think that A simply ignores > the ICMP message and the .Net framework has no way of passing that > information to the user (without being root and listening for the raw > Icmp packets)
It seems like here is the problem, if you send a udp packet to a host which is not listening on that port, nothing will happen, *BUT* if you Connect first, the next ReceiveFrom will throw an exception. You can find example code both in c and C# attached to this email. I would prefer not to have to connect the socket to get this functionality, but if that is the Socket API, I guess I am stuck with it. POB. -- [EMAIL PROTECTED] http://pobox.com/~boykin jabber: [EMAIL PROTECTED] fingerprint=D250 4AD9 4544 B7D2 A17C 911D D608 D387 6718 D75F code is speech. support freedom on the net. http://www.eff.org/
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MYPORT 3490 // the port users will be connecting to
#define DEST_PORT 4240 // the port we try sending to (no one listens there)
#define DEST_IP "127.0.0.1"
int main(int argc, char* argv[])
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in dest_addr; // connector's address information
int sin_size;
int resp, size;
char buf[1024];
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // do some error checking!
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
//Bind to the address:
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(DEST_PORT); // short, network byte order
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct
//If I connect, then recvfrom will give an error, else not
resp = connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
resp = sendto(sockfd, "hello udp",8, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
if( resp == -1 )
{
perror("sendto");
exit(1);
}
//Now get the response:
resp = recvfrom(sockfd, buf, 1024, 0, (struct sockaddr *)&dest_addr, &size);
if( resp == -1 )
{
perror("recvfrom");
exit(1);
}
return 0;
}
using System.Net.Sockets;
using System.Net;
public class Test {
public static void Main(string[] arg)
{
EndPoint end = new IPEndPoint(IPAddress.Parse(arg[0]),
System.Int16.Parse(arg[1]));
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
byte[] buf = new byte[1024];
s.Bind(new IPEndPoint(IPAddress.Any, 23456));
s.Connect(end);
s.SendTo(buf, end);
s.ReceiveFrom(buf, ref end);
}
}
pgp00000.pgp
Description: PGP signature
