Hello.
I have a TCP server I am coding and having a problem with a input for my switch statment. I think I know what my problem is but not sure how to correct it. My switch statment input is an int. What I am reading in from my client is of type char. Is there a way to convert that? Also is there a better way of reading in from the client and using the switch statment? I have thought about using strtok() to break up the string I am receiving from the client. If so I am open to some ideas. I have included my code to make sure that is the problem.
Code:#include "classes.h" int main(void) { int switchInput; int i = 0; int connected; int sock; int bytes_received; int sin_size; int true = 1; int tempCourse = 0; char send_data[BUF]; char recv_data[BUF]; char tempDept[5]; char tempDay[1]; char tempTime[1]; FILE *filePointer; sched_record data[MAX_RECORD]; filePointer = fopen (BINFILE, "rb"); if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror ("Socket"); exit(1); } if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)) == -1) { perror ("Setsocketopt"); exit(1); } self.sin_family = AF_INET; self.sin_port = htons(noneya); bzero(&(self.sin_zero), 8); self.sin_addr.s_addr = inet_addr ("ip address i am hiding"); if (bind (sock, (struct sockaddr *)(&self), sizeof (self)) < 0) { perror ("Unable to bind"); exit(1); } if (listen(sock, 5) == -1) { perror("Listen"); exit(1); } if (filePointer == NULL) { perror("**Can't open file**"); exit(1); } printf("\nTCPServer waiting for client on port xxxxx"); fflush(stdout); while(1) { sin_size = sizeof(struct sockaddr_in); connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); printf("\n I got a connection from (%s, %d)", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); printf("\n"); char send_data[] = "Server is Ready"; send(connected, send_data, strlen(send_data), 0); while(1) { bytes_received = recv(connected, recv_data, BUF, 0); recv_data[bytes_received] = '\0'; printf("\n%s", recv_data); switchInput = recv_data; switch(switchInput) { case 1: fread(data, sizeof(sched_record), MAX_RECORD, filePointer); fclose(filePointer); printf("Enter Dept Name: "); scanf("%s", tempDept); for (i=0; i<MAX_RECORD; i++){ if (strcmp(tempDept, data[i].Dept)==0){ if(tempCourse != data[i].course){ printf("\n%s %d", data[i].Dept, data[i].course); tempCourse = data[i].course; } } } break; case 2: fread(data, sizeof(sched_record), MAX_RECORD, filePointer); fclose(filePointer); printf("Enter Dept Name: "); scanf("%s", tempDept); printf("Enter Course Number: "); scanf("%d", &tempCourse); qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction); for (i=0; i < MAX_RECORD; i++){ if ((strcmp(tempDept, data[i].Dept)==0) && tempCourse == data[i].course){ printf("\n%d%s", data[i].start.hour, data[i].Dept); } } break; } } } close(sock); return 0; }



LinkBack URL
About LinkBacks



