I have a dumb little library I started for a game I’m working on.
Here it is. Not much to it yet, but it might be a place to start.
I’ve compiled it on the following platforms with the compile options:
IRIX62: gcc -o network -DTEST network.c
Linux2.0.34: gcc -o network -DTEST network.c
SunOS5.5: gcc -o network -DTEST network.c -lsocket -lnsl
Haven’t had a chance to try to compile it on other platforms.
Kent Mein
------------------------network.c-----------------------------------
/* #include <stream.h> */
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <arpa/inet.h>
#define SERVER 1
#define NETSERVER 2
#define CLIENT 3
#define MAXBUFFSIZE 1024
#define NUM_WAITING_CLIENTS 5
#define MAXUSERS 10
struct sockaddr_in node;
struct hostent *host;
char mybuff[MAXBUFFSIZE];
int done=0;
int port=9999;
char server[200]=“localhost”;
int init_networktcp();
int server_modetcp(int SD);
int client_modetcp(int SD);
int server_modesingle();
int myrecvtcp(int SD, char *mybuff);
int mysendtcp(int SD, char *mybuff);
void handle_userstcp(int *USERS,int size);
struct sockaddr_in get_servertcp(int SD);
void bcastpkttcp(char *pkt, int SD,int *USERS,int num_users);
int init_networktcp() {
int SD,i;
host = (struct hostent *)gethostbyname(server);
if (host ==(struct hostent *)NULL) {
printf(“Error lookingup hostname.\n”);
return -1;
}
bzero(&node, sizeof(node));
node.sin_family =host->h_addrtype;
if (port < 1) node.sin_port = htons(9119);
else node.sin_port = htons(port);
node.sin_addr.s_addr = htonl(INADDR_ANY);
printf(“Creating a socket\n”);
if ((SD=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
printf(“Error in creating a socket.\n”);
return -1;
}
i=1;
if (setsockopt(SD,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i)) < 0) {
printf(“Error in modifying socket.\n”);
return -1;
}
printf(“Binding to a socket\n”);
if (bind(SD, (struct sockaddr *)&node, sizeof(node)) < 0) {
printf(“Error in binding to a socket.\n”);
close(SD);
return -1;
}
return SD;
}
int mysendtcp(int SD, char *mybuff) {
return send(SD,mybuff,sizeof(char) * MAXBUFFSIZE,0);
}
int myrecvtcp(int SD, char *mybuff) {
return recv(SD,mybuff,sizeof(char) * MAXBUFFSIZE,0);
}
/* send the pkt to everybody except on incoming socket */
void bcastpkttcp(char *pkt, int SD,int *USERS,int num_users) {
int i;
for (i = 0; i < num_users; i++) {
if (SD != USERS[i]) mysendtcp( USERS[i], pkt );
}
}
/* Searches a given subnet for a server and connects to it.
*/
struct sockaddr_in get_servertcp(int SD) {
char *message;
int myaddr[4];
mysendtcp(SD, “ATTACH”);
/* figure out hub and return it I think??? */
}
int client_modetcp(int SD) {
struct sockaddr_in hub;
hub=get_servertcp(SD);
if (connect(SD,(struct sockaddr *)&hub,sizeof(hub)) < 0) {
printf("Error connecting to server.\n");
return 0;
}
while (!done) {
}
return 1;
}
void handle_userstcp(int *USERS,int size) {
int x;
struct timeval tv;
fd_set readset;
char mybuff[MAXBUFFSIZE];
tv.tv_sec=3;
tv.tv_usec =0;
for (x=0;x<size;x++) {
FD_SET(USERS[x],&readset);
if (FD_ISSET(USERS[x],&readset)) {
myrecvtcp(USERS[x],mybuff);
printf(“Message comming in: %s\n”, mybuff);
}
}
}
int server_modetcp(int SD) {
int c,
newclient,
num_users=0,
USERS[MAXUSERS];
char mybuff[MAXBUFFSIZE];
fd_set readset;
printf(“Listening to socket\n”);
if (listen(SD,NUM_WAITING_CLIENTS) < 0 ) {
printf(“Maximum number of clients reached.\n”);
close(SD);
return -1;
}
FD_ZERO(&readset);
FD_SET(STDIN_FILENO, &readset);
FD_SET(SD, &readset);
while (!done) {
/* loop through connected users */
handle_userstcp(USERS,num_users);
/* look for new connections */
while (FD_ISSET(SD, &readset)) {
if ((newclient = accept(SD,NULL,NULL)) < 0) {
printf("Problem connecting\n");
} else {
printf("Got a connection\n");
myrecvtcp(newclient,mybuff);
printf("buff = %s\n", mybuff);
if (strncmp(mybuff,"LOOKING",7) == 0) {
/* add this guy to server */
if (num_users < MAXUSERS) {
USERS[num_users]=newclient;
num_users++;
} else {
close(newclient);
}
} else if (strncmp(mybuff,"QUIT",4) == 0) {
close(newclient);
}
}
}
}
printf(“Exiting server loop\n”);
return 1;
}
int server_modesingle() {
while (!done) {
}
return 1;
}
#ifdef TEST
int main() {
int SD= init_networktcp();
if (SD < 0) {
exit(1);
}
if (SERVER) {
printf(“Entering Server_Mode\n”);
server_modetcp(SD);
} else {
printf(“Entering Client_Mode\n”);
client_modetcp(SD);
}
return 1;
}
#endif