Here are several examples of simplistic communication via socket. All these programs are the same: the client to enter a string and the server sends it to the poster.
Example: communication across a socket file - It only works if the client and server are on the same machine
The server
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
extern const char* const sys_errlist[];
#define NAME_SOCKET ("socket_file" )
#define SZ_BUF (256)
main(
int argc,
char *argv[])
{
int sk_creat;
int sk_dialog;
int pid;
int sz_read;
char buf[SZ_BUF];
struct sockaddr_un adr_server;
signal(SIGCHLD, SIG_IGN);
unlink(NAME_SOCKET);
if ((sk_creat=socket(AF_UNIX, SOCK_STREAM, 0)) == (-1))
{
fprintf(stderr, "ligne %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
memset(&adr_server, 0, sizeof(struct sockaddr_un));
adr_server.sun_len=strlen(NAME_SOCKET) + 1;
adr_server.sun_family=AF_UNIX;
memcpy(&(adr_server.sun_path), NAME_SOCKET, strlen(NAME_SOCKET));
if (bind(sk_creat, &adr_server, sizeof(struct sockaddr_un)) == (-1))
{
fprintf(stderr, "ligne %u - bind() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
listen(sk_creat, 1);
fputc('\n', stdout);
while (1)
{
printf("ppid=%u, pid=%u\tAwaiting entry...", getppid(), getpid());
fflush(stdout);
if ((sk_dialog=accept(sk_creat, NULL, NULL)) == (-1))
{
fprintf(stderr, "line %u - accept() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
fputs("Entry issued\n", stdout);
switch (pid=fork())
{
case (-1):
close(sk_creat);
close(sk_dialog);
fprintf(stderr, "line %u - fork() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
case 0:
close(sk_creat);
while ((sz_read=read(sk_dialog, buf, SZ_BUF)) > 0)
{
printf("\n\tppid=%u, pid=%u\tThe server has read '%s'%s\n", getppid(), getpid(), buf, strcmp(buf, "EOT" ) != 0 ?"" :"=> End call" );
if (strcmp(buf, "EOT" ) == 0)
break;
}
if (sz_read == (-1))
{
close(sk_dialog);
fprintf(stderr, "line %u - read() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
close(sk_dialog);
exit(0);
default:
close(sk_dialog);
}
}
close(sk_creat);
}
The client
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
extern const char* const sys_errlist[];
#define NAME_SOCKET ("socket_file" )
#define SZ_BUF (256)
main(
int argc,
char *argv[])
{
int sk_connect;
int sk_dialog;
char buf[SZ_BUF];
char *pt;
struct sockaddr_un adr_server;
memset(&adr_server, 0, sizeof(struct sockaddr_un));
adr_server.sun_len=strlen(NAME_SOCKET) + 1;
adr_server.sun_family=AF_UNIX;
memcpy(&adr_server.sun_path, NAME_SOCKET, strlen(NAME_SOCKET));
fputc('\n', stdout);
do
{
if ((sk_dialog=socket(AF_UNIX, SOCK_STREAM, 0)) == (-1))
{
fprintf(stderr, "line %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
if ((sk_connect=connect(sk_dialog, &adr_server, sizeof(struct sockaddr_un))) == (-1))
{
fprintf(stderr, "line %u - connect() - %s\n", __LINE__, sys_errlist[errno]);
sleep(5);
}
}
while (sk_connect == (-1));
printf("Connection successful\n" );
do
{
fputs("Enter string (EOT finally) :", stdout); fflush(stdout);
fflush(stdin); fgets(buf, SZ_BUF, stdin);
if ((pt=strchr(buf, '\n')) != NULL)
*pt='\0';
if (write(sk_dialog, buf, strlen(buf) + 1) == (-1))
fprintf(stderr, "line %u - write(%s) - %s\n", __LINE__, buf, sys_errlist[errno]);
}
while (strcmp(buf, "EOT" ) != 0);
close(sk_dialog);
}
Example 2: tcp communication.
The server
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
extern const char* const sys_errlist[];
#define SERVICE_LABEL ("foo" )
#define SERVICE_PROTOCOL ("tcp" )
#define SZ_BUF (256)
main(
int argc,
char *argv[])
{
ushort i;
ushort j;
int sk_creat;
int sk_dialog;
int pid;
int len_adr;
int sz_read;
char buf[SZ_BUF];
char hostname[MAXHOSTNAMELEN + 1];
struct sockaddr_in adr_server;
struct sockaddr_in adr_client;
struct hostent *host_info;
struct servent *service_info;
char *adr_ascii;
signal(SIGCHLD, SIG_IGN);
if (gethostname(hostname, MAXHOSTNAMELEN) != 0)
{
fprintf(stderr, "line %u - gethostname(%s) - %s\n", __LINE__, hostname, sys_errlist[errno]);
exit(errno);
}
printf("gethostname='%s'\n", hostname);
if ((service_info=getservbyname(SERVICE_LABEL, SERVICE_PROTOCOL)) == NULL)
{
fprintf(stderr, "line %u - getservbyname(%s, %s) - %s\n", __LINE__, SERVICE_LABEL, SERVICE_PROTOCOL, sys_errlist[errno]);
exit(errno);
}
fputc('\n', stdout);
printf("service_name='%s'\n", service_info->s_name);
for (i=0; service_info->s_aliases[i] != NULL; i++)
printf("service_s_alias[%hu]='%s'\n", i, service_info->s_aliases[i]);
printf("service_port=%u\n", ntohs(service_info->s_port));
printf("service_protocol='%s'\n", service_info->s_proto);
if ((sk_creat=socket(AF_INET, SOCK_STREAM, 0)) == (-1))
{
fprintf(stderr, "line %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
printf("Socket created\n" );
memset(&adr_server, 0, sizeof(struct sockaddr_in));
adr_server.sin_family=AF_INET;
adr_server.sin_port=service_info->s_port;
adr_server.sin_addr.s_addr=INADDR_ANY;
if (bind(sk_creat, &adr_server, sizeof(struct sockaddr_in)) == (-1))
{
fprintf(stderr, "line %u - bind() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
printf("Socket connected to the network\n" );
listen(sk_creat, 1);
fputc('\n', stdout);
while (1)
{
printf("ppid=%u, pid=%u\tAwaiting entry..", getppid(), getpid());
fflush(stdout);
len_adr=sizeof(struct sockaddr_in);
if ((sk_dialog=accept(sk_creat, &adr_client, &len_adr)) == (-1))
{
fprintf(stderr, "line %u - accept() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
fputs("Entry issued ", stdout);
if ((adr_ascii=inet_ntoa(adr_client.sin_addr)) > (char*)0)
{
printf("(adr=%s", adr_ascii);
if ((host_info=gethostbyaddr((char*)&adr_client.sin_addr.s_addr, sizeof(struct in_addr), AF_INET)) != NULL)
printf(" - %s)\n", host_info->h_name);
else
{
fputs("- ???)\n", stdout);
fprintf(stderr, "line %u - gethostbyaddr() - %s\n", __LINE__, sys_errlist[errno]);
}
}
else
{
fputs("(adr=???)\n", stdout);
fprintf(stderr, "line %u - inet_ntoa() - %s\n", __LINE__, sys_errlist[errno]);
}
switch (pid=fork())
{
case (-1):
close(sk_creat);
close(sk_dialog);
fprintf(stderr, "line %u - fork() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
case 0:
close(sk_creat);
while ((sz_read=read(sk_dialog, buf, SZ_BUF)) > 0)
{
printf("\n\tppid=%u, pid=%u\tThe server has read '%s'%s\n", getppid(), getpid(), buf, strcmp(buf, "EOT" ) != 0 ?"" :"=> End call" );
if (strcmp(buf, "EOT" ) == 0)
break;
}
if (sz_read == (-1))
{
close(sk_dialog);
fprintf(stderr, "line %u - read() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
close(sk_dialog);
exit(0);
default:
close(sk_dialog);
}
}
close(sk_creat);
}
The client
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
extern const char* const sys_errlist[];
#define SERVER_DEFAULT ("localhost" )
#define SERVICE_LABEL ("foo" )
#define SERVICE_PROTOCOL ("tcp" )
#define SZ_BUF (256)
main(
int argc,
char *argv[])
{
ushort i;
ushort j;
int sk_connect;
int sk_dialog;
char buf[SZ_BUF];
char hostname[MAXHOSTNAMELEN + 1];
char *server;
char *pt;
struct sockaddr_in adr_server;
struct hostent *host_info;
struct servent *service_info;
if (argc > 1)
server=argv[1];
else
{
server=SERVER_DEFAULT;
printf("No argument for %s - Using %s\n", *argv, SERVER_DEFAULT);
}
if (gethostname(hostname, MAXHOSTNAMELEN) != 0)
{
fprintf(stderr, "line %u - gethostname(%s) - %s\n", __LINE__, hostname, sys_errlist[errno]);
exit(errno);
}
printf("gethostname='%s'\n", hostname);
if ((host_info=gethostbyname(server)) == NULL)
{
fprintf(stderr, "line %u - gethostbyname(%s) - %s\n", __LINE__, server, sys_errlist[errno]);
exit(errno);
}
fputc('\n', stdout);
printf("host_info.h_name='%s'\n", host_info->h_name);
for (i=0; host_info->h_aliases[i] != NULL; i++)
printf("host_info.h_alias[%hu]='%s'\n", i, host_info->h_aliases[i]);
printf("host_info.h_addrtype=%u\n", host_info->h_addrtype);
printf("host_info.h_length=%u\n", host_info->h_length);
for (i=0; host_info->h_addr_list[i] != NULL; i++)
{
printf("host_info.h_addr_list[%hu]=", i);
for (j=0; j < host_info->h_length; j++)
printf("%hu ", (unsigned char)host_info->h_addr_list[i][j]);
fputc('\n', stdout);
}
if ((service_info=getservbyname(SERVICE_LABEL, SERVICE_PROTOCOL)) ==NULL)
{
fprintf(stderr, "line %u - getservbyname(%s, %s) - %s\n", __LINE__, SERVICE_LABEL, SERVICE_PROTOCOL, sys_errlist[errno]);
exit(errno);
}
fputc('\n', stdout);
printf("service_name='%s'\n", service_info->s_name);
for (i=0; service_info->s_aliases[i] != NULL; i++)
printf("service_s_alias[%hu]='%s'\n", i, service_info->s_aliases[i]);
printf("service_port=%u\n", ntohs(service_info->s_port));
printf("service_protocol='%s'\n", service_info->s_proto);
memset(&adr_server, 0, sizeof(struct sockaddr_in));
adr_server.sin_len=host_info->h_length;
adr_server.sin_family=AF_INET;
adr_server.sin_port=service_info->s_port;
memcpy(&adr_server.sin_addr.s_addr, host_info->h_addr, host_info->h_length);
fputc('\n', stdout);
do {
if ((sk_dialog=socket(AF_INET, SOCK_STREAM, 0)) == (-1))
{
fprintf(stderr, "line %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
if ((sk_connect=connect(sk_dialog, &adr_server, sizeof(struct sockaddr_in))) == (-1))
{
fprintf(stderr, "line %u - connect() - %s\n", __LINE__, sys_errlist[errno]);
sleep(5);
}
} while (sk_connect == (-1));
printf("Connection successful\n" );
do {
fputs("Enter string (EOT finally) :", stdout); fflush(stdout);
fflush(stdin); fgets(buf, SZ_BUF, stdin);
if ((pt=strchr(buf, '\n')) != NULL)
*pt='\0';
if (write(sk_dialog, buf, strlen(buf) + 1) == (-1))
fprintf(stderr, "line %u - write(%s) - %s\n", __LINE__, buf, sys_errlist[errno]);
} while (strcmp(buf, "EOT" ) != 0);
close(sk_dialog);
exit(0);
}
Bookmarks