faccio un esempio di server veramente base scritto in C per Unix
codice:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include "errlib.h"
#include "sockwrap.h"
#define MAXBUFL 255
#define ADD_PORT 9999
char *prog;
static void add_client (int sockfd)
{
ssize_t n;
char buf[MAXBUFL+1];
while ( (n = Readline(0,buf,sizeof(buf))) > 0)
{
Writen (sockfd, buf, n);
if ( (n = Readline(sockfd,buf,sizeof(buf))) > 0)
Writen (1, buf, n);
else
err_quit("(%s) error - unexpected EOF from server", prog);
}
}
int main (int argc, char *argv[])
{
int sockfd;
struct sockaddr_in servaddr;
// for libraries to know the program name
prog = argv[0];
// check the arguments
if (argc != 2)
err_quit ("usage: %s IPaddress_of_server", prog);
// create socket
sockfd = Socket(AF_INET, SOCK_STREAM, 0);
// specify address of server to connect to
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(ADD_PORT);
Inet_aton(argv[1], &servaddr.sin_addr);
// try to connect
Connect(sockfd, (SA*) &servaddr, sizeof(servaddr));
// read the questions, send to server, get and display the answers
add_client (sockfd);
// done
exit(0);
}
Commenti
la libreria sys/types.h contiene dei tipi di dato in formato neutro di rete che hanno lo scopo di eliminare le incompatibilità tra le rappresentazioni dei tipi base sulle varie macchine.
La sys/socket.h contiene i tipi e le funzioni che servono a trattare un socket (in C) come se fosse un file, con la differenza che nei socket non è possibile l'accesso casuale.
La libreria arpa/inet contiene i tipi e le funzioni che servono a trattare gli indirizzi IP.
Includo anche errlib.h
codice:
/*
module: errlib.h
purpose: definitions of function sin errlib.c
reference: Stevens, Unix network programming (2ed), p.922
*/
#include <stdarg.h>
extern int daemon_proc;
void err_msg (const char *fmt, ...);
void err_quit (const char *fmt, ...);
void err_ret (const char *fmt, ...);
void err_sys (const char *fmt, ...);
void err_dump (const char *fmt, ...);
ed errlib.c che è necessaria per compilare
codice:
/*
module: errlib.c
purpose: library of error functions
reference: Stevens, Unix network programming (2ed), p.922
*/
#include <stdio.h>
#include <stdlib.h> // exit()
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#define MAXLINE 4095
int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
{
int errno_save = errno, n;
char buf[MAXLINE+1];
vsnprintf (buf, MAXLINE, fmt, ap);
n = strlen(buf);
if (errnoflag)
snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
strcat (buf, "\n");
if (daemon_proc)
syslog (level, buf);
else
{
fflush (stdout);
fputs (buf, stderr);
fflush (stderr);
}
}
void err_ret (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
err_doit (1, LOG_INFO, fmt, ap);
va_end (ap);
return;
}
void err_sys (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
err_doit (1, LOG_ERR, fmt, ap);
va_end (ap);
exit (1);
}
void err_msg (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
err_doit (0, LOG_INFO, fmt, ap);
va_end (ap);
return;
}
void err_quit (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
err_doit (0, LOG_ERR, fmt, ap);
va_end (ap);
exit (1);
}
Se vuoi posso mandarti sockwrap.h, ma contiene soltanto i wrapper delle funzioni usate in questo programma.
Per farlo funzionare senza la libreria mancante basta che cambi la lettera iniziale di tutte le funzioni da maiuscola a minuscola.