I wrote this simple client/server example that passes an array of structures to the server...This is just a very simple example of passing data between the client and the server...its by no means a rigorous program
If you can find anything of use in it, please feel free to use it.
Usage:
run server ./server&
run client ./client 127.0.0.1
server.c
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <strings.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
struct mystr
{
unsigned int one;
unsigned int two;
unsigned int three;
unsigned int four;
};
#define DEFAULT_PROTOCOL 0
#define ARRSIZE 3
#define MAXLINE 7
int main(int argc, char**argv)
{
int i = 0, n = 0, j = 0;
struct mystr thestr[ARRSIZE];
char *ch = (char*)&thestr;
int listenfd, connfd, val = 1;
struct sockaddr_in servaddr;
signal(SIGCHLD, SIG_IGN);
if ((listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
{
perror("socket");
exit(EXIT_FAILURE);
}
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(50000);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((bind(listenfd, (const struct sockaddr*)&servaddr, sizeof(servaddr))) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
if ((listen(listenfd, 5)) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
for (;;)
{
connfd = accept(listenfd, NULL, NULL);
if (fork())
{
close(connfd);
}
else
{
j = 0;
while ((n = read(connfd, &ch[j], MAXLINE)) > 0)
{
j += n;
}
for (i = 0; i < 3; ++i)
{
fprintf(stdout, "\none->%d, two->%d, three->%d, four->%d\n", thestr[i].one,\
thestr[i].two, thestr[i].three, thestr[i].four);
}
exit(EXIT_SUCCESS);
}
}
exit(EXIT_SUCCESS);
}
client.c
#include <stdio.h>
#include <stdlib.h> …