943,740 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1012
  • C++ RSS
Feb 24th, 2009
0

socket c++

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Feb 24th, 2009
0

Re: socket c++

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem on Returning Reference to Template Object
Next Thread in C++ Forum Timeline: C++ do-while command & scoring!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC