Click to See Complete Forum and Search --> : Whiteboard, C++ Compilation Error.


naveenkumars
12-20-2004, 01:54 AM
Hi all

I got a c++ code for whiteboard.When i compiled with gcc in linux i got some errors as below

gcc whiteboard.cpp whiteboard.cpp:25:
conflicting types for `char*sys_errlist[]' /usr/include/bits/sys_errlist.h:28:
previous declaration as `const char* const sys_errlist[]'
whiteboard.cpp: In function `int main(int, char**)':
whiteboard.cpp:84: invalid conversion from `int*' to `socklen_t*'


Why this error comes
Plz help me

Thank U

CaptainPinko
12-20-2004, 03:32 AM
perhaps post some of that code here for us to take a look?

naveenkumars
12-20-2004, 07:20 AM
Hi ,

U have asked for the code , so here's the code!!

extern int errno;
extern char *sys_errlist[]; --> error bco'z of this !!
int passiveTCP(char *, int);
int passivesock(char *, char *, int);
int errexit(char *, char *, char *);

int main
(
int argc,
char *argv[]
)
{
char *service = PORT; // service name or port number
struct sockaddr_in fsin; // the address of the client
int msock; // master server socket
fd_set rfds; // read file descriptor set
fd_set afds; // active file descriptor set
int alen; // length of client's address
int fd, nfds; // from-address length

switch (argc)
{
case 1:
printf("using port %s\n", service);
break;
case 2:
service = argv[1];
break;
default:
errexit("usage: server [port]\n", "", "");
}

msock = passiveTCP(service, QLEN);
printf("Master socket = %d\n", msock);

nfds = getdtablesize();
printf("Up to %d connections\n", nfds);

FD_ZERO(&afds);
FD_SET(msock, &afds);

while (1)
{
bcopy((char *)&afds, (char *)&rfds, sizeof(rfds));

printf("Waiting on select...\n");
if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0)
errexit("select: %s\n", sys_errlist[errno], "");

if (FD_ISSET(msock, &rfds))
{
// A new connection
int ssock;
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if (ssock < 0)
errexit("accept: %s\n", sys_errlist[errno], "");
printf("Accept on %d.\n", ssock);
FD_SET(ssock, &afds);

// Transmit the session log to the new connection...
// ... tbd
}

for (fd = 0; fd < nfds; ++fd)
{
if ((fd != msock) && FD_ISSET(fd, &rfds))
{
// Read each input stream
#ifdef TESTMODE
printf("Checking for data on %d...\n", fd);
#endif
char buf[BUFSIZE];
int cc = read(fd, buf, sizeof buf);
if (cc < 0)
{
// client exited - slear this socket
(void)close(fd);
FD_CLR(fd, &afds);

continue;
}
else if (cc == 0)
{
// EOF - close connection with this client
printf("Closing connection to %d\n", fd);
(void)close(fd);
FD_CLR(fd, &afds);
}
else // cc > 0
{
#ifdef TESTMODE
buf[cc] = '\0';
printf("Data ('%s') being broadcast...\n", buf);
#endif
// broadcast the data to all clients except current one
for (int bfd = 0; bfd < nfds; ++bfd)
{
if ((bfd != msock) && (bfd != fd) && FD_ISSET(bfd, &afds))
//if ((bfd != msock) && FD_ISSET(bfd, &afds))
{
if (write(bfd, buf, cc) < 0)
errexit("client write: %s\n", sys_errlist[errno], "");
}
}

// append the data to the session log
// ...tbd
}
}
}
}
}


int passiveTCP
(
char *service, // service associated with the desired port
int qlen // maximum server request queue length
)
{
return passivesock(service, "tcp", qlen);
}

#include <netdb.h>

u_short htons(), ntohs();

u_short portbase = 0; // port base, for non-root servers

int passivesock
(
char *service, // service or port (e.g. "echo", or "13")
char *protocol, // name of protocol to use ("tcp" or "udp")
int qlen // maximum length of the server request queue
)
{
struct servent *pse; // pointer to service information entry
struct protoent *ppe; // pointer to protocol information entry
struct sockaddr_in sin; // an Internet endpoint address
int s, type; // socket description and socket type

bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;

// Map service name to port number
if (pse = getservbyname(service, protocol))
sin.sin_port = htons(ntohs((u_short)pse->s_port) + portbase);
else if ((sin.sin_port = htons((u_short)atoi(service))) == 0)
errexit("can't get \"%s\" service entry\n", service, "");

// Map protocol name to protocol number
if ((ppe = getprotobyname(protocol)) == 0)
errexit("can't get \"%s\" protocol entry\n", protocol, "");

// Use protocol to choose a socket type
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;

// Allocate a socket
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("can't create socket: %s\n", sys_errlist[errno], "");

// Bind the socket
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
errexit("can't bind to %s port: %s\n", service, sys_errlist[errno]);

// Put socket in passive mode
if (type == SOCK_STREAM && listen(s, qlen) < 0)
errexit("can't listen on %s port: %s\n", service, sys_errlist[errno]);

return s;
}




int errexit
(
char *format,
char *a, char *b
)
{
fprintf(stderr, format, a, b);
printf(format, a, b);
exit(1);
}




plz help...
thank u

tecknophreak
12-20-2004, 09:02 AM
Does the program compile if you comment that line out?

It should, by including stdio.h, or cstdio in c++, you automatically get the extern.... line, so that line doesn't even need to be in the c/c++ code.

evac-q8r
12-20-2004, 12:51 PM
Yes, so have you tried putting const in that line so that in now reads...extern const char *sys_errlist[];That should solve your problem. The other problem will still be looming though. I can't advise you on this because I do not know what the function in line 84 returns:ssock = accept(msock, (struct sockaddr *)&fsin, &alen);But you (or someone else) has ssock declared as an integer (line 82), but in line 84 as shown above I presume is a function that returns type socklen_t*. But that is a guess.

EVAC

tecknophreak
12-20-2004, 01:14 PM
Ah, sorry, missed that one and forgot you were in C++. You'll have to do a:
reinterpret_cast<socklen_t *>(&alen)

Since g++ hates invalid casting... or I guess you could use socklen_t alen to fix it as well.

goon12
12-20-2004, 02:50 PM
Originally posted by evac-q8r
I can't advise you on this because I do not know what the function in line 84 returns:ssock = accept(msock, (struct sockaddr *)&fsin, &alen);But you (or someone else) has ssock declared as an integer (line 82), but in line 84 as shown above I presume is a function that returns type socklen_t*. But that is a guess.

I'm pretty sure the accept function returns an int, the descriptor for the accepted socket.

evac-q8r
12-20-2004, 03:07 PM
I think you're probably right. Thanks for the clarification anyways though.

EVAC