Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C sockets programming questions
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
methodtwo
Apprentice
Apprentice


Joined: 01 Feb 2008
Posts: 228

PostPosted: Sun Jan 13, 2013 1:31 am    Post subject: C sockets programming questions Reply with quote

Hi there
I'm trying to write a client and server as an exercise in learning the sockets api. The client needs to send the server 2 integers to provide X,Y coordinates for a "move" in a game of tic-tac-toe. The server just verifies that the coordinates are in range. If they're not a text string is sent back. If they are then the two coordinates are sent back to the client. If i have:
Code:
void get_player_move(void)
{

 int len;
 int n=0;

 char sendline[16], recvline[30];
 
   int x, y;
   printf("Enter X,Y coordinates for your move: ");
   scanf( "%d%d", &x, &y);
   snprintf(sendline, sizeof(sendline), "%c%c\n", x, y);

   for(;;) {
     if(n > 0) {
       printf("Enter X,Y coordinates for your move: ");
       scanf("%d%d", &x, &y);
       snprintf(sendline, sizeof(sendline), "%c%c\n", x, y);
     }
     if(sendto(sockfd, sendline, sizeof(sendline), 0, res->ai_addr, res->ai_addrlen) ==-1){
      perror("sendto failed");
      exit(1);
  }
     if(recvfrom(sockfd, recvline, sizeof(recvline), 0, res->ai_addr, &res->ai_addrlen) ==-1){
       perror("recvfrom failed");
       exit(1);
   }
   if(sscanf(recvline, "%d%d", &x, &y) == 2)
     break;
   else
     printf("%s\n", recvline);
     n++;   
  }
  matrix[x][y] = 'X';
}

for the client. And
Code:
void get_player_move(void)
{
   char recvline[16], sendline[30], textline[30];
   int x, y;

   for(;;){
   if(recvfrom(sockfd, recvline, sizeof(recvline), 0,(struct sockaddr*)&cli_addr, &cli_len) ==-1){
     perror("recvfrom error");
     exit(1);
   }

   if(sscanf(recvline, "%d%d", &x, &y) != 2){
      perror("sscanf error on server");
      exit(1);
   }
   x--; y--;
   printf("x is %d, y is %d\n", x, y);
   if(matrix[x][y] != ' '){
    snprintf(textline, sizeof(textline), "Invalid move, try again.\n");
    if(sendto(sockfd, textline, sizeof(textline), 0, (struct sockaddr*)&cli_addr,cli_len)  ==-1){
       perror("sendto failed");
       exit(1);
   }
   
   }
   else {
        matrix[x][y] = 'X';
        snprintf(sendline, sizeof(sendline), "%c%c\n", x, y);
   if(sendto(sockfd, sendline, sizeof(sendline), 0, (struct sockaddr*)&cli_addr, cli_len) ==-1){
     perror("send move back failed");
     exit(1);
    }
   else
       /*break;*/
       exit(0);
  }
 }
}

Then why does the call to sscanf fail on the server? I thought that snprintf and sscanf could format between data types(?). Could someone let me know what is wrong with this code as far as datatypes and conversion is concerned? I don't need help with the "protocol". I think blocking in recvfrom and "iterative" is fine for a udp exercise.
Thank you very much for any help


Last edited by methodtwo on Sun Jan 13, 2013 9:22 am; edited 2 times in total
Back to top
View user's profile Send private message
syn0ptik
Apprentice
Apprentice


Joined: 09 Jan 2013
Posts: 249

PostPosted: Sun Jan 13, 2013 2:55 am    Post subject: Reply with quote

Better do comparsion with non zero.
Code:
if(sscanf(recvline, "%d%d", &x, &y) != 0)
Back to top
View user's profile Send private message
methodtwo
Apprentice
Apprentice


Joined: 01 Feb 2008
Posts: 228

PostPosted: Sun Jan 13, 2013 3:22 am    Post subject: Reply with quote

Thank you very much for the reply. I didn't understand it though. I mean:
Code:
if(sscanf(recvline, "%d%d", &x, &y) != 0)

Means "if there was no failure" whereas the code i had before meant "if the values were assigned correctly" What is the benfit of doing it the "!= 0" way?. It's just the same but with the logic inverted isn't it?
Back to top
View user's profile Send private message
floppymaster
Developer
Developer


Joined: 07 Jul 2010
Posts: 218
Location: Detroit, MI, USA

PostPosted: Sun Jan 13, 2013 3:23 am    Post subject: Reply with quote

You are trying to parse two integers with nothing in between them. How is sscanf supposed to know where the first number stops and the second one starts?
Back to top
View user's profile Send private message
methodtwo
Apprentice
Apprentice


Joined: 01 Feb 2008
Posts: 228

PostPosted: Sun Jan 13, 2013 3:55 am    Post subject: Reply with quote

Thank you very much for the reply. Got it all working thanks to you. valhallas of data-flow from sweet gentoo!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum