Click to See Complete Forum and Search --> : Connect


Charred_Phoenix
01-24-2003, 04:35 AM
Gah, I started playing with tcp and i can't get the connect() command working the second argument confuses me, I know what structures are and how to work with them just not in this context, i've seen some examples and they don't seem to make much sense.
Thanks :D

siqe
01-24-2003, 08:55 AM
I just did this for the first time yesterday morning. Here's the client code. I think this is as simple as you can get. I had to cast that second argument as a pointer to something else because the compiler complains otherwise.

#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<iostream>

using namespace std;

int main()
{
int socketid;
unsigned long port;
char hostname[128];
hostent *host;
sockaddr_in mysocket;
protoent *protocol;

cout<<"Enter the host: ";
cin>>hostname;
cout<<"Enter the port number: ";
cin>>port;

memset( &mysocket, 0, sizeof( sockaddr_in ) );
host = gethostbyname( hostname );
if( host == NULL )
{
cout<<"there was an error resolving the host."<<endl;
return 1;
}
mysocket.sin_family = AF_INET;
mysocket.sin_port = htons( port );
memcpy( &mysocket.sin_addr, host->h_addr, host->h_length );

protocol = getprotobyname( "tcp" );
if( protocol == NULL )
{
cout<<"there was an error accessing the tcp protocol."<<endl;
return 1;
}

socketid = socket( PF_INET, SOCK_STREAM, protocol->p_proto );
if( socketid < 0 )
{
cout<<"there was an error creating the socket."<<endl;
return 1;
}
if( connect( socketid, (sockaddr*)&mysocket, sizeof( mysocket ) ) < 0 )
{
cout<<"there was an error connecting to the server"<<endl;
return 1;
}
cout<<"everything went well"<<endl;

return 0;
}




You can test it with the server:

#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<iostream>
#include<unistd.h>

using namespace std;

int main()
{
int listensocket, workersocket, trueval = 1, addrlen;
unsigned long port;
protoent *protocol;
sockaddr_in mysocket;

cout<<"Enter port number: ";
cin>>port;

memset( &mysocket, 0, sizeof( mysocket ) );
mysocket.sin_family = AF_INET;
mysocket.sin_addr.s_addr = INADDR_ANY;
mysocket.sin_port = htons( port );

protocol = getprotobyname( "tcp" );
if( protocol == NULL )
{
cout<<"there was an error getting the tcp protocol."<<endl;
return 1;
}

listensocket = socket( PF_INET, SOCK_STREAM, protocol->p_proto );
if( listensocket < 0 )
{
cout<<"could not create listener socket."<<endl;
return 1;
}

if( 0 > bind( listensocket, (sockaddr*)&mysocket, sizeof( mysocket ) ) )
{
cout<<"there was an error binding the listener socket."<<endl;
return 1;
}

setsockopt( listensocket, SOL_SOCKET, SO_REUSEADDR, &trueval, sizeof( trueval ) );

if( 0 > listen( listensocket, 0 ) )
{
cout<<"couldn't listen on the listener socket."<<endl;
return 1;
}

workersocket = accept( listensocket, (sockaddr*)&mysocket, (socklen_t*)&addrlen );
if( workersocket < 0 )
{
cout<<"there was an error opening the worker socket."<<endl;
return 1;
}

close( workersocket );
close( listensocket );

return 0;
}

speaks_1337
01-24-2003, 11:49 AM
nice work on the code, i think i will have to copy some parts of that, i have been having some trouble in those parts too

Charred_Phoenix
01-24-2003, 06:18 PM
Urghhhhhhhhhhhhhhh.... This is exactly what I was talking about,

connect( socketid, (sockaddr*)&mysocket, sizeof( mysocket ) )

That second argument is meant to be the host, if the second half of it is a port, why is it called my SOCKET, as far as i know they are very different things. Your program also uses the gethostbyname() command, what if i start with an IP, I can't just supply it as such because:


#include <stdio.h>
#include <sys/sockets.h>
#include <sys/types.h>

int main(void) {
int ggg;

ggg = gethostbyname("www.linuxnewbie.com");
printf("%d\n", ggg);
return 0
}


Does not output an ip, though i'm not sure how it could, considering an ip adress has 3 decimal points and integers have none.

All I need is an explanation of the second argument of the connect() command.

Thanks

Stuka
01-24-2003, 07:03 PM
Beej's network programming guide (http://www.ecst.csuchico.edu/~beej/guide/net/) should clear up many questions.

Charred_Phoenix
01-24-2003, 07:35 PM
memset(&(dest_addr.sin_zero), '\0', 8);

Can someone explain that line, according to the man page, that should fill 8 bytes of a void variable with \0, what purpose does this have?

ChryZKoiD
01-24-2003, 09:51 PM
When you allocate memory,- as in using a structure its members are positioned sequentially within that piece memory. However it's contents are simply junk that are fragments from other programs run. Therefore you have to use memset so that no values get an accidental value.

So you use this command:
memset(&structure, '\0', sizeof(structure))
to clear it out first.

Charred_Phoenix
01-25-2003, 12:42 AM
Got it! Thanks! :D