All pastes #362458 Raw Edit

Stuff

public c v1 · immutable
·published 2007-02-18 20:58 UTC
rendered paste body
jdp@localhost ~/src/src $ cat code.c#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <unistd.h>#include <fcntl.h>#include "string.c"#define C_NICK_LEN      15#define C_SLOTS         20struct id{int fd; char nick[C_NICK_LEN];};struct id IDA[C_SLOTS];void error(char *msg){        perror(msg);        exit(1);}int main(int argc, char *argv[]){        int sockfd, newsockfd, portno, clilen, n;        char buffer[1025];        char cache[1025]; // not implemented        struct sockaddr_in serv_addr, cli_addr;        if (argc < 2)        {                fprintf(stderr,"ERROR, no port provided?\n");                exit(1);        }        sockfd = socket(AF_INET, SOCK_STREAM, 0);        if (sockfd < 0)                error("ERROR, unable to create socket");        bzero((char *) &serv_addr, sizeof(serv_addr));        portno = atoi(argv[1]);        serv_addr.sin_family = AF_INET;        serv_addr.sin_port = htons(portno);        serv_addr.sin_addr.s_addr = INADDR_ANY;        if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <0)                        error("ERROR, unable to bind");        listen(sockfd,5);        clilen = sizeof(cli_addr);        fcntl(sockfd, F_SETFL, O_NONBLOCK);        while (1)        {                newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);                if (newsockfd < 0)                {                        // we will just keep looping i guess                        sleep(1); //replace with php's usleep() eqiv if it exists                }                else                {                        // ok we were able to accept the socket                        bzero(buffer,1025);                        n = read(newsockfd, buffer,1024);                        // nTable[];                        if (n<0) error("ERROR, unable to read from socket");                        //tg(buffer,1)                        //// ok so what to do ?                        //// character array max length 8 to allow for null termination                        // PRIVMSG                        // JOIN                        // NOTICE                        // PART                        // PONG                        // QUIT                        char *temp = malloc(strlen(buffer)-5);                        sscanf(buffer, "NICK %s\n",temp);                        printf("egg\n");//                      sprintf("thanks: %s\n", temp);                        //printf("spood %s\n", sprintf("thanks: %s\n", temp));                        char tch = buffer[strlen(buffer)-1];                        //if (tch !== 10) {/* not thingied */}//                              printf("NEW LINE!\r\n");                        n = write(newsockfd, buffer, strlen(buffer));                        if (n < 0) error("ERROR, while writing to socket\n");                        close(newsockfd);                }//if                //printf(".\n");        }//while        return 0;}