Need help with connect() in Sockets - C++

Hello

So, I'm using multiple sources for writing in sockets now (pdf, sites,...)
http://www.linuxhowtos.org/manpages/2/connect.htm .



But I'm stuck with the connect-function, how is that struct supposed to look? What should it include? Why that pointer? ...?
1
2
int connect(int sockfd, const struct sockaddr *addr,
            socklen_t addrlen);

Could someone explain that code?

Also, I've already written this piece for the sockets:
1
2
3
4
5
6
7
8
9
//Engine for socket
int socket_a = socket(AF_INET, SOCK_STREAM, 0);
//Connect? - Help needed
if (socket_a == -1) {
	cout<<"An error occured in the connection"<<endl;
} else {
	cout<<"Connection succesful"<<endl;
}
//End - Engine for socket 


Is it okay for now? :)

Thanks for reading,
Niely
it's a pointer to sockaddr because the socket library is using a form of OOP: sockaddr is the "base class", and the actual data members and the size of the structure depend on the value of addr->sa_family

Since you're using AF_INET, you need to create an object of type struct sockaddr_in, fill what's needed, and pass a pointer to that into connect()
^I don't get it. :/
I only know a pointer (*) is something what refers to a struct, which looks like an array?
But what should be in that struct?

What object of type?

Could you/someone parse an easy example of the connect function with it's struct? :)
And which Socket Length? How can a socket have a length??? O_o

Thanks!
Last edited on
From the man page you linked to:
An example of the use of connect() is shown in getaddrinfo(3).


So I clicked getaddrinfo(3) to see the example here:
http://www.linuxhowtos.org/manpages/3/getaddrinfo.htm

Here is the struct's layout:
1
2
3
4
5
6
7
8
9
10
struct addrinfo {
    int              ai_flags;
    int              ai_family;
    int              ai_socktype;
    int              ai_protocol;
    socklen_t        ai_addrlen;
    struct sockaddr *ai_addr;
    char            *ai_canonname;
    struct addrinfo *ai_next;
};


I do suggest you go read that example to get the rest of the details on how to call connect.
Topic archived. No new replies allowed.