Here is some sample code that should help a TON.
/*-----------------------------------------------------------------------------
* Program: TCPserver
*
* Purpose: set up an concurrent TCP server waiting for message
* from client and sending back reply
*
* Usage: TCPserver
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#define BACKLOG 5 /* # of request we're willing to queue */
#define MAXHOSTNAME 32 /* maximum host name length we tolerate */
#define PORTNUM 8888 /* fixed port number to listen to */
err_sys(char * msg) {
perror(msg);
exit(-1);
}
main(int argc,char **argv) {
int s,t; /* socket descriptors */
struct sockaddr_in srv_addr; /* server socket address */
struct sockaddr_in clt_addr; /* client socket address */
struct hostent * hp; /* result of host name lookup */
char localhost[MAXHOSTNAME+1]; /* local host name as character string*/
int cli_len, childpid;
/*
* Deal with the command line arguments
*/
/*
* get our own host information
*/
gethostname(localhost, MAXHOSTNAME);
if ((hp = gethostbyname(localhost)) == NULL) {
fprintf(stderr, "%s: cannot get local host info?\n", argv[0]);
exit(1);
}
/*
* Put the port number and our address info into the socket structure
*/
bzero((char*) &srv_addr, sizeof(srv_addr));
bcopy((char *)hp->h_addr, (char *)&srv_addr.sin_addr, hp->h_length);
srv_addr.sin_family = hp->h_addrtype;
srv_addr.sin_port = htons(PORTNUM);
fprintf(stdout, "Server host: %s, port: %d\n\n", localhost, srv_addr.sin_port);
/*
* Allocate an open socket for incoming connections
*/
if ((s = socket(srv_addr.sin_family, SOCK_STREAM,0)) <0)
err_sys("socket");
/*
* Bind the socket to the service port
*/
if (bind(s, (struct sockaddr *)&srv_addr, sizeof srv_addr) <0)
err_sys("bind");
/*
* Set maximum connections we will fall behind
*/
if (listen(s, BACKLOG)<0)
err_sys("listen");
/*
* Go into an infinite loop waiting for new connections
*/
while (1) {
/*
* We hang in accept() while waiting for new clients
*/
if ((t = accept(s, (struct sockaddr *)&clt_addr, &cli_len))<0)
err_sys("accept");
switch (childpid = fork()) {
case -1: err_sys("fork");
case 0:
close(s);
/*
* the child performs the actual service
*/
service(t);
close(t);
exit(0);
default:
close(t);
}
if (waitpid(0, NULL, WNOHANG) == -1) perror("waitpid");
}
}
service(int sock) {
char buf[BUFSIZ + 1];
int n;
/*
* receive client's request
*/
if ((n = read(sock, buf, sizeof(buf)-1)) == -1)
perror("read");
buf[n] = '\0';
fprintf(stdout, "the client requests:\n%s\n", buf);
/*
* send back reply
*/
if (write(sock, buf, strlen(buf)) == -1)
perror("write");
}
/*-----------------------------------------------------------------------------
* Program: TCPclient.c
*
* Purpose: send message to server by TCP and get feedback
*
* Usage: TCPclient server_IP_address
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#define PORTNUM 8888 /* fixed port number to connect to */
err_sys(char * msg) {
perror(msg);
exit(-1);
}
main(int argc, char **argv) {
int s; /* socket descriptor */
struct sockaddr_in srv_addr; /* server socket address */
struct hostent * hp; /* result of host name lookup */
char buf[BUFSIZ+1]; /* buffer to read service information */
int n;
/*
* Deal with the command line arguments
*/
if (argc<2) {
fprintf(stderr,"Usrv_addrge: %s host\n", argv[0]);
exit(1);
}
/*
* Put the server's address and address type into socket structure
*/
bzero((char*) &srv_addr, sizeof(srv_addr));
if ((srv_addr.sin_addr.s_addr = inet_addr(argv[1])) == -1) {
fprintf(stderr, "%s: %s: Invalid host IP address\n", argv[0], argv[1]);
exit(1);
}
srv_addr.sin_family=AF_INET;
srv_addr.sin_port = htons(PORTNUM);
fprintf(stdout, "Connect to host: %s, port: %d\n\n", argv[1], srv_addr.sin_port);
fflush(stdout);
/*
* Allocate an open socket
*/
if ((s=socket(srv_addr.sin_family, SOCK_STREAM,0))<0)
err_sys("socket");
/*
* Connect to the remote server
*/
if (connect(s, (struct sockaddr *)&srv_addr, sizeof srv_addr)<0)
err_sys("connect");
/*
* Send the request and get reply
*/
sprintf(buf, "%d\n", getpid());
if (write(s, buf, strlen(buf)) == -1)
perror("write");
if ((n = read(s, buf, sizeof(buf)-1)) == -1)
perror("read");
buf[n] = '\0';
fprintf(stdout, "the server replies:\n%s\n", buf);
close(s);
}
/*-----------------------------------------------------------------------------
* Program: UDPserver
*
* Purpose: set up an iterative UDP server waiting for message
* from client and sending back reply
*
* Usage: UDPserver
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#define MAXHOSTNAME 32 /* maximum host name length we tolerate */
#define PORTNUM 8888 /* fixed port number for communication */
err_sys(char * msg) {
perror(msg);
exit(-1);
}
main(int argc,char **argv) {
int s; /* socket descriptors */
struct sockaddr_in srv_addr; /* server socket address */
struct sockaddr clt_addr; /* client socket address */
struct hostent * hp; /* result of host name lookup */
char localhost[MAXHOSTNAME+1]; /* local host name as character string*/
char buf[BUFSIZ+1];
int n, clt_len;
/*
* Deal with the command line arguments
*/
/*
* get our own host information
*/
gethostname(localhost, MAXHOSTNAME);
if ((hp = gethostbyname(localhost)) == NULL) {
fprintf(stderr, "%s: cannot get local host info?\n", argv[0]);
exit(1);
}
/*
* Put the port number and our address info into the socket structure
*/
bzero((char*) &srv_addr, sizeof(srv_addr));
bcopy((char *)hp->h_addr, (char *)&srv_addr.sin_addr, hp->h_length);
srv_addr.sin_family = hp->h_addrtype;
srv_addr.sin_port = htons(PORTNUM);
fprintf(stdout, "Server host: %s, port: %d\n\n", localhost, srv_addr.sin_port);
/*
* Allocate an open socket and bind
*/
if ((s = socket(srv_addr.sin_family, SOCK_DGRAM,0)) <0)
err_sys("socket");
if (bind(s, (struct sockaddr *)&srv_addr, sizeof srv_addr) <0)
err_sys("bind");
/*
* Go into an infinite loop waiting for new clients
*/
while (1) {
/*
* receive request and client socket address
*/
if ((n = recvfrom(s, buf, sizeof(buf)-1, 0, &clt_addr, &clt_len)) <0)
perror("recvfrom");
buf[n] = '\0';
fprintf(stdout, "the client requests:\n%s\n", buf);
/*
* send back reply
*/
if (sendto(s, buf, strlen(buf), 0, &clt_addr, clt_len) < 0)
perror("sendto");
}
}
/*-----------------------------------------------------------------------------
* Program: UDPclient.c
*
* Purpose: send message to server by UDP and get feedback
*
* Usage: UDPclient server_IP_address
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#define PORTNUM 8888 /* fixed port number for communication */
err_sys(char * msg) {
perror(msg);
exit(-1);
}
main(int argc,char **argv) {
int s; /* socket descriptor */
struct sockaddr_in srv_addr; /* server socket address */
struct sockaddr_in clt_addr; /* client socket address */
struct hostent * hp; /* result of host name lookup */
char buf[BUFSIZ+1]; /* buffer to read service information */
int n, srv_addr_len;
/*
* Deal with the command line arguments
*/
if (argc<2) {
fprintf(stderr,"Usrv_addrge: %s host\n", argv[0]);
exit(1);
}
/*
* Put the server's address and address type into socket structure
*/
bzero((char*) &srv_addr, sizeof(srv_addr));
if ((srv_addr.sin_addr.s_addr = inet_addr(argv[1])) == -1) {
fprintf(stderr, "%s: %s: Invalid host IP address\n", argv[0], argv[1]);
exit(1);
}
srv_addr.sin_family=AF_INET;
srv_addr.sin_port = htons(PORTNUM);
/*
* Put my own address and address type into socket structure
*/
bzero((char *) &clt_addr, sizeof(clt_addr));
clt_addr.sin_family = AF_INET;
clt_addr.sin_addr.s_addr = htonl (INADDR_ANY);
clt_addr.sin_port = htons(0);
/*
* Allocate an open socket and bind
*/
if ((s=socket(srv_addr.sin_family, SOCK_DGRAM,0))<0)
err_sys("socket");
if (bind(s, (struct sockaddr *)&clt_addr, sizeof(clt_addr)) <0)
err_sys("bind");
/*
* Send the request and get reply
*/
sprintf(buf, "%d\n", getpid());
if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&srv_addr, sizeof srv_addr) == -1)
err_sys("sendto");
if ((n = recvfrom(s, buf, sizeof(buf)-1, 0, (struct sockaddr *)&srv_addr, &srv_addr_len)) == -1 )
err_sys("recvfrom");
buf[n] = '\0';
fprintf(stdout, "the server replies:\n%s\n", buf);
close(s);
}