> I've never coded any tcp stuff before and have problems with a test program
> I'm writing which is supposed to create a socket connection to be connected
> to by a local client process. The connect() gives me back a socket number of
> listen() sometimes comes straight back and the accept() fails with EFAULT.
> Sometimes the listen seems to work but when I try to connect with the client
> process I get ECONNREFUSED.
> Do I need to set up a an entry in the services file ?
> I enclose the source below.
> Thanks in advance for any replies
> Regards
> Peter Deed
> /* --------------------- SERVER PROCESS ---------------*/
> #include <stdio.h>
> #include <sys/socket.h>
> #include <netdb.h>
> #include <errno.h>
> struct servent *serviceent;
> struct protoent *protocolent;
> struct sockaddr *socketaddress;
> struct sockaddr receiveaddress;
> main()
> {
> int s;
> int *addresslen;
> int retval;
> struct sockaddr *receiveaddress;
> /* Get Service Entry */
why malloc these - just define them as struct sockaddr receiveaddress
and pass &receiveaddress etc... to the bind/accept calls
Quote:> socketaddress=(struct sockaddr*)malloc(sizeof(struct sockaddr));
> receiveaddress=(struct sockaddr*)malloc(sizeof(struct sockaddr));
minor point - what if socket/receiveaddress is NULL
Quote:> protocolent=getprotobyname("tcp");
you don't _need_ to set protocolent - you can use 0 - which will use the
right number for tcp (6 I beleive)
Quote:> s = socket(AF_INET, SOCK_STREAM, protocolent->p_proto);
might be nice to see if s isn't -1
woa!!!!
1) - you need to blank out socketaddress or else god knows what you're
asking to bind to
2) - what is '14' how about sizeof(struct sockaddr) (which should be 16
anyway)
3) - check retval and (if non zero) - use perror("bind") - it's
wonderfully helpful
4) - what port are you binding to. Either you need to specify a port
before you bind (in a sockaddr_in structure which you then memcpy into
the sockaddr structure (or just cast it on the bind/accept if lazy)) -
OR you read it out of a sockaddr_in structure _after_ the bind (to port
0) to see which port you've been given.
Quote:> retval = bind ( s, socketaddress, 14 );
1) - 6000 is a _little_ excessive. You'll probably find the system
maximum is 5 :-)))
2) - listen doesn't actually _do_ anything - it just tell the O/S how to
deal with your socket so it should just come right back.
3) - go on - check retval - it's good for your soul
Quote:> retval = listen ( s, 6000 );
1) - be nice to accept - memset receiveaddress to NULLs
2) - set *addresslen to sizeof(struct sockaddr) before you call
3) - umm - accept returns your shiny new socket - you _probably_ don't
want to call it retval.
4) - again - pesky error checking - if the returned socket is -1 perror
is a _must_
Quote:> retval = accept ( s, receiveaddress, addresslen );
> }
> /*------------------ CLIENT PROCESS ---------------------*/
> #include <stdio.h>
> #include <sys/socket.h>
> #include <netdb.h>
> #include <errno.h>
> //struct servent *serviceent;
> struct sockaddr *socketaddress;
> main()
> {
> int s;
> int retval;
> /* Get Service Entry */
> socketaddress=(struct sockaddr*)malloc(sizeof(struct sockaddr));
6 - no - use 0 - let the O/S decide :-))
Quote:> s = socket(AF_INET, SOCK_STREAM, 6);
go on - be a devil - check that s isn't -1
Quote:> socketaddress->sa_family = AF_INET;
supposed to do. In a _normal_ situation - I'd expect you to set
sin_family, sin_port (which you need to know from the discussion about
'bind' above) and sin_addr.s_addr in a sockaddr_in structure (don't
forget to memset to NULL first :-)) and then copy that into a sockaddr
structure for the connect call. If this is some use of AF_INET sockets
sensible port number if you are treating the sa_data back from the bind
as a string (which it isn't).
7 ?? what's 7 ?? sizeof(struct sockaddr) again perchance ??
Quote:> retval = connect ( s, socketaddress, 7 );
> }
fundamentally
1) check for errors
2) use struct sockaddr_in to set/read values for the calls - memcpy
to/from struct sockaddr for the actual calls themselves
3) use sizeof() - it's your friend.
4) let protocol default from 0 in socket() calls
5) always memset() address buffer for socket calls to ensure no *is
* around
6) man listen, man SSC accept, man bind, man socket and man connect -
all dead useful
have fun,
Paul