On Thu, 27 Nov 1997 11:15:44 -0800 (PST), RHS Linux User wrote:
Hello,
I was trying to open a socket, but the problem I kept running into was that connect wants a sockaddr struct, and in C you just casted it... (struct sockaddr *)&address but, I don't think that that is possible in pascal, so if anyone has successfully done this or knows how. please email me,
Well, I've done some work interfacing GPC with sockets, and the problem you describe is the first one I hit. The only solution I've found so far for problems like these is to wrap them inside callable C routines.
E.g. I have something like:
int bind_socket(int orig_sock, short family, u_short port) {
struct sockaddr_in serv_adr;
memset( &serv_adr, 0, sizeof(serv_adr) ); serv_adr.sin_family = family; serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); serv_adr.sin_port = htons(port);
if (bind( orig_sock, (struct sockaddr *) & serv_adr, sizeof(serv_adr)) < 0) { close(orig_sock); return (-1); }
return (0);
}
in socket.c (with all of the necessary includes)
Then in my Pascal source I use:
function bind_socket(socket : integer; family : ShortInt; port : ShortCard) : integer; C;
and I can bind a socket with a call like:
result := bind_socket(socket, AF_INET, port);
The at compile time I just include my C file with the source:
gcc sockettest.pas socket.c -lsocket or whatever...
As I explore more of the features in GPC hopefully I'll be able to make more calls directly from Pascal, but for calls like bind(), accept() and connect() I've found it easier to write a wrapper in C. Obviously it would take a while to write good wrappers that would return full error conditions to GPC not to mention allowing users to get at all of the socket options you have in C, but I've been just kludging solutions that happen to fit my needs. Right now all I have working is a quick-and-dirty connection where a client can send strings to a server process on another machine and have it echoed back slightly modified.
-Kevin -- Kevin A. Foss --- kfoss@mint.net --