LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   invalid conversion from `int*' to `socklen_t*' (https://www.linuxquestions.org/questions/programming-9/invalid-conversion-from-%60int%2A-to-%60socklen_t%2A-145992/)

r350 02-14-2004 10:29 AM

invalid conversion from `int*' to `socklen_t*'
 
This code compiles fine in gcc but when I'm using g++ whith some added c++ syntax it wont compile.

Code:

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "main.h"
using namespace std;


#define MYPORT 3490        //porten som det andra programet connectar till
#define BACKLOG 10

int main(){
       
        //själva initieringen av severn
        int sockfd, new_fd;
        sockaddr_in my_addr;                //min address
        sockaddr_in their_addr;        //deras address
  int sin_size;
       
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
       
       
        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(MYPORT);
        my_addr.sin_addr.s_addr = INADDR_ANY;
        memset(&(my_addr.sin_zero), '\0', 8);
       

       

        bind(sockfd, (sockaddr *)&my_addr, sizeof(struct sockaddr));
        //slutet
       
        //kopplingen
        listen(sockfd, BACKLOG);
        sin_size = sizeof(sockaddr_in);
       
       
        new_fd = accept(sockfd, (sockaddr *)&their_addr, &sin_size);
        shutdown(sockfd, 2); //behöver inte lyssna för ny längre
        send(new_fd, "BANAN\n", 6, 0);
       
       
        shutdown(new_fd, 2); //stänger av uppkopplingen
       
       
        return 0;
}

Code is taken from beej's socket tutorial.

wapcaplet 02-14-2004 10:49 AM

What line does the error occur on?

r350 02-14-2004 10:50 AM

ohh sorry, line 42: new_fd = accept(sockfd, (sockaddr *)&their_addr, &sin_size);

wapcaplet 02-14-2004 11:55 AM

Hm, according to the accept manual page, the third parameter needs to be a socklen_t pointer; so I suppose what you'd need to do is cast the int pointer you're passing in to a socklen_t pointer:

new_fd = accept(sockfd, (sockaddr *)&their_addr, (socklen_t*)&sin_size);

Not sure if that's the best way to do it, but it should work...

Seems that there is an alternate version of the accept() function that does in fact take an int pointer, which may be what the author was using.

r350 02-14-2004 11:58 AM

Thank you very much, it worked like charm.

ericsun 10-02-2011 05:34 PM

Thanks , mine works~~


All times are GMT -5. The time now is 05:30 PM.