Z.K.
2007-05-06 00:23:34 UTC
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
}
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
}