Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem while compiling socket connect code in C

Tags:

c

sockets

struct hostent *lh               = gethostbyname(hostname);

int socketDescriptor             = socket(AF_INET,SOCK_STREAM, 0);

sockaddr_in socketInfo;

memset(&socketInfo, 0, sizeof(socketInfo));
socketInfo.sin_family            = AF_INET;
socketInfo.sin_addr.s_addr       = ((in_addr *)(lh->h_addr))->s_addr;
socketInfo.sin_port              = htons(portNumber);

connect(socketDescriptor,&socketInfo,sizeof(socketInfo));

When trying to compile, I get the following error:

error: cannot convert ‘sockaddr_in*’ to ‘const sockaddr*’ for argument ‘2’ to ‘int connect(int, const sockaddr*, socklen_t)’

Things look "by the book", but I am missing something (obviously). What is it?

like image 900
James Raitsev Avatar asked May 29 '11 20:05

James Raitsev


People also ask

What is the purpose of socket connect () method?

This method is typically used immediately after a call to GetHostAddresses, which can return multiple IP addresses for a single host. If you are using a connection-oriented protocol such as TCP, the Connect method synchronously establishes a network connection between LocalEndPoint and the specified remote endpoint.

What is socket () bind () listen () accept () and connect ()?

Create a socket with the socket() function; Bind the socket to an address using the bind() function; Listen for connections with the listen() function; Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server.

What is a socket connection error?

A socket error is when there is a problem with the user's computer or the other computer or server while making this connection. There are more than 100 socket errors, but they all boil down to similar problems on either side of the connection. Fixing a socket error is usually easy, but not always.

What are the common causes of socket errors?

Socket errors can be caused by various issues including connectivity problems on the network, client or server computers or due to a firewall, antivirus or a proxy server. This error occurs when the socket connection to the remote server is denied.


2 Answers

I think you are missing struct on sockaddr_in socketInfo. So it should be struct sockaddr_in socketInfo.

Also casting socketInfo to struct sockaddr * would be nice.

connect(socketDescriptor,&socketInfo,sizeof(socketInfo));

Should be

connect(socketDescriptor,(struct sockaddr *) &socketInfo,sizeof(socketInfo));
like image 129
cnicutar Avatar answered Sep 20 '22 07:09

cnicutar


  struct addrinfo *server;
  struct addrinfo hints; 

    memset(&hints, 0, sizeof(struct addrinfo));/*set hints*/
    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_flags = 0;
    hints.ai_protocol = 0;          /* Any protocol */

 getaddrinfo(srvname,port,&hints,&server); /*get server ip fit args*/

 sid = socket(server->ai_family, server->ai_socktype,server->ai_protocol)
 connect(sid,server->ai_addr, server->ai_addrlen)

Those are some codesnipets that will work and that you can start from.

What happens here is that I set up one struct with all the intel and than combine it with some more info to get one nice all pointers fit structure too pass to connect. hope that helps

like image 25
Eduard Thamm Avatar answered Sep 23 '22 07:09

Eduard Thamm