Switch statement in a TCP Server problem

This is a discussion on Switch statement in a TCP Server problem within the C Programming forums, part of the General Programming Boards category; Hello. I have a TCP server I am coding and having a problem with a input for my switch statment. ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    175

    Switch statement in a TCP Server problem

    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;
    }
    Last edited by csharp100; 04-27-2012 at 09:40 PM. Reason: edit out ip address

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Well solved the problem with:
    Code:
    switchInput = atoi(recv_data);
    But I am still wondering if there is a better way of doing the switch. I would like to be able to have the string come in from the client and fill in the switch statement and then the variable tempDept.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why are you comparing against the address of your received data? O_o

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Quote Originally Posted by memcpy View Post
    Why are you comparing against the address of your received data? O_o
    I'm not following you, what line are you talking about?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Hmm... My tempDept variable is of type char and my recv_data is also of type char. So why would this line not work?
    Code:
    tempDept = (recv_data);
    I am getting a compile error of "incompatible types in assignment"

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Hmm... looks like you should be using strcpy.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > My tempDept variable is of type char and my recv_data is also of type char.
    Wrong. One is a single character and one is an array.

    So, your switch statement is effectively comparing against the memory address of the first byte of the array (assuming the assignment succeeds at all).

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Ok, so now I got my info into the server, now I am trying to send it back to the client. My problem is with the send statement. I tested my case 1 earlier with this print statement:
    Code:
    printf("%d %s", data[i].course, data[i].Dept);
    and it printed out exactly what I needed. What I am trying to do is send that info through the socket to the client. Can someone show how this is done? Here is my code:
    Code:
     while(1) {
    
                bytes_received = recv(connected, recv_data, BUF, 0);
                recv_data[bytes_received] = '\0';
                switchInput = atoi(recv_data);
    
                switch(switchInput) {
    
                case 1:
    
                    fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                    fclose(filePointer);
                    char send_data[] = "Enter Department Name";
                    send(connected, send_data, strlen(send_data), 0);
                    bytes_received = recv(connected, recv_data, BUF, 0);
                    recv_data[bytes_received] = '\0';
                    strcpy(tempDept, recv_data);
                    for (i=0; i<MAX_RECORD; i++){
                        if (strcmp(tempDept, data[i].Dept)==0){
                            if(tempCourse != data[i].course){
                                send(connected, send_data, strlen(send_data), 0);
                                tempCourse = data[i].course;
                            }
    
                        }
                    }
                break;
    I tried the following statements and did not get anywhere:
    Code:
    send_data = data[i].couurse; 
    strcpy(send_data, data[i].course); 
    send_data = atoi(data[i].course);

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by memcpy View Post
    > My tempDept variable is of type char and my recv_data is also of type char.
    Wrong. One is a single character and one is an array.
    Actually, their definitions are
    Code:
    char recv_data[BUF];
    char tempDept[5];
    So they're both char arrays.

    Quote Originally Posted by memcpy View Post
    So, your switch statement is effectively comparing against the memory address of the first byte of the array (assuming the assignment succeeds at all).
    This refers to the other problem. switchInput is an int and he WAS comparing (in the switch stmt) this against the address of recv_data, but has since changed that to compare to atoi(recv_data).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Statement - Possible Infinite Loop Problem?
    By Alan Gott in forum C Programming
    Replies: 8
    Last Post: 10-25-2011, 11:01 PM
  2. Having a problem with a Switch Statement
    By ARod609 in forum C Programming
    Replies: 15
    Last Post: 07-13-2011, 06:57 PM
  3. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  4. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21