Need to fix Linux 'connect' warning.

This is a discussion on Need to fix Linux 'connect' warning. within the Networking/Device Communication forums, part of the General Programming Boards category; This is client side code for a program I am working on. I've followed the examples I am learning from, ...

  1. #1
    Registered User bchaffin72's Avatar
    Join Date
    Mar 2009
    Location
    Ontario
    Posts
    13

    Need to fix Linux 'connect' warning.

    This is client side code for a program I am working on. I've followed the examples I am learning from, and get this warning when compiling:

    "passing argument 2 of 'connect' from incompatible pointer type"

    The program seems to work as I intend,but I prefer to resolve warnings whenever possible, just in case they may cause trouble down the road.

    address and port are read from a config file, but I didn't show that code as it didn't seem relevant to the problem.

    Code:
    int s,z;
    
    char *srvr_addr = address;
    char *srvr_port = port;
    struct sockaddr_in adr_srvr;
    int len_inet;
    
    s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    
    memset(&adr_srvr,0,sizeof adr_srvr);
    
    adr_srvr.sin_family = AF_INET;
    adr_srvr.sin_port = htons(atoi(srvr_port));
    adr_srvr.sin_addr.s_addr = inet_addr(srvr_addr);
    
    len_inet = sizeof adr_srvr;
    
    z = connect(s,&adr_srvr,len_inet);
    What might the problem be?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Take a look at 'connect' prototype:

    Code:
    int connect(int , struct sockaddr *, int );
    You should typecast the 'struct sockaddr_in' to the requiered one:

    Code:
    len_inet=sizeof(struct sockaddr);
    connect(s,(struct sockaddr *)&adr_srvr,len_inet);
    Hope that helps.
    Niara

  3. #3
    Registered User bchaffin72's Avatar
    Join Date
    Mar 2009
    Location
    Ontario
    Posts
    13
    Yes, it helped. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc access violation
    By JOCAAN in forum C Programming
    Replies: 7
    Last Post: 11-30-2008, 04:47 AM
  2. Strange gcc warning messages with derived classes
    By skewray in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2007, 05:46 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21