0

我在 c++ 中的套接字编程中使用这个(http://www.codeproject.com/KB/IP/Socks.aspx)库,并将 socks.h 复制到包含文件夹中并编写以下代码:

#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);
        }

}

但是当编译这个我得到一个错误。请帮助我,谢谢

4

2 回答 2

1

要使用套接字,您需要将可执行文件与Ws2_32.lib. 它将修复您在评论中提到的链接错误。

于 2010-05-13T19:38:44.300 回答
1

根据closesocket的 MSDN 文档,您需要链接到 Ws2_32.lib。

于 2010-05-13T19:41:10.393 回答