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);
	printf("Server\n");
	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");
		exit(1);
		}
	listen(sock, 5);
		if ((newsock = accept(sock, (struct sockaddr *)(&server), &addrsize)) == -1) /* Error  */
		{
		printf("Can't accept connection\n");
		close(newsock);
		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)

help...

Dani AI

Generated

The compiler error means the third parameter of accept() is declared as socklen_t * but the code is passing an int *. C++ enforces stricter pointer-type checking than some C toolchains, so code that compiled with gcc as C may fail when compiled as C++ (or on a platform where socklen_t is not an int). As suggested, prefer declaring the length variable with the correct type instead of casting; correctly noted that on many Linux systems socklen_t is a typedef to int, but ’s caution is valid — that is platform-dependent.

The canonical prototype is:

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Initialize the socklen_t variable to the size of the address structure before calling accept (e.g. set it to sizeof(server)). Avoid blind casts from int * to socklen_t * — they hide real portability/type-safety issues.

Other practical fixes and hardening not yet covered in the thread:

  • Zero the sockaddr_in (memset or aggregate init) before filling fields to avoid garbage bytes.
  • Set SO_REUSEADDR on the listening socket to reduce bind errors during quick restarts.
  • Always check return values from socket(), bind(), listen(), and accept() and use perror() to print errno when they fail.
  • Do not call close(newsock) when accept() returned -1 (closing -1 is an error). Close the listening socket if appropriate.
  • When reading, test the read() result explicitly ( >0, ==0 for EOF, <0 for error ) rather than treating its return as a boolean.

For the official signatures and behaviors see the Linux manual pages: accept(2) and socket(7).

Recommended Answers

All 6 Replies

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.

I compiled the same pogramm using gcc without any error.

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.

This is how is socklen_t defined

#define socklen_t int

This is how is socklen_t defined

#define socklen_t int

Maybe, maybe not.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.