connecting client to server

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 44
Reputation: c++ prog is an unknown quantity at this point 
Solved Threads: 1
c++ prog's Avatar
c++ prog c++ prog is offline Offline
Light Poster

connecting client to server

 
0
  #1
Oct 8th, 2008
hi, my problem is about socket programming. My code below only connects the client to the server, and it works just fine. However, I should create a 2player game which of course require the interaction of the 1st and 2nd player. The problem here is that they really don't interact with each other, it is as if the terminal for the client side is a separate program from the terminal for the server side(sorry if I can't explain it clearly..lol) yeah, the client connected successfully, but they don't interact..pls I really need this code or else I'll fail my subject..tnx a lot!

Client-side
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <stdio.h>
  8. #include <termios.h>
  9. #include <unistd.h>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. const int totchan=7;
  15.  
  16. void error(char *msg)
  17. {
  18. perror(msg);
  19. exit(0);
  20. }
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24. int sockfd, portno, n, input;
  25. struct sockaddr_in serv_addr;
  26. struct hostent *server;
  27.  
  28. char buffer[256];
  29. if (argc < 3) {
  30. fprintf(stderr,"usage %s hostname port\n", argv[0]);
  31. exit(0);
  32. }
  33. portno = atoi(argv[2]);
  34. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  35. if (sockfd < 0)
  36. error("ERROR opening socket");
  37. server = gethostbyname(argv[1]);
  38. if (server == NULL) {
  39. fprintf(stderr,"ERROR, no such host\n");
  40. exit(0);
  41. }
  42. bzero((char *) &serv_addr, sizeof(serv_addr));
  43. serv_addr.sin_family = AF_INET;
  44. bcopy((char *)server->h_addr,
  45. (char *)&serv_addr.sin_addr.s_addr,
  46. server->h_length);
  47. serv_addr.sin_port = htons(portno);
  48.  
  49. if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
  50. error("ERROR connecting");
  51.  
  52.  
  53. cout<<"+--------------------------------------+"<<endl;
  54. cout<<"| This is a shooting game. You must |"<<endl;
  55. cout<<"| type as many as you can, the |"<<endl;
  56. cout<<"| letters on the screen that you can |"<<endl;
  57. cout<<"| see. The player with the most |"<<endl;
  58. cout<<"| number of points wins the game! |"<<endl;
  59. cout<<"| Each player only has 3 lives, |"<<endl;
  60. cout<<"| so if it runs out, you lose! |"<<endl;
  61. cout<<"+--------------------------------------+"<<endl;
  62.  
  63. string blocks[25][25];
  64. string player1="^^^";
  65. string player2="^^^";
  66.  
  67. //blah blah blah this is the main game

server-side
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <stdio.h>
  8. #include <termios.h>
  9. #include <unistd.h>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. const int totchan=7;
  15.  
  16. void error(char *msg)
  17. {
  18. perror(msg);
  19. exit(1);
  20. }
  21.  
  22.  
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26. int sockfd, newsockfd, portno, clilen;
  27. char buffer[256];
  28. struct sockaddr_in serv_addr, cli_addr;
  29. int n,input;
  30. if (argc < 2) {
  31. fprintf(stderr,"ERROR, no port provided\n");
  32. exit(1);
  33. }
  34. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  35. if (sockfd < 0)
  36. error("ERROR opening socket");
  37. bzero((char *) &serv_addr, sizeof(serv_addr));
  38. portno = atoi(argv[1]);
  39. serv_addr.sin_family = AF_INET;
  40. serv_addr.sin_addr.s_addr = INADDR_ANY;
  41. serv_addr.sin_port = htons(portno);
  42. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  43. sizeof(serv_addr)) < 0)
  44. error("ERROR on binding");
  45. listen(sockfd,5);
  46. clilen = sizeof(cli_addr);
  47. newsockfd = accept(sockfd,
  48. (struct sockaddr *) &cli_addr, (socklen_t *)
  49. &clilen);
  50. if (newsockfd < 0)
  51. error("ERROR on accept");
  52.  
  53.  
  54. cout<<"+--------------------------------------+"<<endl;
  55. cout<<"| This is a shooting game. You must |"<<endl;
  56. cout<<"| type as many as you can, the |"<<endl;
  57. cout<<"| letters on the screen that you can |"<<endl;
  58. cout<<"| see. The player with the most |"<<endl;
  59. cout<<"| number of points wins the game! |"<<endl;
  60. cout<<"| Each player only has 3 lives, |"<<endl;
  61. cout<<"| so if it runs out, you lose! |"<<endl;
  62. cout<<"+--------------------------------------+"<<endl;
  63.  
  64. string blocks[25][25];
  65. string player1="^^^";
  66. string player2="^^^";
  67.  
  68.  
  69. //blah blah blah this is the main game
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: connecting client to server

 
0
  #2
Oct 8th, 2008
ok, what u posted is just the connection part.
What do you mean they don't interract? I mean, u know u have to send messages from server to client and from client to server via send, recv || write, read.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: connecting client to server

 
0
  #3
Oct 8th, 2008
try posting the entire problem ... i mean your assignment requirements and how you thought about implementig it. Then I will probably be able to help you with it a bit more.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: c++ prog is an unknown quantity at this point 
Solved Threads: 1
c++ prog's Avatar
c++ prog c++ prog is offline Offline
Light Poster

Re: connecting client to server

 
0
  #4
Oct 8th, 2008
yah...that's what my problem is...I cant send & receive...how can I?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: connecting client to server

 
0
  #5
Oct 8th, 2008
Originally Posted by c++ prog View Post
yah...that's what my problem is...I cant send & receive...how can I?
It works just fine for me. I just added the followng lines to your program:
client side:

char buff[16] = "mamaaremere";
send( sockfd, buff, 16, 0 );

server side:

char buff[16];
int howmany = recv( newsockfd, buff, 16, 0 );
cout<<howmany<<" "<<buff<<endl;

just as expected... the server prints the recived message: mamaaremere
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: c++ prog is an unknown quantity at this point 
Solved Threads: 1
c++ prog's Avatar
c++ prog c++ prog is offline Offline
Light Poster

Re: connecting client to server

 
0
  #6
Oct 8th, 2008
hmmm..im almost there!!tnx!! do you know how to read & write???
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC