/***********************************************************************/ /* */ /* SOCKCLT : SOCK CLIENT */ /* */ /***********************************************************************/ #include #include #include #include #include #include #include #include #include #define TRUE 0 #define FALSE -1 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 */ char addr[11] = "192.168.1.1"; int PORT = 400; void INZSR(void); void err_log(char* msg, int opt, char* function, int line); void main(void){ int sockfd, len, rc; struct sockaddr_in iaddr; char buff[48]; printf("** SOCKCLT **\n"); getchar(); INZSR(); /*-------------------------------------------------------*/ /* socket : Get a socket descriptor */ /*-------------------------------------------------------*/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0 )) < 0){ perror("SOCKET"); getchar(); exit(0); } /*-----------------------------------------------------------*/ /* connect: connect to the server using a set of port number */ /*-----------------------------------------------------------*/ 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 */ rc = connect(sockfd, (struct sockaddr *)&iaddr, sizeof(iaddr)); if(rc < 0){ perror("CONNECT"); getchar(); exit(0); } /*-----------------------------------------------------------*/ /* send a message to the server using secure session */ /*-----------------------------------------------------------*/ memset(buff, 0, sizeof(buff)); strcpy(buff, "* CLIENT 1ST MESSAGE USING SOCKET"); len = strlen(buff); rc = send(sockfd, buff, len, 0); if(rc != len){ if(rc < 0){ perror("SEND"); getchar(); exit(0); } else{ printf("SEND did not all data\n"); getchar(); exit(0); } } else{ printf("SEND OK = %s\n", buff); } /*-----------------------------------------------------------*/ /* receive the message from the server using SOCKET */ /*-----------------------------------------------------------*/ memset(buff, 0, sizeof(buff)); rc = recv(sockfd, buff, len, 0); if(rc < 0){ perror("RECV"); getchar(); exit(0); } printf("RECV OK = %s\n", buff); close(sockfd); printf("**************************************\n"); printf("* SOCKCLT SUCCESSFULLY COMPLETE ! *\n"); printf("**************************************\n"); getchar(); } /****************/ 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)); }