Hi I'm trying to create and use some basic socket. I made a code but the compiler reports an error.
I'm using linux ( sys/socket.h library)
The code is>

#include <iostream>
#include <cstdlib>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>

#define PORT 1066

using namespace std;

int main()
	int sock, newsock;
	int i = 0;
	char c, line[BUFSIZ];
	struct sockaddr_in server;
	int addrsize = sizeof (server);
	sock = socket(AF_INET, SOCK_STREAM, 0);
	server.sin_family = AF_INET;
	server.sin_port = htons((short)PORT);
	server.sin_addr.s_addr = INADDR_ANY;
		if (bind(sock, (struct sockaddr *)(&server), addrsize) == -1)
		printf("Can't bind address to port.\n");
	listen(sock, 5);
		if ((newsock = accept(sock, (struct sockaddr *)(&server), &addrsize)) == -1) /* Error  */
		printf("Can't accept connection\n");
		newsock = -1;
		else printf("Accepted connection\n");
		while(read(newsock, &c, 1))
		line[i++] = c;
			if (c == 0)
			line[i] = 0;
			printf("Data: %s\n", line);
			i = 0;
	return 0;

The error is: error: invalid conversion from `int*' to ` socklen_t*'
It is in the line where the accept() function is called. ( 40)


10 Years
Discussion Span
Last Post by slacke

Well you should either typecast addrsize to socklen_t* or just declare it that way. From what I've seen in google searches, socklen_t is just an int.


to> dilpi.mathews

Did you include the same libraries as I do?
What is the Operation System you work with?

I was suspecting that it may be a compiler probleme - because I used the same struct for the bind function and it worked.

I just can't find the socklen_t* in any library ( I think it should be somewhere defined and in my code it's not so I thought it may be in some library).

But I found Sinkulas reference to Beej's guide for net. prog. -
and it's great.


I declared the addrsize variable as a socklen_t type instead of int.
And it worked.
I think that the type socklen_t may be declared (created??) from type int. But after that all it is a new type.
The compiler checks for types and it found that I want to convert one type to another ( and it dont know the way how to do that)

Please correct me if I'm wrong. I'm not sure about that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.