Thread: Invalid conversions

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    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
    Last edited by gr8npwrfl; 07-29-2008 at 01:23 PM. Reason: clarity

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you mean socklen_t, use socklen_t instead of int.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    I don't understand, I highlighted the line that is generating the error.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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;

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM