All pastes #364229 Raw Edit

Anonymous

public c v1 · immutable
#364229 ·published 2007-02-20 00:18 UTC
rendered paste body
#include <string.h>#include <stdlib.h>#include <stdio.h>#include <netdb.h>#include <netinet/in.h>#include <fcntl.h>#include "x.h"// network destription#define NETWORKDESC "testNEt"// servernamechar *SERVERNAME="irc.eu.localhost.noexist.net";// maximum connections//// TODO: reuse expired sockets#define MAXCONNECT 8 /*The maximum number of connections*/// socket setting#define SERVERHOST "localhost"#define PORTNUMBER 7072#define CONNECTQUEUE 3#define BUFFERLEN 1#define VERSION "Fernandez-1.0"// some globalsint clientfd[MAXCONNECT],    nextclient=0,    clientstatus[MAXCONNECT]; // clientstatus array, 0 = new connection; 1 = registered;main(int parameters,char **parameter){        printf("%s %s\r\n",__DATE__, __TIME__);        struct sockaddr_in serv_addr;        struct hostent *server;        int sockfd;        sockfd=socket(AF_INET,SOCK_STREAM,0); //Obviously we should check for errors, here, but I wanted to keep the code to a minimum (Error = <0).        server=gethostbyname(SERVERHOST);        bzero((char*)&serv_addr,sizeof(serv_addr));        serv_addr.sin_family=AF_INET;//explain me        bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);        serv_addr.sin_port=htons(PORTNUMBER);        char *buffer[MAXCONNECT][1024];        bzero(buffer, 1024*MAXCONNECT);        int bufferI[MAXCONNECT];        void *gbuffer = malloc(1024);// temp storage of completed line of text        bzero((char *)gbuffer, 1024);        puts("Started server\n");        serv_addr.sin_addr.s_addr=INADDR_ANY; //Allow connection from anyone        bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));        listen(sockfd,CONNECTQUEUE);        if (sockfd == 0) { puts("unable to bind\n"); exit(1); }        fcntl(sockfd, F_SETFL, O_NONBLOCK); //Don't block while waiting        for(;;) //Loop forever        {                char data[BUFFERLEN]; //The data received                bzero(data, BUFFERLEN);                int length,a,b; //The variable to loop around each client when echoing                struct sockaddr_in cli_addr; //A structure to store the client details                int newsockfd,clilen=sizeof(cli_addr); //It's size is needed by accept                newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);                if(newsockfd!=-1)                {                        clientfd[nextclient++]=newsockfd; //Make a note of the new FD in the array                        bufferI[nextclient-1] = 0;                        clientstatus[nextclient-1] = 0;                        fcntl(newsockfd,F_SETFL,O_NONBLOCK); //Don't block while waiting//                      doConnect(nextclient-1);                }                for(a=0;a<nextclient;a++)                {                        if(length=read(clientfd[a],data,BUFFERLEN)) //Get the data                        {                                if (length == 1)                                {                                        bcopy(&data, &buffer[a][bufferI[a]], 1);                                        bufferI[a]++;                                }                                for(b=0;b<nextclient;b++)                                if((clientfd[b]))                                  if(length==1)                                    if(*data == 10)                                    {                                        memcpy(gbuffer, (char *)buffer[a], 1024);                                        write(clientfd[b],(char *)gbuffer,1024);                                    }                                // flush now all data is sent globally                                if(*data == 10)                                {                                        bzero(buffer[a], 1024);                                        bufferI[a] = 0;                                }                        }                        else usleep(10);                }        }}void doConnect(int id){        // show connected to server but need to register message to client connecting        char *auth[1024];        sprintf((char*)auth, ":%s NOTICE AUTH :*** Connected, please now register...\n", SERVERNAME);        write(clientfd[id],auth,strlen((char *)auth));}void parseLine(int id, char *line){        clientstatus[id] = 0;        //clientfd[id];}/*:SERVERNAME NOTICE AUTH :*** Connected, please now register...:SERVERNAME 451 nickname :You have not registered:SERVERNAME 433 * destname :Nickname is already in use.:SERVERNAME 001 nickname :Welcome to the SERVERDESC IRC Network nick!ident@hostname:SERVERNAME 002 nickname :Your host is NETWORKDESC, running version VERSION:SERVERNAME 003 nickname :This server was created __DATE__ at __TIME__ gmt??:SERVERNAME 004 nickname SERVERNAME VERSION xi oaqvh*/