=====Video Wall Server Program Source=====
cli-srv.c
#include "includes.h"
#include "defines.h"
void talk_to_srv(int fd)
{
char tmp[2048];
send(fd,tmp,sizeof(tmp),0);
if(send(fd,tmp,sizeof(tmp),0) == -1)
{
printf("[CLIENT-SIDE]: (send) tmp Data could not be sent at this time.\n");
exit(-1);
}
}
connectcli.c
#include "includes.h"
#include "defines.h"
int connect_to_srv(char *host, int portnum)
{
int sock;
struct sockaddr_in servadd; // the address to connect to
//struct hostent *hp; // used to get address
/* Search for a valid socket to connect to */
sock = socket(AF_INET, SOCK_STREAM, 0); // search for a valid socket
if(sock == -1) // if no valid sockets found
{
printf("[CLIENT-SIDE]: (socket) Failed to find a valid socket.\n");
return -1; // then exit with error
}
/* Connect to server */
bzero(&servadd, sizeof(servadd)); // zero out the address
hp = gethostbyname(host); // lookup server hostname/ip
if(hp == NULL) // if no valid hostname or ip
{
printf("[CLIENT-SIDE] (?) Could not find valid server host.\n");
return -1; // then exit with an error
}
bcopy(hp->h_addr,(struct sockaddr *)&servadd.sin_addr,hp->h_length);
servadd.sin_port = htons(portnum); // fill in socket port number
servadd.sin_family = AF_INET; // fill in socket family type
if(connect(sock,(struct sockaddr *)&servadd, sizeof(servadd)) != 0)
{
printf("[CLIENT-SIDE] (connect) Could not connect to server or server refused connection.\n");
return -1; // if cannot connect then exit with error
}
return sock;
}
defines.h
#define HOSTLEN 256
#define BACKLOG 1
#define ONLINE 1
int portnum;
struct sockaddr_in saddr;
struct hostent *hp;
char hostname[HOSTLEN];
int sock_id;
includes.h
#include
#include
#include
#include
#include
#include
#include
#include
#include
processreq.c
#include "includes.h"
#include "defines.h"
void processreq(int fd, int delay)
{
char tmp[2048];
recv(fd,tmp,sizeof(tmp),0);
printf("%s\n",tmp);
}
socket.c
/*
*
* socket.c - socket creation c code
*
*/
#include "includes.h"
#include "defines.h"
int msock(int portnum)
{
return create_sock(portnum,BACKLOG);
}
int create_sock(int portnum, int backlog)
{
sock_id = socket(PF_INET, SOCK_STREAM, 0);
if(sock_id == -1)
{
printf("[ERROR]: socket: creation failed.\n");
return -1;
}
bzero((void *)&saddr, sizeof(saddr));
gethostname(hostname, HOSTLEN);
hp = gethostbyname(hostname);
bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
saddr.sin_port = htons(portnum);
saddr.sin_family = AF_INET;
if(bind(sock_id, (struct sockaddr *)&saddr,sizeof(saddr)) != 0)
{
printf("[ERROR]: bind: socket bind failed!\n");
return -1;
}
if(listen(sock_id,backlog) != 0)
{
printf("[ERROR]: listen: unable to listen to socket!\n");
return -1;
}
return sock_id;
}
WOS.c
#include "includes.h"
#include "defines.h"
//#include "socket.c"
//#include "processreq.c"
int main(int argc, char *argv[])
{
int sock, fd, delay;
if(argc <= 2)
{
printf("%s: is missing argument for port number and/or time delay.\n",argv[0]);
exit(-1);
}
portnum = atoi(argv[1]);
sock = msock(portnum);
delay = atoi(argv[2]);
if(sock == -1)
{
printf("[ERROR]: socket: socket creation failed.\n");
exit(-1);
}
while(1){
fd = accept(sock,NULL,NULL);
if(fd == -1)
{
printf("[ERROR]: accept: could not accept connection to socket at this time.\n");
break;
}
processreq(fd, delay);
close(fd);
//return 0;
}
}
Makefile
all: WOS
WOS: WOS.o socket.o processreq.o connectcli.o cli-srv.o
gcc WOS.o socket.o processreq.o connectcli.o cli-srv.o -o WOS
WOS.o: WOS.c
gcc -c WOS.c
socket.o: socket.c
gcc -c socket.c
processreq.o: processreq.c
gcc -c processreq.c
connectcli.o: connectcli.c
gcc -c connectcli.c
cli-srv.o: cli-srv.c
gcc -c cli-srv.c