Weird socket problem

This is a discussion on Weird socket problem within the Linux Programming forums, part of the Platform Specific Boards category; I wrote the following code: Code: /* prs.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> main(int ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    Weird socket problem

    I wrote the following code:


    Code:
    /* prs.c */
    #include <sys/types.h>                   
    #include <sys/socket.h>
    #include <netinet/in.h>  
    #include <netdb.h> 
    #include <stdio.h>   
    main(int argc, char *argv[]) { 
        int port, sock, serverlen; char buf[10],move1[10],move2[10],choice,howmuch;
        int n=1,score1,score2;
        struct sockaddr_in server;
        struct sockaddr *serverptr;
        struct hostent *rem;
        if (argc < 3) {    
            printf("Please give host name and port number\n"); exit(1); }
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            perror("socket"); exit(1); }
        if ((rem = gethostbyname(argv[1])) == NULL) {
            perror("gethostbyname"); exit(1); }
        port = atoi(argv[2]);              
        server.sin_family = AF_INET;                 
        bcopy((char *) rem -> h_addr, (char *) &server.sin_addr,
             rem -> h_length);
        server.sin_port = htons(port); 
        serverptr = (struct sockaddr *) &server;
        serverlen = sizeof server;
        if (connect(sock, serverptr, serverlen) < 0) {
            perror("connect"); exit(1); }
        while(1) {
            bzero(buf, sizeof buf);                      
            if(read(sock,&howmuch,sizeof(int))<0) {
                perror("read");
                exit(1);
            }
            if(read(sock, buf,howmuch) < 0) {
                perror("read");
                exit(1);
            }
            if(!strcmp(buf,"STOP")) break;
            if(!strcmp(buf,"READY")) {
                printf("Give round %d play: ",n);
                choice=getchar();getchar();
                switch(choice) { 
                case 'p':
    	howmuch=6;
    	if(write(sock,&howmuch,sizeof(int))<0){
    	    perror("read");
    	    exit(1);
    	}
                    if(write(sock,"PAPER",6) < 0) {
                        perror("read");
                        exit(1);
                    }
                    strcpy(move2,"PAPER");
                    break;
                case 'r':
    	howmuch=5;
    	if(write(sock,&howmuch,sizeof(int))<0){
                        perror("read");
                        exit(1);
                    }
                    if(write(sock,"ROCK",5) < 0) {
                        perror("read");
                        exit(1);
                    }
                    strcpy(move2,"ROCK");
                    break;
                case 's':
    	howmuch=9;
    	if(write(sock,&howmuch,sizeof(int))<0){
                        perror("read");
                        exit(1);
                    }
                    if(write(sock,"SCISSORS",9)<0) {
                        perror("read");
                        exit(1);
                    }
                    strcpy(move2,"SCISSORS");
                    break;
                default:
                    printf("Invalid choice\n");
                } 
            }
            bzero(move1,sizeof move1);
            printf("%s\n",move2);             /* Here it prints as it should */
            fflush(stdout);
            if(read(sock,&howmuch,sizeof(int))<0) {
                perror("read");
                exit(1);
            }
            printf("\"%s\"\n",move2);       /* Here it doesn't print anything  */
            if(read(sock,move1,howmuch)<0) {
                perror("read");
                exit(1);
            }
            printf("Player 1:%10s  Player2:%10s \n",move1,move2);
            n++;
        }
        if(read(sock,&score1,sizeof(int))<0) {
            perror("read");
            exit(1);
        }
        if(read(sock,&score2,sizeof(int))<0) {
            perror("read");
            exit(1);
        }
        printf("Score = %d - %d\n",score1,score2);
        if(score1==score2) printf("Nobody won\n");
            else printf("You %s\n",(score1>score2?"lost":"won"));
        close(sock);            
        exit(0);
    }

    The problem comes in for the second printf of move2.
    The command :
    if(read(sock,&howmuch,sizeof(int))<0) {
    perror("read");
    exit(1);
    }

    affects the variable move2.Why does this happen?
    If I don't put the command above it prints move2.
    What is really happening?
    Last edited by k_w_s_t_a_s; 06-05-2003 at 03:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket or send() problem?
    By Achy in forum C Programming
    Replies: 5
    Last Post: 06-09-2006, 01:09 PM
  2. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  3. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  4. Weird connection problem.
    By tilex in forum Networking/Device Communication
    Replies: 6
    Last Post: 02-06-2005, 09:11 AM
  5. Weird mouse problem in XP
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 06-16-2004, 11:47 AM

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