Shouldn't this code work? (select())

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

Join Date: May 2009
Posts: 30
Reputation: azjherben is an unknown quantity at this point 
Solved Threads: 0
azjherben azjherben is offline Offline
Light Poster

Shouldn't this code work? (select())

 
0
  #1
Jul 18th, 2009
Shouldn't this code work? (select())

Well, this is basically a proxy.
It takes things from a program that is running on port 15777 and the puts it out to host on port 15776, but sence I added the
select() witch is needed. It hasn't been working right. Any help?

  1. #include <windows.h> //Required for socket init
  2. #include <iostream>
  3. #include <sys/time.h>
  4. using namespace std;
  5. int main(){
  6.  
  7.  
  8.  
  9.  
  10.  
  11. char buf[256];
  12. char uua[256];
  13. WSAData wsdata; //Declare WSAData
  14. WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
  15. WSAStartup(wsver, &wsdata);
  16. //WSA starting code
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //Hosting thing.
  23.  
  24.  
  25. SOCKET Szocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  26. SOCKADDR_IN serverInf;
  27. serverInf.sin_family=AF_INET;
  28. serverInf.sin_addr.s_addr=INADDR_ANY;
  29. serverInf.sin_port=htons(15776);
  30. bind(Szocket,(SOCKADDR*)(&serverInf),sizeof(serverInf));
  31. listen(Szocket,1);
  32. SOCKET TempSock=SOCKET_ERROR;
  33. while(TempSock==SOCKET_ERROR)
  34. {
  35. std::cout<<"Waiting for incoming connections...\r\n";
  36. TempSock=accept(Szocket,NULL,NULL);
  37. }
  38. u_long iMode=1;
  39. ioctlsocket(Szocket,FIONBIO,&iMode);
  40. Szocket=TempSock;
  41. std::cout<<"Client connected!\r\n\r\n";
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
  52. sockaddr_in sin;
  53. sin.sin_port=htons(15777); //Connect to port 15777
  54. sin.sin_addr.s_addr=inet_addr("127.0.0.1"); //Connect to localhost
  55. sin.sin_family=AF_INET;
  56. ioctlsocket(Szocket,FIONBIO,&iMode);
  57. if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ //Check the condition
  58. std::cout<<"Connect failed, error: "<<WSAGetLastError(); //Returns error code
  59. WSACleanup(); //Cleanup the library
  60. std::string lisquit;
  61. std::cin>>lisquit;
  62. }
  63. std::cout<<"Connection successful!\n";
  64.  
  65.  
  66. timeval tiz;
  67. tiz.tv_sec = 1;
  68. tiz.tv_usec = 0;
  69.  
  70. FD_SET OrigMC;
  71. FD_SET PrxMC;
  72.  
  73.  
  74.  
  75.  
  76. //while (true){
  77.  
  78. FD_ZERO(&OrigMC);
  79. FD_ZERO(&PrxMC);
  80. FD_SET(Szocket,&PrxMC);
  81. FD_SET(kSock,&OrigMC);
  82.  
  83.  
  84. cout<<"Loop started.\n";
  85.  
  86.  
  87.  
  88. int selecta = select(0,&PrxMC,NULL,NULL,&tiz);
  89. cout<<"Has ready to recive: "<<selecta<<"\n";
  90.  
  91. //if(selecta == 1){
  92. recv(Szocket,uua,sizeof(uua),0); //Recive from my proxy server
  93. send(kSock,uua,sizeof(uua),0); //Send to the real one
  94. cout<<"In: "<<uua<<"\n\n"; //Echo it
  95. //}
  96.  
  97. int selectb = select(0,&OrigMC,NULL,NULL,&tiz);
  98. cout<<"Has ready to recive: "<<selectb<<"\n";
  99.  
  100. if(selectb == 1){
  101. recv(kSock, buf, sizeof(buf), 0); //Recive from the real server
  102. send(Szocket,buf,sizeof(buf),0); //Send to the proxy server
  103. cout<<"Out: "<<buf<<"\n\n"; //Echo what it just recived and sent
  104. }
  105.  
  106. //No more sleep?
  107. Sleep(50);
  108. // Azjherben.org
  109.  
  110.  
  111. //}
  112.  
  113.  
  114.  
  115.  
  116.  
  117. }
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Shouldn't this code work? (select())

 
0
  #2
Jul 18th, 2009
Perhaps work on the code formatting first.

http://cboard.cprogramming.com/netwo...rk-select.html
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 48
Reputation: thelamb is on a distinguished road 
Solved Threads: 7
thelamb thelamb is offline Offline
Light Poster

Re: Shouldn't this code work? (select())

 
0
  #3
Jul 18th, 2009
And give us some more information on what exactly isn't working, are there errors or?

Secondly, the way you use select() will only work with one socket, what if you want to have more clients connecting to your proxy? Select() returns the number of sockets that are ready, so if you check if ( selectb >= 1 ) it will resolve to true but you'll have no idea which socket is ready.

For this you need to check if your socket is still in the FD_SET after the select() call.

So basically:
  1. int selectb = select(0,&OrigMC,NULL,NULL,&tiz);
  2. if ( selectb >= 1 )
  3. {<blockquote>if( FD_ISSET( kSock, &OrigMC )
  4. {<blockquote>recv(kSock, buf, sizeof(buf), 0);
  5. send(Szocket,buf,sizeof(buf),0);
  6. </blockquote>}
  7. </blockquote>}

Of course it still only checks kSock, but it's up to your imagination how to make it work for more sockets.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 30
Reputation: azjherben is an unknown quantity at this point 
Solved Threads: 0
azjherben azjherben is offline Offline
Light Poster

Re: Shouldn't this code work? (select())

 
0
  #4
Jul 18th, 2009
This code works good on the first recive and send and recive and send but then starts echoing out 1's and -1's really fast I guess it also disconnects from the client. If I try to make it cout what it is sending/reciving it makes alot of beeping sounds.


  1. #include <windows.h> //Required for socket init
  2. #include <iostream>
  3. #include <sys/time.h>
  4. using namespace std;
  5. int main(){
  6.  
  7.  
  8.  
  9.  
  10.  
  11. char buf[256];
  12. char uua[256];
  13. WSAData wsdata; //Declare WSAData
  14. WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
  15. WSAStartup(wsver, &wsdata);
  16. //WSA starting code
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //Hosting thing.
  23.  
  24.  
  25. SOCKET Szocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  26. SOCKADDR_IN serverInf;
  27. serverInf.sin_family=AF_INET;
  28. serverInf.sin_addr.s_addr=INADDR_ANY;
  29. serverInf.sin_port=htons(15776);
  30. bind(Szocket,(SOCKADDR*)(&serverInf),sizeof(serverInf));
  31. listen(Szocket,1);
  32. SOCKET TempSock=SOCKET_ERROR;
  33. while(TempSock==SOCKET_ERROR)
  34. {
  35. std::cout<<"Waiting for incoming connections...\r\n";
  36. TempSock=accept(Szocket,NULL,NULL);
  37. }
  38. u_long iMode=1;
  39. //ioctlsocket(Szocket,FIONBIO,&iMode);
  40. Szocket=TempSock;
  41. std::cout<<"Client connected!\r\n\r\n";
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
  52. sockaddr_in sin;
  53. sin.sin_port=htons(15777); //Connect to port 15777
  54. sin.sin_addr.s_addr=inet_addr("127.0.0.1"); //Connect to localhost
  55. sin.sin_family=AF_INET;
  56. //ioctlsocket(Szocket,FIONBIO,&iMode);
  57. if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ //Check the condition
  58. std::cout<<"Connect failed, error: "<<WSAGetLastError(); //Returns error code
  59. WSACleanup(); //Cleanup the library
  60. std::string lisquit;
  61. std::cin>>lisquit;
  62. }
  63. std::cout<<"Connection successful!\n";
  64.  
  65.  
  66. timeval tiz;
  67. tiz.tv_sec = 1;
  68. tiz.tv_usec = 0;
  69.  
  70. FD_SET OrigMC;
  71. FD_SET PrxMC;
  72.  
  73.  
  74.  
  75.  
  76. //while (true){
  77.  
  78. FD_ZERO(&OrigMC);
  79. FD_ZERO(&PrxMC);
  80. FD_SET(Szocket,&PrxMC);
  81. FD_SET(kSock,&OrigMC);
  82.  
  83.  
  84. cout<<"Loop started.\n";
  85.  
  86. while (true)
  87. {
  88. int selecta = select(0,&PrxMC,NULL,NULL,&tiz);
  89. cout<<"Has ready to recive: "<<selecta<<"\n";
  90.  
  91. if ( selecta >= 1 )
  92. {
  93. if( FD_ISSET( Szocket, &PrxMC ))
  94. {
  95. recv(Szocket, buf, sizeof(buf), 0);
  96. send(kSock,buf,sizeof(buf),0);
  97. }
  98. }
  99.  
  100.  
  101.  
  102.  
  103. int selectb = select(0,&OrigMC,NULL,NULL,&tiz);
  104. cout<<"Has ready to recive: "<<selectb<<"\n";
  105.  
  106. if ( selectb >= 1 )
  107. {
  108. if( FD_ISSET( kSock, &OrigMC ))
  109. {
  110. recv(kSock, buf, sizeof(buf), 0);
  111. send(Szocket,buf,sizeof(buf),0);
  112. }
  113. }
  114.  
  115. //No more sleep?
  116. Sleep(50);
  117. // Azjherben.org
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126. }
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