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

Need help with C, C++, perl, python, etc?

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

Postby hertelbrian » 2006-10-17 16:17

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);
}
hertelbrian
 
Posts: 13
Joined: 2006-10-05 22:49

Postby ajdlinux » 2006-10-17 20:45

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
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby hertelbrian » 2006-10-18 01:44

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*'
hertelbrian
 
Posts: 13
Joined: 2006-10-05 22:49

Postby carm » 2006-10-18 10:18

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

Postby carm » 2006-10-18 10:19

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.
carm
 
Posts: 5
Joined: 2006-10-18 06:50
Location: Belarus

Postby hertelbrian » 2006-10-18 12:58

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
hertelbrian
 
Posts: 13
Joined: 2006-10-05 22:49

Postby brain » 2006-10-18 13:09

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++.
User avatar
brain
 
Posts: 253
Joined: 2006-04-25 17:15

Postby ajdlinux » 2006-10-18 20:43

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
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby brain » 2006-10-18 21:56

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.
User avatar
brain
 
Posts: 253
Joined: 2006-04-25 17:15

Postby hertelbrian » 2006-10-19 01:48

thanks brain... worked liked a charm

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

oh well.. all is good now
hertelbrian
 
Posts: 13
Joined: 2006-10-05 22:49

Postby ajdlinux » 2006-10-19 08:36

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
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable