socket c++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

socket c++

 
0
  #1
Feb 24th, 2009
So basically I am creating a client/server kind of program. The client/user enters a question eg "Who are you? "
The server part receives this and checks it with a txt file and returns eg "I am a computer" or "Answer not found"

So when i compile and run my program, I type in the question but nothing else happens, the prompt just stays there and no output is seen.

My text file is something like this
What are you?:I am a computer.

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <string>
  9. #include <fstream>
  10. #include <vector>
  11. #include <cstdlib>
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. enum { RECV_PORT = 5000, MSGSIZE = 1024 };
  16. const char fileName[30] = "file.txt";
  17. char* s;
  18. char input[255];
  19. char* result = NULL;
  20. string qns, ans;
  21. socklen_t addr_len = sizeof(sockaddr) ;
  22.  
  23. int server( int socket_fd )
  24. {
  25. sockaddr_in my_addr ;
  26. memset( &my_addr, 0, sizeof(my_addr) ) ;
  27. my_addr.sin_family = AF_INET ;
  28. my_addr.sin_port = htons( RECV_PORT ) ;
  29. my_addr.sin_addr.s_addr = INADDR_ANY ;
  30.  
  31. if ( bind( socket_fd, (sockaddr*)&my_addr, addr_len ) != 0 )
  32. return 2 ;
  33.  
  34. ifstream infile(fileName);
  35. while( true )
  36. {
  37. //infile.open(fileName);
  38.  
  39. char recv_data[MSGSIZE+1] ;
  40. sockaddr_in client_addr ;
  41.  
  42. int bytes_recd = recvfrom( socket_fd, recv_data, MSGSIZE, 0,
  43. (sockaddr*)&client_addr, &addr_len ) ;
  44. if( bytes_recd == -1 )
  45. break ;
  46.  
  47. else
  48.  
  49.  
  50. recv_data[bytes_recd] = '\0' ;
  51.  
  52. {
  53.  
  54. infile.getline(input,255, '\n');
  55.  
  56. vector<string> parts;
  57. string tmpstr(input);
  58. s = input;
  59.  
  60. result = strtok(s, ":");
  61.  
  62. while (result != NULL)
  63. {
  64. if (result != "" && result != ":")
  65. {
  66. parts.push_back(result);
  67. }
  68.  
  69. result = strtok(NULL, ":");
  70. }
  71.  
  72. qns = parts[0];
  73. ans = parts[1];
  74.  
  75. }
  76. string tmpvar(recv_data);
  77.  
  78. cout << "from " << inet_ntoa(client_addr.sin_addr)
  79. << ':' << ntohs(client_addr.sin_port) << " - "
  80. << recv_data << endl ;
  81.  
  82. if (tmpvar == qns)
  83. cout << ans << endl;
  84.  
  85.  
  86. }
  87. return 0 ;
  88. }
  89.  
  90. int client( int socket_fd )
  91. {
  92. cout << "Enter Address to connect: " ;
  93. string address ;
  94. cin >> address >> ws ;
  95.  
  96. sockaddr_in peer_addr ;
  97. memset( &peer_addr, 0, sizeof(peer_addr) ) ;
  98. peer_addr.sin_family = AF_INET ;
  99. peer_addr.sin_port = htons( RECV_PORT ) ;
  100. peer_addr.sin_addr.s_addr =
  101. *(in_addr_t*)(gethostbyname( address.c_str() )->h_addr) ;
  102.  
  103. string send_str ;
  104. while( std::getline( std::cin, send_str ) )
  105. {
  106. send_str.resize(MSGSIZE) ;
  107. sendto( socket_fd, send_str.c_str(), MSGSIZE, 0,
  108. (sockaddr*)&peer_addr, addr_len ) ;
  109. }
  110. return 0 ;
  111. }
  112.  
  113. int main()
  114. {
  115. int socket_fd = socket( AF_INET, SOCK_DGRAM, 0 ) ;
  116. if( socket_fd == -1 ) return 1 ;
  117. return fork() == 0 ? server( socket_fd ) : client( socket_fd ) ;
  118. }
Last edited by number87; Feb 24th, 2009 at 6:03 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: socket c++

 
0
  #2
Feb 24th, 2009
1) make sure you are adding a \n to the end of the data stream (I've found it to work).
2) Install wireshark. Then you can sniff the traffic on that port, and check all incoming and outgoing (ehem) traffic.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 708 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC