Hi,
I have a stream server application written in C, that I need to incorporate in a GUI C++ managed new program, but I am having trouble finding the equivalent of the C sockets function calls.
The C apps initialize this way:
/*
* Init socket
*/
if ( 0 > (sock_lci = socket(AF_INET, SOCK_STREAM, 0)) )
{
exit(-1);
}
int len, on;
on = 1;
setsockopt(sock_lci, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
ser_lci.sin_family = AF_INET;
ser_lci.sin_addr.s_addr = INADDR_ANY;
ser_lci.sin_port = htons(PORT);
len = sizeof(ser_lci);
if ( 0 > bind(sock_lci, (struct sockaddr *)&ser_lci, len) )
{ exit(-1)
}
if ( -1 == listen(sock_lci, 1) )
{
exit(-1);
}
Then, the main loops does something like:
fd_set ready;
int sockfd_in, nbin, nb;
char *bb, buff[MAX_LEN_MESS];
FD_ZERO(&ready);
FD_SET(sock_lci, &ready);
if ( 0 > (nb = select(sock_lci+1, &ready, (fd_set *)NULL, (fd_set *)NULL, &to)) )
{ exit(-1);
}
if(nb == 0) return;
if ( FD_ISSET(sock_lci, &ready) )
{ if ( 0> (sockfd_in = accept(sock_lci, (struct sockaddr *)NULL,
(int *)NULL)) )
{ perror("rr_lci");
return;
}
bb = (char *)buff;
ioctlsocket(sockfd_in, FIONREAD, &nbin);
if ( nbin != 0 && nbin <= MAX_LEN_MESS)
{ if ( 0 >= (nb = recv(sockfd_in, bb, nbin, 0)) )
{ exit(-1);
}
}
/* Answer */
if ( -1 == send(sockfd_in, "Answer", 7, 0))
exit(-1);
So, basically, the app listens on the port PORT, from any possible host, gets the string sent and replies.
I guess that I have to use now the Socket Class, but I can't find an example that reproduces my code, including how to define what function will be triggered from the main loop when a message is ready to be read.
Any help would be much appreciated.