Client connection problem Winsock

I am working on a c++ client that will connect to a server I have listening on a port. I can telnet the server, connect to it with a browser, and I already wrote a java client that interacts correctly with it.

However, I cannot for the life of me figure out what I am doing wrong with the c++ client.

Here is my current working code (copied from a tutorial). Note that I have tried 5 other tutorials, gotten all of them to compile, but none will connect. They all fail on the connect call...

#include <winsock2.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#pragma comment(lib, "Ws2_32.lib")

using namespace std;

int gPort = 3390;
void main()
{
string waitForIt;
SOCKET lhSocket;
SOCKADDR_IN lSockAddr;
WSADATA wsaData;
int lConnect;
int lLength;
char lData[]="SendData";
if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
cout<<"Socket Initialization Error. Program aborted\n";
cin >> waitForIt;
return;
}
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
cout<<"Invalid Socket "<<GetLastError()<<". Program Aborted\n"<<endl;
}
memset(&lSockAddr,0, sizeof(lSockAddr));
lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(gPort);
lSockAddr.sin_addr.s_addr = inet_addr("myaddress-removed for privacy");
lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));
if(lConnect != 0)
{
cout<<"Connect Error. Program aborted\n";
cin >> waitForIt;
return;
}
lLength = send(lhSocket,lData,strlen(lData),0);
if(lLength < strlen(lData))
{
cout<<"Send Error.\n";
}
closesocket(lhSocket);

return;
}


I am trying to reason as to what the problem could be, and since 3 other clients can connect I figure it cant be a firewall (I've tried on many computers including those that can connect with other clients) or a port forwarding problem. Can anyone shed some light on this matter?
Thanks,
-Fred
Last edited on
Topic archived. No new replies allowed.