Subject | UDF Problem. Can not connect using sockets |
---|---|
Author | jssahdra |
Post date | 2004-05-14T12:39:33Z |
Hi,
I have written a UDF, which connects to another application over a
TCP socket and sends some message. But somehow it is not able to
connect to that IP and port. That application is running on the same
machine on a port 30000.
here is the code snipper:
int sendmessage(const char *ip, u_int32_t port, char *message)
{
int sock;
struct sockaddr_in nas;
struct in_addr inip;
char newcmd[1000];
if (inet_aton(ip) == 0)
{
//fprintf(STDERR,"Invalid IP address: %s",ip);
return 2;
}
inet_aton(ip,&inip);
nas.sin_family = AF_INET;
nas.sin_port = htons (port);
nas.sin_addr.s_addr = inip.s_addr;
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
//fprintf(STDERR,"Error: Creating socket for: %s",ip);
return 3;
}
if (connect(sock, (struct sockaddr *) &nas,sizeof(struct
sockaddr_in)) < 0)
{
//fprintf(STDERR,"Error connecting %s",ip);
close(sock);
return 4;
}
sprintf(newcmd,"%s\n\n",message);
if (send(sock,newcmd,sizeof(newcmd),0)==-1)
{
//fprintf(STDERR,"SOCK: Error sending message. %
s",message);
close(sock);
return 5;
}
close(sock);
return 1;
}
---code for declaration
DECLARE EXTERNAL FUNCTION sendmessage CSTRING(25) , int, CSTRING(5000)
RETURNS int by value
ENTRY_POINT 'sendmessage' MODULE_NAME 'itl_udf';
---calling code--
SQL> select sendmessage('192.168.0.25',30000,'TEST') from
rdb$database;
SENDMESSAGE
============
4
--------
You can see it is returnig 4 which is connect failure.
Any ideas ???
Thanks & Regards:
JS
I have written a UDF, which connects to another application over a
TCP socket and sends some message. But somehow it is not able to
connect to that IP and port. That application is running on the same
machine on a port 30000.
here is the code snipper:
int sendmessage(const char *ip, u_int32_t port, char *message)
{
int sock;
struct sockaddr_in nas;
struct in_addr inip;
char newcmd[1000];
if (inet_aton(ip) == 0)
{
//fprintf(STDERR,"Invalid IP address: %s",ip);
return 2;
}
inet_aton(ip,&inip);
nas.sin_family = AF_INET;
nas.sin_port = htons (port);
nas.sin_addr.s_addr = inip.s_addr;
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
//fprintf(STDERR,"Error: Creating socket for: %s",ip);
return 3;
}
if (connect(sock, (struct sockaddr *) &nas,sizeof(struct
sockaddr_in)) < 0)
{
//fprintf(STDERR,"Error connecting %s",ip);
close(sock);
return 4;
}
sprintf(newcmd,"%s\n\n",message);
if (send(sock,newcmd,sizeof(newcmd),0)==-1)
{
//fprintf(STDERR,"SOCK: Error sending message. %
s",message);
close(sock);
return 5;
}
close(sock);
return 1;
}
---code for declaration
DECLARE EXTERNAL FUNCTION sendmessage CSTRING(25) , int, CSTRING(5000)
RETURNS int by value
ENTRY_POINT 'sendmessage' MODULE_NAME 'itl_udf';
---calling code--
SQL> select sendmessage('192.168.0.25',30000,'TEST') from
rdb$database;
SENDMESSAGE
============
4
--------
You can see it is returnig 4 which is connect failure.
Any ideas ???
Thanks & Regards:
JS