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:
[IMG]http://img92.imageshack.us/img92/6016/proxyexamplerz4.png[/IMG]


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:

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <winsock2.h>

and linked:

WSOCK32.LIB MPR.LIB

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

--------------------Configuration: socktest - Win32 Debug--------------------
Compiling...
socktest.cpp
c:\program files\microsoft visual studio\vc98\include\winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(134) : warning C4005: 'FD_SET' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(83) : see previous definition of 'FD_SET'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(143) : error C2011: 'timeval' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(199) : error C2011: 'hostent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(212) : error C2011: 'netent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(219) : error C2011: 'servent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(226) : error C2011: 'protoent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(310) : error C2011: 'in_addr' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(368) : error C2011: 'sockaddr_in' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(378) : error C2011: 'WSAData' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(430) : warning C4005: 'SO_DONTLINGER' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(391) : see previous definition of 'SO_DONTLINGER'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(483) : warning C4005: 'AF_IPX' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(449) : see previous definition of 'AF_IPX'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(506) : warning C4005: 'AF_MAX' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(468) : see previous definition of 'AF_MAX'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(512) : error C2011: 'sockaddr' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(521) : error C2011: 'sockproto' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(560) : error C2011: 'linger' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(573) : warning C4005: 'SOMAXCONN' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(533) : see previous definition of 'SOMAXCONN'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(598) : warning C4005: 'FD_READ' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(551) : see previous definition of 'FD_READ'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(601) : warning C4005: 'FD_WRITE' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(552) : see previous definition of 'FD_WRITE'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(604) : warning C4005: 'FD_OOB' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(553) : see previous definition of 'FD_OOB'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(607) : warning C4005: 'FD_ACCEPT' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(554) : see previous definition of 'FD_ACCEPT'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(610) : warning C4005: 'FD_CONNECT' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(555) : see previous definition of 'FD_CONNECT'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(613) : warning C4005: 'FD_CLOSE' : macro redefinition
        c:\program files\microsoft visual studio\vc98\include\winsock.h(556) : see previous definition of 'FD_CLOSE'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1427) : error C2375: 'accept' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(707) : see declaration of 'accept'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1448) : error C2375: 'bind' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(710) : see declaration of 'bind'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1467) : error C2375: 'closesocket' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(712) : see declaration of 'closesocket'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1486) : error C2375: 'connect' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(714) : see declaration of 'connect'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1507) : error C2375: 'ioctlsocket' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(716) : see declaration of 'ioctlsocket'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1528) : error C2375: 'getpeername' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(718) : see declaration of 'getpeername'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1549) : error C2375: 'getsockname' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(721) : see declaration of 'getsockname'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1572) : error C2375: 'getsockopt' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(724) : see declaration of 'getsockopt'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1593) : error C2375: 'htonl' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(727) : see declaration of 'htonl'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1610) : error C2375: 'htons' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(729) : see declaration of 'htons'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1627) : error C2375: 'inet_addr' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(731) : see declaration of 'inet_addr'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1644) : error C2375: 'inet_ntoa' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(733) : see declaration of 'inet_ntoa'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1662) : error C2375: 'listen' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(735) : see declaration of 'listen'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1680) : error C2375: 'ntohl' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(737) : see declaration of 'ntohl'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1697) : error C2375: 'ntohs' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(739) : see declaration of 'ntohs'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1717) : error C2375: 'recv' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(741) : see declaration of 'recv'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1742) : error C2375: 'recvfrom' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(743) : see declaration of 'recvfrom'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1768) : error C2375: 'select' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(746) : see declaration of 'select'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1792) : error C2375: 'send' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(749) : see declaration of 'send'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1817) : error C2375: 'sendto' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(751) : see declaration of 'sendto'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1843) : error C2375: 'setsockopt' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(754) : see declaration of 'setsockopt'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1865) : error C2375: 'shutdown' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(757) : see declaration of 'shutdown'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1885) : error C2375: 'socket' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(759) : see declaration of 'socket'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1908) : error C2375: 'gethostbyaddr' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(763) : see declaration of 'gethostbyaddr'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1927) : error C2375: 'gethostbyname' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(766) : see declaration of 'gethostbyname'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1945) : error C2375: 'gethostname' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(768) : see declaration of 'gethostname'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1964) : error C2375: 'getservbyport' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(770) : see declaration of 'getservbyport'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(1983) : error C2375: 'getservbyname' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(772) : see declaration of 'getservbyname'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2001) : error C2375: 'getprotobynumber' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(775) : see declaration of 'getprotobynumber'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2018) : error C2375: 'getprotobyname' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(777) : see declaration of 'getprotobyname'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2038) : error C2375: 'WSAStartup' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(781) : see declaration of 'WSAStartup'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2056) : error C2375: 'WSACleanup' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(783) : see declaration of 'WSACleanup'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2073) : error C2375: 'WSASetLastError' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(785) : see declaration of 'WSASetLastError'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2090) : error C2375: 'WSAGetLastError' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(787) : see declaration of 'WSAGetLastError'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2107) : error C2375: 'WSAIsBlocking' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(789) : see declaration of 'WSAIsBlocking'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2124) : error C2375: 'WSAUnhookBlockingHook' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(791) : see declaration of 'WSAUnhookBlockingHook'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2141) : error C2375: 'WSASetBlockingHook' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(793) : see declaration of 'WSASetBlockingHook'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2158) : error C2375: 'WSACancelBlockingCall' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(795) : see declaration of 'WSACancelBlockingCall'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2180) : error C2375: 'WSAAsyncGetServByName' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(797) : see declaration of 'WSAAsyncGetServByName'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2207) : error C2375: 'WSAAsyncGetServByPort' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(802) : see declaration of 'WSAAsyncGetServByPort'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2233) : error C2375: 'WSAAsyncGetProtoByName' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(806) : see declaration of 'WSAAsyncGetProtoByName'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2258) : error C2375: 'WSAAsyncGetProtoByNumber' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(810) : see declaration of 'WSAAsyncGetProtoByNumber'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2283) : error C2375: 'WSAAsyncGetHostByName' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(814) : see declaration of 'WSAAsyncGetHostByName'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2310) : error C2375: 'WSAAsyncGetHostByAddr' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(818) : see declaration of 'WSAAsyncGetHostByAddr'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2333) : error C2375: 'WSACancelAsyncRequest' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(822) : see declaration of 'WSACancelAsyncRequest'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(2353) : error C2375: 'WSAAsyncSelect' : redefinition; different linkage
        c:\program files\microsoft visual studio\vc98\include\winsock.h(824) : see declaration of 'WSAAsyncSelect'
Error executing cl.exe.

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:

// socktest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <winsock.h>
#include <iostream>
#include <stdio.h>


#define DEST_IP   "127.0.0.1"
#define DEST_PORT 888

using namespace std;



int main(int argc, char* argv[])
{
	//msg will contain the message to be sent to the listen socket
	char *msg = "message";

	//len is the length of the message
	//bytes_sent is the emount of bytes that got sent
	int len, bytes_sent;

	//The winsock library
    WSADATA wsaData;

	//starting up the winsock library, error checking
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        cout << "WSAStartup failed.\n";
        exit(1);
    }
	
	//cleaning the winsock library, error checking
	if (WSACleanup() != 0) {
		cout << "WSACleanup failed \n";
		exit(1);
	}

	//sockfd will contain the return value of socket cheation. if -1 then error
    int sockfd;

	//information about the socket to be created. structure
    struct sockaddr_in dest_addr; 
	
	//creating the socket.
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
	
	//looking for erros while creating the socket
	if(sockfd==-1) {
		cout << "Socket creation failed. \n";
		exit(1);
	}

	//setting the sockaddr_in structure
    dest_addr.sin_family = AF_INET;          // host byte order
    dest_addr.sin_port = htons(DEST_PORT);   // short, network byte order
    dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); //destination IP
    memset(&(dest_addr.sin_zero), '\0', 8);  // zero the rest of the struct


    //error check the connect()
    if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr))==-1) {
		cout << "Socket connection failed.\n";
		exit(1);
	}

	//sending the data to the other socket.
	while(1) {
		Sleep(100);
		len = strlen(msg);
		bytes_sent = send(sockfd, msg, len, 0);
	}

return 0;
}

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

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:

WSANOTINITIALISED
10093	
Successful WSAStartup not yet performed.
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!

//starting up the winsock library, error checking
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        cout << "WSAStartup failed.\n";
        exit(1);
    }

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


Yours,
Eddy

> 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.

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.