I have been trying for days now to connect to my SQL Server through a socket. I can't find what is wrong.
I think I did a correct setup of my sql server, I am able to connect to it from ever the Server manager or Visual Studio with the same connection info that I am using in my Socket. It works everytime but with the socket. The socket can connect but when it send a string to the server, the SQL Server Logs gives me this: "length specified in netword packet payload did not match number of bytes read; the connection have been closed Error: 17836, Severity 20, State 17".
Here is the Socket code I use. It Took it from Microsoft Socket Server/client example:
#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;
}
Note that I have hidden my password. This code should be really easy for you to try. Just add ws2_32.lib in your linker additional dependancies. I am really really stuck with this. I am pretty sure the size is correct. I also coded a Socket server to check what I receive and it seems correct. Ideas: - Do I need a header not described anywhere? -Do I have a special settings to do somewhere ? My telnet connection tells me my port is open and ready.<
Thank you for your help