944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 14982
  • C++ RSS
Aug 12th, 2006
0

WinSock problems.

Expand Post »
Hello everyone,

I am trying to create a "proxy" for a program. I will try to explain how I am going to do that.

The program I want to write a proxy for sends and receives data to/from a server. The IP and port the client connects to is somewhere in the memory, and I know how to change that.

I will write the memory of the client to connect to port 1000 (just a random number...) on localhost.

like this:
http://img92.imageshack.us/img92/601...examplerz4.png


Well, I hope it is clear now what I want to do. The problem is that I can't get through a very basic stadium: creating the sockets.

I've tried about every tutorial, snippet, I read many guides about sockets, the winsock functions, but I keep getting errors.

The worst error is if I try
#include <winsock2.h>

I have included:
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <winsock2.h>

and linked:
C++ Syntax (Toggle Plain Text)
  1. WSOCK32.LIB MPR.LIB

The including of winsock2.h adds 58 errors and 11 warnings to the debug window.

C++ Syntax (Toggle Plain Text)
  1. --------------------Configuration: socktest - Win32 Debug--------------------
  2. Compiling...
  3. socktest.cpp
  4. c:\program files\microsoft visual studio\vc98\include\winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition
  5. c:\program files\microsoft visual studio\vc98\include\winsock2.h(134) : warning C4005: 'FD_SET' : macro redefinition
  6. c:\program files\microsoft visual studio\vc98\include\winsock.h(83) : see previous definition of 'FD_SET'
  7. c:\program files\microsoft visual studio\vc98\include\winsock2.h(143) : error C2011: 'timeval' : 'struct' type redefinition
  8. c:\program files\microsoft visual studio\vc98\include\winsock2.h(199) : error C2011: 'hostent' : 'struct' type redefinition
  9. c:\program files\microsoft visual studio\vc98\include\winsock2.h(212) : error C2011: 'netent' : 'struct' type redefinition
  10. c:\program files\microsoft visual studio\vc98\include\winsock2.h(219) : error C2011: 'servent' : 'struct' type redefinition
  11. c:\program files\microsoft visual studio\vc98\include\winsock2.h(226) : error C2011: 'protoent' : 'struct' type redefinition
  12. c:\program files\microsoft visual studio\vc98\include\winsock2.h(310) : error C2011: 'in_addr' : 'struct' type redefinition
  13. c:\program files\microsoft visual studio\vc98\include\winsock2.h(368) : error C2011: 'sockaddr_in' : 'struct' type redefinition
  14. c:\program files\microsoft visual studio\vc98\include\winsock2.h(378) : error C2011: 'WSAData' : 'struct' type redefinition
  15. c:\program files\microsoft visual studio\vc98\include\winsock2.h(430) : warning C4005: 'SO_DONTLINGER' : macro redefinition
  16. c:\program files\microsoft visual studio\vc98\include\winsock.h(391) : see previous definition of 'SO_DONTLINGER'
  17. c:\program files\microsoft visual studio\vc98\include\winsock2.h(483) : warning C4005: 'AF_IPX' : macro redefinition
  18. c:\program files\microsoft visual studio\vc98\include\winsock.h(449) : see previous definition of 'AF_IPX'
  19. c:\program files\microsoft visual studio\vc98\include\winsock2.h(506) : warning C4005: 'AF_MAX' : macro redefinition
  20. c:\program files\microsoft visual studio\vc98\include\winsock.h(468) : see previous definition of 'AF_MAX'
  21. c:\program files\microsoft visual studio\vc98\include\winsock2.h(512) : error C2011: 'sockaddr' : 'struct' type redefinition
  22. c:\program files\microsoft visual studio\vc98\include\winsock2.h(521) : error C2011: 'sockproto' : 'struct' type redefinition
  23. c:\program files\microsoft visual studio\vc98\include\winsock2.h(560) : error C2011: 'linger' : 'struct' type redefinition
  24. c:\program files\microsoft visual studio\vc98\include\winsock2.h(573) : warning C4005: 'SOMAXCONN' : macro redefinition
  25. c:\program files\microsoft visual studio\vc98\include\winsock.h(533) : see previous definition of 'SOMAXCONN'
  26. c:\program files\microsoft visual studio\vc98\include\winsock2.h(598) : warning C4005: 'FD_READ' : macro redefinition
  27. c:\program files\microsoft visual studio\vc98\include\winsock.h(551) : see previous definition of 'FD_READ'
  28. c:\program files\microsoft visual studio\vc98\include\winsock2.h(601) : warning C4005: 'FD_WRITE' : macro redefinition
  29. c:\program files\microsoft visual studio\vc98\include\winsock.h(552) : see previous definition of 'FD_WRITE'
  30. c:\program files\microsoft visual studio\vc98\include\winsock2.h(604) : warning C4005: 'FD_OOB' : macro redefinition
  31. c:\program files\microsoft visual studio\vc98\include\winsock.h(553) : see previous definition of 'FD_OOB'
  32. c:\program files\microsoft visual studio\vc98\include\winsock2.h(607) : warning C4005: 'FD_ACCEPT' : macro redefinition
  33. c:\program files\microsoft visual studio\vc98\include\winsock.h(554) : see previous definition of 'FD_ACCEPT'
  34. c:\program files\microsoft visual studio\vc98\include\winsock2.h(610) : warning C4005: 'FD_CONNECT' : macro redefinition
  35. c:\program files\microsoft visual studio\vc98\include\winsock.h(555) : see previous definition of 'FD_CONNECT'
  36. c:\program files\microsoft visual studio\vc98\include\winsock2.h(613) : warning C4005: 'FD_CLOSE' : macro redefinition
  37. c:\program files\microsoft visual studio\vc98\include\winsock.h(556) : see previous definition of 'FD_CLOSE'
  38. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1427) : error C2375: 'accept' : redefinition; different linkage
  39. c:\program files\microsoft visual studio\vc98\include\winsock.h(707) : see declaration of 'accept'
  40. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1448) : error C2375: 'bind' : redefinition; different linkage
  41. c:\program files\microsoft visual studio\vc98\include\winsock.h(710) : see declaration of 'bind'
  42. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1467) : error C2375: 'closesocket' : redefinition; different linkage
  43. c:\program files\microsoft visual studio\vc98\include\winsock.h(712) : see declaration of 'closesocket'
  44. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1486) : error C2375: 'connect' : redefinition; different linkage
  45. c:\program files\microsoft visual studio\vc98\include\winsock.h(714) : see declaration of 'connect'
  46. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1507) : error C2375: 'ioctlsocket' : redefinition; different linkage
  47. c:\program files\microsoft visual studio\vc98\include\winsock.h(716) : see declaration of 'ioctlsocket'
  48. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1528) : error C2375: 'getpeername' : redefinition; different linkage
  49. c:\program files\microsoft visual studio\vc98\include\winsock.h(718) : see declaration of 'getpeername'
  50. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1549) : error C2375: 'getsockname' : redefinition; different linkage
  51. c:\program files\microsoft visual studio\vc98\include\winsock.h(721) : see declaration of 'getsockname'
  52. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1572) : error C2375: 'getsockopt' : redefinition; different linkage
  53. c:\program files\microsoft visual studio\vc98\include\winsock.h(724) : see declaration of 'getsockopt'
  54. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1593) : error C2375: 'htonl' : redefinition; different linkage
  55. c:\program files\microsoft visual studio\vc98\include\winsock.h(727) : see declaration of 'htonl'
  56. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1610) : error C2375: 'htons' : redefinition; different linkage
  57. c:\program files\microsoft visual studio\vc98\include\winsock.h(729) : see declaration of 'htons'
  58. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1627) : error C2375: 'inet_addr' : redefinition; different linkage
  59. c:\program files\microsoft visual studio\vc98\include\winsock.h(731) : see declaration of 'inet_addr'
  60. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1644) : error C2375: 'inet_ntoa' : redefinition; different linkage
  61. c:\program files\microsoft visual studio\vc98\include\winsock.h(733) : see declaration of 'inet_ntoa'
  62. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1662) : error C2375: 'listen' : redefinition; different linkage
  63. c:\program files\microsoft visual studio\vc98\include\winsock.h(735) : see declaration of 'listen'
  64. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1680) : error C2375: 'ntohl' : redefinition; different linkage
  65. c:\program files\microsoft visual studio\vc98\include\winsock.h(737) : see declaration of 'ntohl'
  66. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1697) : error C2375: 'ntohs' : redefinition; different linkage
  67. c:\program files\microsoft visual studio\vc98\include\winsock.h(739) : see declaration of 'ntohs'
  68. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1717) : error C2375: 'recv' : redefinition; different linkage
  69. c:\program files\microsoft visual studio\vc98\include\winsock.h(741) : see declaration of 'recv'
  70. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1742) : error C2375: 'recvfrom' : redefinition; different linkage
  71. c:\program files\microsoft visual studio\vc98\include\winsock.h(743) : see declaration of 'recvfrom'
  72. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1768) : error C2375: 'select' : redefinition; different linkage
  73. c:\program files\microsoft visual studio\vc98\include\winsock.h(746) : see declaration of 'select'
  74. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1792) : error C2375: 'send' : redefinition; different linkage
  75. c:\program files\microsoft visual studio\vc98\include\winsock.h(749) : see declaration of 'send'
  76. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1817) : error C2375: 'sendto' : redefinition; different linkage
  77. c:\program files\microsoft visual studio\vc98\include\winsock.h(751) : see declaration of 'sendto'
  78. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1843) : error C2375: 'setsockopt' : redefinition; different linkage
  79. c:\program files\microsoft visual studio\vc98\include\winsock.h(754) : see declaration of 'setsockopt'
  80. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1865) : error C2375: 'shutdown' : redefinition; different linkage
  81. c:\program files\microsoft visual studio\vc98\include\winsock.h(757) : see declaration of 'shutdown'
  82. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1885) : error C2375: 'socket' : redefinition; different linkage
  83. c:\program files\microsoft visual studio\vc98\include\winsock.h(759) : see declaration of 'socket'
  84. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1908) : error C2375: 'gethostbyaddr' : redefinition; different linkage
  85. c:\program files\microsoft visual studio\vc98\include\winsock.h(763) : see declaration of 'gethostbyaddr'
  86. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1927) : error C2375: 'gethostbyname' : redefinition; different linkage
  87. c:\program files\microsoft visual studio\vc98\include\winsock.h(766) : see declaration of 'gethostbyname'
  88. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1945) : error C2375: 'gethostname' : redefinition; different linkage
  89. c:\program files\microsoft visual studio\vc98\include\winsock.h(768) : see declaration of 'gethostname'
  90. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1964) : error C2375: 'getservbyport' : redefinition; different linkage
  91. c:\program files\microsoft visual studio\vc98\include\winsock.h(770) : see declaration of 'getservbyport'
  92. c:\program files\microsoft visual studio\vc98\include\winsock2.h(1983) : error C2375: 'getservbyname' : redefinition; different linkage
  93. c:\program files\microsoft visual studio\vc98\include\winsock.h(772) : see declaration of 'getservbyname'
  94. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2001) : error C2375: 'getprotobynumber' : redefinition; different linkage
  95. c:\program files\microsoft visual studio\vc98\include\winsock.h(775) : see declaration of 'getprotobynumber'
  96. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2018) : error C2375: 'getprotobyname' : redefinition; different linkage
  97. c:\program files\microsoft visual studio\vc98\include\winsock.h(777) : see declaration of 'getprotobyname'
  98. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2038) : error C2375: 'WSAStartup' : redefinition; different linkage
  99. c:\program files\microsoft visual studio\vc98\include\winsock.h(781) : see declaration of 'WSAStartup'
  100. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2056) : error C2375: 'WSACleanup' : redefinition; different linkage
  101. c:\program files\microsoft visual studio\vc98\include\winsock.h(783) : see declaration of 'WSACleanup'
  102. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2073) : error C2375: 'WSASetLastError' : redefinition; different linkage
  103. c:\program files\microsoft visual studio\vc98\include\winsock.h(785) : see declaration of 'WSASetLastError'
  104. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2090) : error C2375: 'WSAGetLastError' : redefinition; different linkage
  105. c:\program files\microsoft visual studio\vc98\include\winsock.h(787) : see declaration of 'WSAGetLastError'
  106. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2107) : error C2375: 'WSAIsBlocking' : redefinition; different linkage
  107. c:\program files\microsoft visual studio\vc98\include\winsock.h(789) : see declaration of 'WSAIsBlocking'
  108. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2124) : error C2375: 'WSAUnhookBlockingHook' : redefinition; different linkage
  109. c:\program files\microsoft visual studio\vc98\include\winsock.h(791) : see declaration of 'WSAUnhookBlockingHook'
  110. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2141) : error C2375: 'WSASetBlockingHook' : redefinition; different linkage
  111. c:\program files\microsoft visual studio\vc98\include\winsock.h(793) : see declaration of 'WSASetBlockingHook'
  112. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2158) : error C2375: 'WSACancelBlockingCall' : redefinition; different linkage
  113. c:\program files\microsoft visual studio\vc98\include\winsock.h(795) : see declaration of 'WSACancelBlockingCall'
  114. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2180) : error C2375: 'WSAAsyncGetServByName' : redefinition; different linkage
  115. c:\program files\microsoft visual studio\vc98\include\winsock.h(797) : see declaration of 'WSAAsyncGetServByName'
  116. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2207) : error C2375: 'WSAAsyncGetServByPort' : redefinition; different linkage
  117. c:\program files\microsoft visual studio\vc98\include\winsock.h(802) : see declaration of 'WSAAsyncGetServByPort'
  118. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2233) : error C2375: 'WSAAsyncGetProtoByName' : redefinition; different linkage
  119. c:\program files\microsoft visual studio\vc98\include\winsock.h(806) : see declaration of 'WSAAsyncGetProtoByName'
  120. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2258) : error C2375: 'WSAAsyncGetProtoByNumber' : redefinition; different linkage
  121. c:\program files\microsoft visual studio\vc98\include\winsock.h(810) : see declaration of 'WSAAsyncGetProtoByNumber'
  122. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2283) : error C2375: 'WSAAsyncGetHostByName' : redefinition; different linkage
  123. c:\program files\microsoft visual studio\vc98\include\winsock.h(814) : see declaration of 'WSAAsyncGetHostByName'
  124. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2310) : error C2375: 'WSAAsyncGetHostByAddr' : redefinition; different linkage
  125. c:\program files\microsoft visual studio\vc98\include\winsock.h(818) : see declaration of 'WSAAsyncGetHostByAddr'
  126. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2333) : error C2375: 'WSACancelAsyncRequest' : redefinition; different linkage
  127. c:\program files\microsoft visual studio\vc98\include\winsock.h(822) : see declaration of 'WSACancelAsyncRequest'
  128. c:\program files\microsoft visual studio\vc98\include\winsock2.h(2353) : error C2375: 'WSAAsyncSelect' : redefinition; different linkage
  129. c:\program files\microsoft visual studio\vc98\include\winsock.h(824) : see declaration of 'WSAAsyncSelect'
  130. Error executing cl.exe.
  131.  
  132. socktest.exe - 58 error(s), 11 warning(s)

Well, because of these errors I decided not to include winsock2.h but just winsock.h.

this code was my result:
C++ Syntax (Toggle Plain Text)
  1. // socktest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <winsock.h>
  7. #include <iostream>
  8. #include <stdio.h>
  9.  
  10.  
  11. #define DEST_IP "127.0.0.1"
  12. #define DEST_PORT 888
  13.  
  14. using namespace std;
  15.  
  16.  
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20. //msg will contain the message to be sent to the listen socket
  21. char *msg = "message";
  22.  
  23. //len is the length of the message
  24. //bytes_sent is the emount of bytes that got sent
  25. int len, bytes_sent;
  26.  
  27. //The winsock library
  28. WSADATA wsaData;
  29.  
  30. //starting up the winsock library, error checking
  31. if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
  32. cout << "WSAStartup failed.\n";
  33. exit(1);
  34. }
  35.  
  36. //cleaning the winsock library, error checking
  37. if (WSACleanup() != 0) {
  38. cout << "WSACleanup failed \n";
  39. exit(1);
  40. }
  41.  
  42. //sockfd will contain the return value of socket cheation. if -1 then error
  43. int sockfd;
  44.  
  45. //information about the socket to be created. structure
  46. struct sockaddr_in dest_addr;
  47.  
  48. //creating the socket.
  49. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  50.  
  51. //looking for erros while creating the socket
  52. if(sockfd==-1) {
  53. cout << "Socket creation failed. \n";
  54. exit(1);
  55. }
  56.  
  57. //setting the sockaddr_in structure
  58. dest_addr.sin_family = AF_INET; // host byte order
  59. dest_addr.sin_port = htons(DEST_PORT); // short, network byte order
  60. dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); //destination IP
  61. memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct
  62.  
  63.  
  64. //error check the connect()
  65. if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr))==-1) {
  66. cout << "Socket connection failed.\n";
  67. exit(1);
  68. }
  69.  
  70. //sending the data to the other socket.
  71. while(1) {
  72. Sleep(100);
  73. len = strlen(msg);
  74. bytes_sent = send(sockfd, msg, len, 0);
  75. }
  76.  
  77. return 0;
  78. }

Cool. This one actually compiled without errors.
Unfortunately it just outputs socket creation failed and then quits.

I am using the compiler Microsoft Visual Studio 6.0, on a Windows XP prof computer.


Yours sincerely,
Eddy
Similar Threads
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006
Aug 12th, 2006
0

Re: WinSock problems.

This was supposed to be an edit, but I did not find an edit button, my apologies.

I called WSAGetLastError and it returned 10093.

According to MSDN error 10093 means:

C++ Syntax (Toggle Plain Text)
  1. WSANOTINITIALISED
  2. 10093
  3. Successful WSAStartup not yet performed.
  4. Either the application has not called WSAStartup or WSAStartup failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or WSACleanup has been called too many times.

As a matter of fact I have performed WSAStartup, and it does not return an error!

C++ Syntax (Toggle Plain Text)
  1. //starting up the winsock library, error checking
  2. if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
  3. cout << "WSAStartup failed.\n";
  4. exit(1);
  5. }

It never outputs that the startup has failed, so I don't believe that's the problem...


Yours,
Eddy
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006
Aug 12th, 2006
0

Re: WinSock problems.

> It never outputs that the startup has failed, so I don't believe that's the problem...
I bet the following call to cleanup() before you get to do any real work has something to do with it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 12th, 2006
0

Re: WinSock problems.

Sorry, I didn't quite catch that.

Did you take a look at my code?
It starts up the WSA-thing, then it cleans, then it starts the socket, which gives an error.

Greetz, Eddy
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006
Aug 12th, 2006
0

Re: WinSock problems.

Because WSAStartup() and WSACleanup() are like { and }

They SURROUND the body of code which does network API calls.

Calling cleanup takes you back to the state you were in before you called startup, which is why you get the error that startup hasn't been called.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 12th, 2006
0

Re: WinSock problems.

That worked, thanks a lot!

Don't know why I didn't figure this out with plain logic... Why would you want to clear something before you even use it?


Thanks a lot,
Eddy
Reputation Points: 38
Solved Threads: 3
Junior Poster in Training
Eddy Dean is offline Offline
56 posts
since Jul 2006

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: linking problems
Next Thread in C++ Forum Timeline: Header files





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


Follow us on Twitter


© 2011 DaniWeb® LLC