socket programming

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

socket programming

 
0
  #1
Oct 6th, 2008
this code is a game that uses socket to be able to connect to each other so that there would be a 2player game.. but there something wrong here...here's the code..i'm using linux as my operating system ..this is the client side..
  1. #include <stdio.h>
  2. #include <iostream>
  3. //#include <conio.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9.  
  10. using namespace std;
  11.  
  12. void error(char *msg)
  13. {
  14. perror(msg);
  15. exit(0);
  16. }
  17.  
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. int sockfd,portno, n, n1;
  22. struct sockaddr_in serv_addr;
  23. struct hostent *server;
  24.  
  25.  
  26. char buffer[256];
  27. if (argc < 3) {
  28. fprintf(stderr,"usage %s hostname port\n", argv[0]);
  29. exit(0);
  30. }
  31. portno = atoi(argv[2]);
  32. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  33. if (sockfd < 0)
  34. error("ERROR opening socket");
  35. server = gethostbyname(argv[1]);
  36. if (server == NULL) {
  37. fprintf(stderr,"ERROR, no such host\n");
  38. exit(0);
  39. }
  40. bzero((char *) &serv_addr, sizeof(serv_addr));
  41. serv_addr.sin_family = AF_INET;
  42. bcopy((char *)server->h_addr,
  43. (char *)&serv_addr.sin_addr.s_addr,
  44. server->h_length);
  45. serv_addr.sin_port = htons(portno);
  46. //if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
  47. // error("ERROR connecting");
  48. //if (connect(sockfd, &serv_addr,sizeof(serv_addr)) < 0)
  49. //error("ERROR connecting");
  50. printf("Welcome to Battle of Jack 'en Poy\n");
  51. printf("Please enter your name: ");
  52. bzero(buffer,256);
  53. fgets(buffer,255,stdin);
  54. n = write(sockfd,buffer,strlen(buffer));
  55. bzero(buffer,256);
  56. n = write(sockfd," ! ",3);
  57.  
  58. if (n < 0)
  59. error("ERROR writing to socket");
  60. bzero(buffer,256);
  61. n = read(sockfd,buffer,255);
  62. if (n < 0)
  63. error("ERROR reading from socket");
  64. printf("%s\n",buffer);
  65. return 0;
  66.  
  67.  
  68. }

this is the server side..
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <time.h>
  6. #include <windows.h>
  7. #include <conio.h>
  8.  
  9. using namespace std;
  10.  
  11. #define splayer 1
  12. #define cplayer 0
  13.  
  14.  
  15. char buffer_name[256];
  16. int newsockfd;
  17. int n;
  18.  
  19. void error(char *msg)
  20. {
  21. perror(msg);
  22. exit(1);
  23. }
  24.  
  25. void game();
  26.  
  27. string player1="^^^";
  28. string player2="^^^";
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32.  
  33. int sockfd, newsockfd, portno, clilen, pid;
  34. struct sockaddr_in serv_addr, cli_addr;
  35.  
  36. if (argc < 2) {
  37. fprintf(stderr,"ERROR, no port provided\n");
  38. exit(1);
  39. }
  40. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  41. if (sockfd < 0)
  42. error("ERROR opening socket");
  43. bzero((char *) &serv_addr, sizeof(serv_addr));
  44. portno = atoi(argv[1]);
  45. serv_addr.sin_family = AF_INET;
  46. serv_addr.sin_addr.s_addr = INADDR_ANY;
  47. serv_addr.sin_port = htons(portno);
  48. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  49. sizeof(serv_addr)) < 0)
  50. error("ERROR on binding");
  51. listen(sockfd,5);
  52. clilen = sizeof(cli_addr);
  53. while (1) {
  54.  
  55. if (newsockfd < 0)
  56. error("ERROR on accept");
  57. pid = fork();
  58. if (pid < 0)
  59. error("ERROR on fork");
  60. if (pid == 0) {
  61. close(sockfd);
  62. dostuff();
  63. exit(0);
  64. }
  65. else close(newsockfd);
  66. }
  67. return 0;
  68.  
  69. //game();
  70.  
  71. }
  72.  
  73. void game()
  74. {
  75. cout<<"+--------------------------------------+"<<endl;
  76. cout<<"| This is a shooting game. You must |"<<endl;
  77. cout<<"| type as many as you can, the |"<<endl;
  78. cout<<"| letters on the screen that you can |"<<endl;
  79. cout<<"| see. The player with the most |"<<endl;
  80. cout<<"| number of points wins the game! |"<<endl;
  81. cout<<"| Each player only has 3 lives, |"<<endl;
  82. cout<<"| so if it runs out, you lose! |"<<endl;
  83. cout<<"+--------------------------------------+"<<endl;
  84.  
  85. string blocks[25][25];
  86. //string player1="^^^";
  87. //string player2="^^^";
  88.  
  89. //BLAH BLAH BLAH BLAH BLAH BLAH BLAH
  90. //THIS IS THE MAIN GAME
you know what people...my professor didn't taught us socket..he simply game us this project..so any help would be appreciated..tnx
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,854
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 119
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: socket programming

 
0
  #2
Oct 6th, 2008
Are you being able to communicate from client to server ? what errors are u getting ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: socket programming

 
0
  #3
Oct 6th, 2008
"something wrong" is not a description.
There are plenty of coding errors which may be a problem in the end, but which may not be the most immediate problem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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