通过Socket将数据发送到SQL Server

时间:2013-02-19 22:17:37

标签: sql sockets

我几天来一直试图通过套接字连接到我的SQL Server。我找不到有什么问题。

我认为我的sql服务器设置正确,我可以从服务器管理器或Visual Studio连接到它,并使用我在Socket中使用的相同连接信息。 它每次都可以使用套接字。套接字可以连接,但是当它向服务器发送一个字符串时,SQL Server Logs会给我这样的信息: “netword数据包有效负载中指定的长度与读取的字节数不匹配;连接已关闭 错误:17836,严重程度20,状态17“。

这是我使用的套接字代码。它从Microsoft套接字服务器/客户端示例中获取它:

#include "stdafx.h"

#include "ClientMFC.h"

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")



using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }
    //WSADATA is a struct that is filled up by the call 
    //to WSAStartup
    WSADATA wsaData;


    //WSAStartup initializes the program for calling WinSock.
    //The first parameter specifies the highest version of the 
    //WinSock specification, the program is allowed to use.
    int wsaret=WSAStartup(0x101,&wsaData);

    //WSAStartup returns zero on success.
    //If it fails we exit.
    if(wsaret!=0)
    {
        return 0;
    }

    SOCKET ConnectSocket = INVALID_SOCKET;

    struct sockaddr_in dest_addr;   // will hold the destination addr  

    dest_addr.sin_family = AF_INET;          // host byte order
    dest_addr.sin_port = htons(2433);   // short, network byte order
    dest_addr.sin_addr.s_addr = inet_addr("10.100.17.114");
    memset(&(dest_addr.sin_zero), '\0', 8);  // zero the rest of the struct
    ConnectSocket = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!


   // Connect to server.
    int iResult = connect( ConnectSocket, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
    }

    const char *   toto   = "<connectionString>Data Source=10.100.17.114,2433;ENCRYPT=false;Initial Catalog=MetricsLocal;Persist Security Info=True;User ID=sa;Password=XXX</connectionString>";

    char *SendBuf = (char*)malloc(strlen(toto) * sizeof(char));
    // convert to network byte ordering. This is important for running on the ps3 and xenon
    for(INT BufIndex = 0; BufIndex < strlen(toto); ++BufIndex)
    {
        SendBuf[BufIndex] = toto[BufIndex];//htons(toto[BufIndex]);
    }


    int BytesSent           = send(ConnectSocket, SendBuf , strlen(SendBuf),0);

    printf("BytesSent = %d, Supposed = %d\n", BytesSent, strlen(toto));


    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }


    return nRetCode;
}

请注意,我隐藏了我的密码。 这段代码应该很容易让你尝试。只需在链接器中添加ws2_32.lib附加依赖项。 我真的很困惑。 我很确定尺寸是正确的。我还编写了一个Socket服务器来检查我收到的内容,看起来是正确的。 思路: - 我需要一个没有描述的标题吗? - 我有特殊的设置去做某个地方吗? 我的telnet连接告诉我我的端口已打开并准备就绪。&lt;

感谢您的帮助

0 个答案:

没有答案