-
Invalid conversions
I am trying to compile this code and I am getting an error I don't understand
Here is the section of code:
Code:
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in addr, claddr;
pid_t pid;
int i, found;
int flag;
int rc;
pthread_t threadid;
signal(SIGCHLD, SIG_IGN);
if (argc >= 2) {
strcpy(rootdir, argv[1]);
} else {
getcwd(rootdir, 512);
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
printf("Error in socket.\n");
return -1;
}
flag = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0)
{
printf("Error in setsockopt for REUSEADDR\n");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(7513);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
printf("Error in bind\n");
return -1;
}
if (listen(sock, 1) < 0)
{
printf("Error in listen\n");
return -1;
}
while (1)
{
int consock, size;
size = sizeof(claddr);
consock = accept(sock, (struct sockaddr *)&claddr, &size);
//printf("New connection arriving...\n");
if (consock < 0)
{
printf("Error in accept\n");
return 0;
}
And this is the error it says I have.
Code:
/home/roger/Nethost/main.cpp|898|error: invalid conversion from ‘int*’ to ‘socklen_t*’|
/home/roger/Nethost/main.cpp|898|error: initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’|
I am new to programing in C and have never done socket programing before.
Any help would be appreciated
-
If you mean socklen_t, use socklen_t instead of int.
-
I don't understand, I highlighted the line that is generating the error.
-
If you want size to be a socklen_t, if you plan to use size as a socklen_t, then declare it as a socklen_t:
Code:
/* not int size; */
socklen_t size;
-
This code is compiled using C++. And in C++ different types means error (it's pretty much a programming error in C, too, unless you use an explicit cast - and that means you know what you're doing, and in this case, it's WRONG).
accept is expecting the type socklen_t*, but you're passing it int*, which are not the same.
The correct solution then, is what tabstop is pointing out.