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...

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.