/***********************************************************************/ /* */ /* SOCKSVR : TEST SOCKET SERVER */ /* */ /***********************************************************************/ #include #include #include #include #include #include #include #include #include #define TRUE 0 #define FALSE -1 #define TCP_LEN 1492 typedef struct { int BYTESPRO; int BYTESAVL; char MSGID[7]; char RESRVD; char EXCPDATA[100]; } ERRSTRUCTURE; /* Define the error return structure */ ERRSTRUCTURE errcode;/* Error Code Structure for RCVMSG */ int PORT = 400; void INZSR(void); void err_log(char* msg, int opt, char* function, int line); void main(void){ int sockfd, len, rc, newfd, on = 1; struct sockaddr_in iaddr; char buff[48]; char errmsg[128]; printf("** SOCKSVR **\n"); getchar(); INZSR(); /*-------------------------------------------------------*/ /* socket : Get a socket descriptor */ /*-------------------------------------------------------*/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0 )) < 0){ perror("SOCKET"); getchar(); exit(0); } printf("* SOCKET OK\n"); if((rc = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on))) < 0){ perror("SOCKOPT"); getchar(); exit(0); } printf("* SOCKOPT OK\n"); /*-------------------------------------------------------*/ /* bind : bind address to the socket */ /*-------------------------------------------------------*/ memset(&iaddr, 0x00, sizeof(struct sockaddr_in)); iaddr.sin_family = AF_INET; iaddr.sin_port = htons(PORT); /* PORT no set*/ iaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* any address OK */ if(bind(sockfd, (struct sockaddr *)&iaddr, sizeof(iaddr)) < 0){ perror("BIND"); getchar(); exit(0); } printf("* BIND OK\n"); /*---------------------------------------------------------*/ /* listen : wait for new client's call */ /* Up to 5 clients can be queued */ /*---------------------------------------------------------*/ rc = listen(sockfd, 10); if(rc < 0){/* LISTEN */ perror("LISTEN"); getchar(); exit(0); }/* LISTEN */ printf("* LISTEN OK\n"); /*---------------------------------------------------------*/ /* accept : accept new client's request */ /* from parent, should block */ /*---------------------------------------------------------*/ newfd = accept(sockfd, (struct sockaddr *)NULL, NULL); if(newfd < 0){/*failed*/ perror("ACCEPT"); getchar(); exit(0); }/*failed*/ printf("* ACCEPT OK\n"); /*---------------------------------------------------------*/ /* RECEV : receive message from the client */ /*---------------------------------------------------------*/ len = 48; memset(buff, 0, sizeof(buff)); rc = recv(newfd, buff, TCP_LEN, 0); if(rc < 0){ perror("RECV"); getchar(); exit(0); } printf("* RECV OK = %s\n", buff); /*---------------------------------------------------------*/ /* SEND : send the message to the client */ /*---------------------------------------------------------*/ memset(buff, 0, sizeof(buff)); strcpy(buff, "* SOCK SERVER *"); rc = send(newfd, buff, len, 0); if(rc < 0){ perror("SEND"); getchar(); exit(0); } printf("* SEND OK\n"); close(sockfd); close(newfd); printf("**************************************\n"); printf("* SOCKSVR SUCCESSFULLY COMPLETE ! *\n"); printf("**************************************\n"); getchar(); return; } /****************/ void INZSR(void) /****************/ { errcode.BYTESPRO = errcode.BYTESAVL = 0; } /********************************************************/ void err_log(char* msg, int opt, char* err_at, int line) /********************************************************/ { fprintf(stderr, "%s\n", msg); fprintf(stderr, " ->ERR AT = %s, LINE = %d\n", err_at, line); if(opt == TRUE) fprintf(stderr, "%s\n", strerror(errno)); }