Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

getting error when compiling, dont know what i'm doing wrong

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
hertelbrian
Posts: 13
Joined: 2006-10-05 22:49

getting error when compiling, dont know what i'm doing wrong

#1 Post by hertelbrian »

this code is copied from a lab that i did in college a while ago... there is something we had to change in the mentioned line of code but i cant figure out what it is. any ideas guys?

Code: Select all

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main()
{
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;
    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = inet_addr("192.168.0.1");
    server_address.sin_port = 9734;
    server_len = sizeof(server_address);
    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
    listen(server_sockfd, 5);
    while(1) {
        char ch;

        printf("server waiting\n");
        client_sockfd = accept(server_sockfd, 
            (struct sockaddr *)&client_address, &client_len); //**THIS LINE**
        read(client_sockfd, &ch, 1);
        ch++;
        write(client_sockfd, &ch, 1);
        close(client_sockfd);
    }
}

this is a simple server app. that just waits for client to connect to it and sends it a character...
i forget exact error, but its somehting like cant convert int* to sockaddr
any help is appreciated...


BELOW is the client code if people feel like compiling it and trying out for themselves

Code: Select all

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main()
{
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    char ch = 'A';
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.0.1");
    address.sin_port = 9734;
    len = sizeof(address);
    result = connect(sockfd, (struct sockaddr *)&address, len);

    if(result == -1) {
        perror("oops: client1");
        exit(1);
    }
    write(sockfd, &ch, 1);
    read(sockfd, &ch, 1);
    printf("char from server = %c\n", ch);
    close(sockfd);
    exit(0);
}

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#2 Post by ajdlinux »

Please get the exact error.

FWIW, I had no trouble compiling it with GCC on my Etch machine.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au

hertelbrian
Posts: 13
Joined: 2006-10-05 22:49

#3 Post by hertelbrian »

this is the exact error i'm getting...

no idea why i'm getting it

server.cpp: In function `int main()':
server.cpp:26: error: invalid conversion from `int*' to `socklen_t*'

carm
Posts: 5
Joined: 2006-10-18 06:50
Location: Belarus
Contact:

#4 Post by carm »

Hello All.
hertelbrian wrote:this is the exact error i'm getting...

no idea why i'm getting it

server.cpp: In function `int main()':
server.cpp:26: error: invalid conversion from `int*' to `socklen_t*'
socklen_t this is a unsigned type, try use unsigned int.

carm
Posts: 5
Joined: 2006-10-18 06:50
Location: Belarus
Contact:

#5 Post by carm »

Hello All.
hertelbrian wrote:this is the exact error i'm getting...

no idea why i'm getting it

server.cpp: In function `int main()':
server.cpp:26: error: invalid conversion from `int*' to `socklen_t*'
hmm socklen_t this is a unsigned type, try use unsigned int.
Last edited by carm on 2006-10-18 13:51, edited 1 time in total.

hertelbrian
Posts: 13
Joined: 2006-10-05 22:49

#6 Post by hertelbrian »

the exact compile command i was using is 'g++ server.cpp'

i have to use g++ becuase this is going to be put into a current c++ program i have written... just testing out the client server stuff in another program for ease of use

User avatar
brain
Posts: 253
Joined: 2006-04-25 17:15

#7 Post by brain »

Change

Code: Select all

int server_len, client_len;
to

Code: Select all

socklen_t server_len, client_len;
hertelbrian wrote:i have to use g++
No you don't have to. This is C code, not C++. It doesn't matter if it's going to end up in C++ code. This is still not C++.

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#8 Post by ajdlinux »

hertelbrian wrote:i have to use g++ becuase this is going to be put into a current c++ program i have written... just testing out the client server stuff in another program for ease of use
It's C code, not C++ code, and the reason it is not compiling is that it is valid C code but not C++ code.

There are plenty of examples on the net about this, just search for a few.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au

User avatar
brain
Posts: 253
Joined: 2006-04-25 17:15

#9 Post by brain »

ajdlinux wrote:and the reason it is not compiling is that it is valid C code but not C++ code.
Not even close. Most C code is valid C++ code. The reason it's not compiling (like me and carm said before) is that both bind() and accept() want addrlen to be of type socklen_t. hertelbrian had those variables declared as regular int's which is not the same. The type socklen_t is unsigned and and normal int signed.

hertelbrian
Posts: 13
Joined: 2006-10-05 22:49

#10 Post by hertelbrian »

thanks brain... worked liked a charm

yeah from what i know... C code compiles in C++ environment

oh well.. all is good now

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#11 Post by ajdlinux »

brain wrote:
ajdlinux wrote:and the reason it is not compiling is that it is valid C code but not C++ code.
Not even close. Most C code is valid C++ code. The reason it's not compiling (like me and carm said before) is that both bind() and accept() want addrlen to be of type socklen_t. hertelbrian had those variables declared as regular int's which is not the same. The type socklen_t is unsigned and and normal int signed.
Not even close to what I meant. *This* C code is valid C++ in terms of syntax however the compiler does not accept it - as you said, it was different types. The C compiler will accept it but the C++ compiler will not. This therefore makes it valid C but not valid C++.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au

Post Reply