The TCP socket provides you the most reliable connection to the server. It uses the highest level offered in the TCP/IP protocol stack which deploys several processes to ensure the reliability of each message. TCP also offers you a special open communication flow which is similar to low-level I/O called streams.
The Socket API simplifies the interface to access nearly every network and protocol. TCP (Transaction Control Protocol) is, perhaps, the more common protocol on the Internet. It has two basic communicators: clients and servers. Servers offer services to the network; clients connect to these services through ports. The services available on the network are quite varied, from time services to databases.
Implementing clients and servers are fairly straightforward, given you follow a particular formula. Clients build the socket, connect to the server and then exchange messages.
/************************************************************************/ /*** Example client that lets user input short text lines and sends ***/ /*** to a server. In turn, it picks up the message from the server ***/ /*** and displays the results on the console. ***/ /************************************************************************/ #include <stdio.h> #include <string.h> #include <ctype.h> #include <sys/socket.h> #include <resolv.h> /* For some reason Mandrake does not work with sys/types.h */ /*#include <sys/types.h>*/ /* Use this if you can -- it's more portable */ #include <netdb.h> #define DEFAULT_PORT 9999 /*-------------------------------------------------------------------*/ /*-- chat -- exchange messages between client and server --*/ /*-------------------------------------------------------------------*/ void chat(int sd) { int bytes; char buf[1024]; do { bytes = recv(sd, buf, sizeof(buf)-1, 0); /* Get message from server */ if ( bytes < 0 ) /* If connection failed, close */ { perror("reading failed"); break; } buf[bytes] = 0; /* All messages sent raw--needs NULL-termination */ printf("%s", buf); /* Display server's message */ if ( fgets(buf, sizeof(buf), stdin) != NULL ) /* Read message from console */ if ( send(sd, buf, strlen(buf)-1, 0) < 0 ) /* Send to server */ { perror("writing failed"); /* If pipe broken, close */ break; } else break; /* If end of console input, close */ } while ( strcmp(buf, "bye\n") != 0 ); /* Repeat until "bye" sent */ } /*-------------------------------------------------------------------*/ /*-- convert_address -- accept host:port, lookup host and convert --*/ /*-- port. It using common port name (e.g. "http") convert that. --*/ /*-------------------------------------------------------------------*/ int convert_address(char *host, struct sockaddr_in *addr) { char *c; int port=htons(DEFAULT_PORT); struct hostent *ent; if ( (c = strchr(host, ':')) != NULL ) { struct servent *serv; *c = 0; if ( isdigit(c[1]) ) port = htons(atoi(c+1)); /* If number, convert. */ else { serv = getservbyname(c+1, "TCP"); /* If using common name, convert. */ if ( serv == NULL ) { perror(c+1); return -1; } port = serv->s_port; } } ent = gethostbyname(host); /* Convert hostname (address) */ if ( ent != NULL ) { bzero(addr, sizeof(*addr)); /* This is a deprecated call: use memset for new development */ addr->sin_family = AF_INET; /* Select the IP network */ addr->sin_port = port; /* Set port number (already network-byte) */ addr->sin_addr.s_addr = *(long*)ent->h_addr; /* Set the address */ } else { perror(host); return -1; } return 0; } /*-------------------------------------------------------------------*/ /*-- main -- Create socket and connect to server. Call call(). --*/ /*-------------------------------------------------------------------*/ int main(int count, char *strings[]) { int sd; struct sockaddr_in addr; if ( count != 2 ) { printf("usage: %s <host>[<port>]", strings[0]); exit(0); } sd = socket(PF_INET, SOCK_STREAM, 0); /* Create socket */ if ( sd > 0 ) { perror("Socket"); exit(1); } if ( convert_address(strings[1], &addr) != 0 ) exit(1); if ( connect(sd, &addr, sizeof(addr)) != 0 ) { perror("Connect"); exit(1); } chat(sd); close(sd); }