Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

C sockets programming questions

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
methodtwo
Apprentice
Apprentice
Posts: 231
Joined: Fri Feb 01, 2008 4:34 pm

C sockets programming questions

  • Quote

Post by methodtwo » Sun Jan 13, 2013 1:31 am

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: Select all

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: Select all

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.
Top
syn0ptik
Apprentice
Apprentice
User avatar
Posts: 267
Joined: Wed Jan 09, 2013 9:44 am

  • Quote

Post by syn0ptik » Sun Jan 13, 2013 2:55 am

Better do comparsion with non zero.

Code: Select all

if(sscanf(recvline, "%d%d", &x, &y) != 0)
Top
methodtwo
Apprentice
Apprentice
Posts: 231
Joined: Fri Feb 01, 2008 4:34 pm

  • Quote

Post by methodtwo » Sun Jan 13, 2013 3:22 am

Thank you very much for the reply. I didn't understand it though. I mean:

Code: Select all

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?
Top
floppymaster
Developer
Developer
User avatar
Posts: 238
Joined: Wed Jul 07, 2010 1:05 am
Location: Detroit, MI, USA

  • Quote

Post by floppymaster » Sun Jan 13, 2013 3:23 am

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?
Top
methodtwo
Apprentice
Apprentice
Posts: 231
Joined: Fri Feb 01, 2008 4:34 pm

  • Quote

Post by methodtwo » Sun Jan 13, 2013 3:55 am

Thank you very much for the reply. Got it all working thanks to you. valhallas of data-flow from sweet gentoo!
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Authors
Gentoo is a trademark of the Gentoo Foundation, Inc. and of Förderverein Gentoo e.V.
The contents of this document, unless otherwise expressly stated, are licensed under the CC-BY-SA-4.0 license.
The Gentoo Name and Logo Usage Guidelines apply.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy