Discussion:
TCPIP programming
(too old to reply)
Z.K.
2007-05-06 00:23:34 UTC
Permalink
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got
some code off the Internet and it opens a socket okay, but it will not
connect to the address that I used which is my print server.


Z.K.


code:

// NetworkTest2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);

SOCKET s; //socket handle

// The one and only application object

CWinApp theApp;

using namespace std;

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

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.

if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}


}

return nRetCode;
}

//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;

int error = WSAStartup(0x0202, &wsadata);


//did something happen?
if(error)
{
AfxMessageBox("error starting up winsock");
return false;
}

//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}

//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information

target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket

if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}

//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;

temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;

temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}


return false;

}

//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);

WSACleanup(); //clean up the socket
}
Arkady Frenkel
2007-05-06 05:22:37 UTC
Permalink
When you received connect error print out what error you received with
WSAGetLastError()
Arkady
Post by Z.K.
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got some
code off the Internet and it opens a socket okay, but it will not connect
to the address that I used which is my print server.
Z.K.
// NetworkTest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);
SOCKET s; //socket handle
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.
if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}
}
return nRetCode;
}
//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//did something happen?
if(error)
{ AfxMessageBox("error starting up winsock");
return false;
}
//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}
//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information
target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}
//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;
temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;
temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}
return false;
}
//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);
WSACleanup(); //clean up the socket
}
Z.K.
2007-05-06 10:21:28 UTC
Permalink
Ok, thanks. Apparently it is a timeout error as the error code is 10060
though I am not really sure what that is telling me since I am able to
ping the address just fine and I even tried it with the loopback address
of 127.0.0.1 with the same error result.

Z.K.
Post by Arkady Frenkel
When you received connect error print out what error you received with
WSAGetLastError()
Arkady
Post by Z.K.
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got some
code off the Internet and it opens a socket okay, but it will not connect
to the address that I used which is my print server.
Z.K.
// NetworkTest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);
SOCKET s; //socket handle
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.
if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}
}
return nRetCode;
}
//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//did something happen?
if(error)
{ AfxMessageBox("error starting up winsock");
return false;
}
//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}
//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information
target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}
//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;
temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;
temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}
return false;
}
//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);
WSACleanup(); //clean up the socket
}
Arkady Frenkel
2007-05-07 06:28:57 UTC
Permalink
Check that some server listening with those IP/port you try to connect. Use
"netstat -a" in dos box for that
Arkady
Post by Z.K.
Ok, thanks. Apparently it is a timeout error as the error code is 10060
though I am not really sure what that is telling me since I am able to
ping the address just fine and I even tried it with the loopback address
of 127.0.0.1 with the same error result.
Z.K.
Post by Arkady Frenkel
When you received connect error print out what error you received with
WSAGetLastError()
Arkady
Post by Z.K.
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got
some code off the Internet and it opens a socket okay, but it will not
connect to the address that I used which is my print server.
Z.K.
// NetworkTest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);
SOCKET s; //socket handle
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.
if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}
}
return nRetCode;
}
//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//did something happen?
if(error)
{ AfxMessageBox("error starting up winsock");
return false;
}
//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}
//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information
target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}
//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;
temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;
temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}
return false;
}
//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);
WSACleanup(); //clean up the socket
}
Z.K.
2007-05-07 16:56:59 UTC
Permalink
Ok, I ran the netstat -a command and the result I get back is:

TCP SN8LPT391:sapsp58 192.0.0.192:10240 SYN_SENT

Z.K.
Post by Arkady Frenkel
Check that some server listening with those IP/port you try to connect.
Use "netstat -a" in dos box for that
Arkady
Post by Z.K.
Ok, thanks. Apparently it is a timeout error as the error code is 10060
though I am not really sure what that is telling me since I am able to
ping the address just fine and I even tried it with the loopback address
of 127.0.0.1 with the same error result.
Z.K.
Post by Arkady Frenkel
When you received connect error print out what error you received with
WSAGetLastError()
Arkady
Post by Z.K.
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got
some code off the Internet and it opens a socket okay, but it will not
connect to the address that I used which is my print server.
Z.K.
// NetworkTest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);
SOCKET s; //socket handle
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.
if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}
}
return nRetCode;
}
//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//did something happen?
if(error)
{ AfxMessageBox("error starting up winsock");
return false;
}
//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}
//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information
target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}
//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;
temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;
temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}
return false;
}
//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);
WSACleanup(); //clean up the socket
}
Arkady Frenkel
2007-05-08 07:11:51 UTC
Permalink
You need to check if server wait in the listening state for your client
Arkady
Post by Z.K.
TCP SN8LPT391:sapsp58 192.0.0.192:10240 SYN_SENT
Z.K.
Post by Arkady Frenkel
Check that some server listening with those IP/port you try to connect.
Use "netstat -a" in dos box for that
Arkady
Post by Z.K.
Ok, thanks. Apparently it is a timeout error as the error code is 10060
though I am not really sure what that is telling me since I am able to
ping the address just fine and I even tried it with the loopback address
of 127.0.0.1 with the same error result.
Z.K.
Post by Arkady Frenkel
When you received connect error print out what error you received with
WSAGetLastError()
Arkady
Post by Z.K.
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got
some code off the Internet and it opens a socket okay, but it will not
connect to the address that I used which is my print server.
Z.K.
// NetworkTest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);
SOCKET s; //socket handle
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.
if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}
}
return nRetCode;
}
//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//did something happen?
if(error)
{ AfxMessageBox("error starting up winsock");
return false;
}
//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}
//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information
target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}
//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;
temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;
temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}
return false;
}
//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);
WSACleanup(); //clean up the socket
}
Ben Voigt
2007-05-24 17:41:08 UTC
Permalink
Post by Z.K.
Ok, thanks. Apparently it is a timeout error as the error code is 10060
though I am not really sure what that is telling me since I am able to
ping the address just fine and I even tried it with the loopback address
of 127.0.0.1 with the same error result.
Looks like the port number is wrong. Having a server port number of 60000
would be very unusual, that is in the range used for client ports.
Post by Z.K.
Z.K.
Post by Arkady Frenkel
When you received connect error print out what error you received with
WSAGetLastError()
Arkady
Post by Z.K.
I was wondering if someone might be able to help me with some TCPIP
programming; this is my first attempt and it was not a success. I got
some code off the Internet and it opens a socket okay, but it will not
connect to the address that I used which is my print server.
Z.K.
// NetworkTest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetworkTest2.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
bool ConnectToHost(int PortNo, char* IPAddress);
void CloseConnection(void);
SOCKET s; //socket handle
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), 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.
if(!ConnectToHost(60000,"192.168.0.250"))
{
AfxMessageBox("Error communicating to print server");
CloseConnection();
}
else
{
AfxMessageBox("Connection with print server is a success");
CloseConnection();
}
}
return nRetCode;
}
//ConnectToHost - connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//start up Winsock
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//did something happen?
if(error)
{ AfxMessageBox("error starting up winsock");
return false;
}
//did we get the right winsock version
if(wsadata.wVersion != 0x0202)
{
AfxMessageBox("incorrect winsock version");
WSACleanup();
return false;
}
//Fill out the information needed to initialize a socket...
SOCKADDR_IN target; //socket address information
target.sin_family = AF_INET; //address family Internet
target.sin_port = htons(PortNo); //port to connect to
target.sin_addr.S_un.S_addr = inet_addr(IPAddress); //Target IP address
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
if(s == INVALID_SOCKET)
{
AfxMessageBox("Invalid Socket");
return false; //could not create socket
}
//Try connecting
if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
CString temp;
temp.Format("Error Connecting to %s", IPAddress);
AfxMessageBox(temp);
return false;
}
else
{
CString temp;
temp.Format("Connecting to %s", IPAddress);
AfxMessageBox(temp);
return true;
}
return false;
}
//CloseConnection -- shuts down the socket and closes any connection to it
void CloseConnection(void)
{
//Close the socket if it exists
if(s)
closesocket(s);
WSACleanup(); //clean up the socket
}
Loading...