开发者

a problem in socks.h

i use this (http://www.codeproject.com/KB/IP/Socks.aspx) lib in my socket programing in c++ and copy the socks.h in include folder and write this code:

#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include "socks.h"

#define PORT 1001                       // the port client will be connecting to 
#define MAXDATASIZE 100

static void ReadThread(void* lp);
int socketId;

int main(int argc, char* argv[])
{


        const char temp[]="GET / HTTP/1.0\r\n\r\n";

        CSocks cs;
        cs.SetVersion(SOCKS_VER4);
        cs.SetSocksPort(1080);
        cs.SetDestinationPort(1001);
        cs.SetDestinationAddress("192.168.11.97");

        cs.SetSocksAddress("192.168.11.97");
        //cs.SetVersion(SOCKS_VER5);
        //cs.SetSocksAddress("128.0.21.200");



        socketId = cs.Connect();

        // if failed
        if (cs.m_IsError)
        {
            printf( "\n%s", cs.GetLastErrorMessage());
            getch();
            return 0;
        }

        // send packet for requesting to a server
        if(socketId > 0)
        {
            send(socketId, temp, strlen(temp), 0);

            HANDLE ReadThreadID;                                            // handle for read thread id
            HANDLE handle;                                                  // handle for thread handle

            handle = CreateThread ((LPSECURITY_ATTRIBUTES)NULL,             // No security attributes.
                        开发者_如何学编程            (DWORD)0,                               // Use same stack size.
                                    (LPTHREAD_START_ROUTINE)ReadThread,     // Thread procedure.
                                    (LPVOID)(void*)NULL,                    // Parameter to pass.
                                    (DWORD)0,                               // Run immediately.
                                    (LPDWORD)&ReadThreadID);        

            WaitForSingleObject(handle, INFINITE);
        }
        else
        {
            printf("\nSocks Server / Destination Server not started..");
        }
        closesocket(socketId);
        getch();

        return 0;
}


// Thread Proc for reading from server socket.
static void ReadThread(void* lp)
{

        int numbytes; 
        char buf[MAXDATASIZE];

        while(1)
        {

            if ((numbytes=recv(socketId, buf, MAXDATASIZE-1, 0)) == -1)
            {
                    printf("\nServer / Socks Server has been closed Receive thread Closed\0");
                    break;
            }
            if (numbytes == 0) break;
                buf[numbytes] = '\0';
                printf("Received: %s\r\n",buf);
                send(socketId,buf,strlen(buf),0);
        }

}

but when compile this i get an error . pls help me thanks


To use sockets you need to link your executable with Ws2_32.lib. It will fix the link error that you have mentioned in comments.


According to the MSDN documentation for closesocket, you need to link to Ws2_32.lib.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜