Client/Server sockets and reading a file

Please support our C++ advertiser: Intel Parallel Studio Home
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

Client/Server sockets and reading a file

 
0
  #1
Feb 15th, 2009
So, what Im trying to do here is a client server socket program. The client will ask a question to the server. For example, How are you?
Then the server opens a text file, compares the string from the client and outputs the answer to the client.

My text file is something like this.
How are you? I am fine.

so client asks How are you?
server outputs to client I am fine.

So far I have tried compiling my code and there seems to be a compilation error. If I do not include the file reading and string checking, the client/server socket code works fine. As in I can make the server repeat whatever the client inputs.

  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. std::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. fstream 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. recv_data[bytes_recd] = '\0' ;
  49.  
  50. {
  51. infile.getline(input,255, '\n');
  52.  
  53. while(!infile.eof())
  54. {
  55.  
  56. vector<string> parts;
  57. std::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. if (recv_data.compare(qns)==0)
  76. std::cout << ans << std::endl;
  77. }
  78.  
  79.  
  80. }
  81.  
  82. std::cout << "from " << inet_ntoa(client_addr.sin_addr)
  83. << ':' << ntohs(client_addr.sin_port) << " - "
  84. << recv_data << std::endl ;
  85. }
  86. return 0 ;
  87. }
  88.  
  89. int client( int socket_fd )
  90. {
  91. std::cout << "Enter Address to connect: " ;
  92. std::string address ; std::cin >> address >> std::ws ;
  93.  
  94. sockaddr_in peer_addr ;
  95. std::memset( &peer_addr, 0, sizeof(peer_addr) ) ;
  96. peer_addr.sin_family = AF_INET ;
  97. peer_addr.sin_port = htons( RECV_PORT ) ;
  98. peer_addr.sin_addr.s_addr =
  99. *(in_addr_t*)(gethostbyname( address.c_str() )->h_addr) ;
  100.  
  101. std::string send_str ;
  102. while( std::getline( std::cin, send_str ) )
  103. {
  104. send_str.resize(MSGSIZE) ;
  105. sendto( socket_fd, send_str.c_str(), MSGSIZE, 0,
  106. (sockaddr*)&peer_addr, addr_len ) ;
  107. }
  108. return 0 ;
  109. }
  110.  
  111. int main()
  112. {
  113.  
  114.  
  115.  
  116.  
  117. int socket_fd = socket( AF_INET, SOCK_DGRAM, 0 ) ;
  118. if( socket_fd == -1 ) return 1 ;
  119. return fork() == 0 ? server( socket_fd ) : client( socket_fd ) ;
  120. }
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: Client/Server sockets and reading a file

 
0
  #2
Feb 15th, 2009
First... I wouldn't make the text file in that way. I'd use a delimiter. Such as:
  1. Hello, How are you?:I'm fine.
  2.  
This saves you the hassle of having to guess where the client sentence ends and the server sentence should begin. You already know... it's because of the :. I suppose the same could be said about the ?, but what if it now becomes a statement? Using a standard delimiter makes life that much easier.
As far as the code goes... it looks to me like your second strtok might throw things off result = strtok(NULL, "?.!"); doesn't match up with your first strtok, which is splitting by " ", not ?.!. You said above that there seems to be a compilation error... if so, what is the error?
Reply With Quote Quick reply to this message  
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

Re: Client/Server sockets and reading a file

 
0
  #3
Feb 15th, 2009
the error is the recv_data type is not the same as my vector strings (qns and ans). So I am unable to string compare. And this is kind of a problem for me
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: Client/Server sockets and reading a file

 
0
  #4
Feb 15th, 2009
ah, yes. You could take the char array, and convert it to string, and then compare. While a bit of a pain I suppose:
  1. std::string tmpvar(recv_data);
  2. if (tmpvar.compare(qns) == 0)

I think you could just do a simple == at that point though. if (tmpvar == qns) {
Reply With Quote Quick reply to this message  
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

Re: Client/Server sockets and reading a file

 
0
  #5
Feb 16th, 2009
ok so now my code compiles properly. However, it doesnt work. I am able to input a question like How are you?
but nothing else happens. It should be able to create a fork and the server side of my code should be comparing the input with the file and displaying the answer.
Is there anything wrong with my file reading loop?


  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. std::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. fstream 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. recv_data[bytes_recd] = '\0' ;
  49.  
  50. {
  51. infile.getline(input,255, '\n');
  52.  
  53. while(!infile.eof())
  54. {
  55.  
  56. vector<string> parts;
  57. std::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. std::string tmpvar(recv_data);
  76.  
  77. if (tmpvar == qns)
  78. std::cout << ans << std::endl;
  79. }
  80.  
  81.  
  82. }
  83. /*=====echo recv_data=====
  84.   std::cout << "from " << inet_ntoa(client_addr.sin_addr)
  85.   << ':' << ntohs(client_addr.sin_port) << " - "
  86.   << recv_data << std::endl ;*/
  87. }
  88. return 0 ;
  89. }
  90.  
  91. int client( int socket_fd )
  92. {
  93. std::cout << "Enter Address to connect: " ;
  94. std::string address ; std::cin >> address >> std::ws ;
  95.  
  96. sockaddr_in peer_addr ;
  97. std::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. std::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.  
  116.  
  117.  
  118.  
  119. int socket_fd = socket( AF_INET, SOCK_DGRAM, 0 ) ;
  120. if( socket_fd == -1 ) return 1 ;
  121. return fork() == 0 ? server( socket_fd ) : client( socket_fd ) ;
  122. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,963
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Client/Server sockets and reading a file

 
0
  #6
Feb 16th, 2009
Without looking through all your code, I see this thing:
	else
	recv_data[bytes_recd] = '\0' ;
	
	{
                 infile.getline(input,255, '\n');
		
		while(!infile.eof())

What is that brace doing there? That code block will always be executed, because the {}-block isn't part of any if/else/while/for block.
Last edited by niek_e; Feb 16th, 2009 at 9:07 am.
Reply With Quote Quick reply to this message  
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

Re: Client/Server sockets and reading a file

 
0
  #7
Feb 17th, 2009
hmm i removed those braces and got the same results as I posted before.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,963
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Client/Server sockets and reading a file

 
0
  #8
Feb 17th, 2009
What I meant was: the braces are in the wrong place. The open brace should be directly after 'else'. How much experience do you have with c/c++? Perhaps you might want to read up on a if/else tutorial
Reply With Quote Quick reply to this message  
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

Re: Client/Server sockets and reading a file

 
0
  #9
Feb 17th, 2009
forgot to mention that in my above post, but i tried putting the braces as you advised too. Both removing and changing the position of the braces resulted in the program doing nothing. Im not new to if else....
I am new to this socket and not sure how to integrate a file read/manipulation with the server part. As I said the echoing of input is fine. I believe it's the file read which makes my program not work but I don't know which part of the logic is wrong.
Last edited by number87; Feb 17th, 2009 at 8:36 pm.
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



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

©2003 - 2009 DaniWeb® LLC