TRAKKER Antares 246X Stationary Terminal User's Manual
CLIENT.CPP Sample Application for a TCP/IP Direct Connect Network
#include <windows.h>
#include <stdio.h>
#include "Utils.h"
// our application uses a fixed port number
const unsigned short SERVER_PORT = 6000;
const unsigned short CLIENT_PORT = 6001;
// we will default to the local host machine
// unless argv[1] has a hostname
const char SERVER_HOSTNAME[] = "ncm";
int main(int argc, char *argv[])
{
// turn on the socket library for this process
WSADATA wsad;
int error = WSAStartup(MAKEWORD(1,1), &wsad);
if (error != 0)
ErrorMessage("WSAStartup", WSAGetLastError());
// create an unitialized connection-oriented socket
SOCKET connection;
connection = socket(PF_INET, SOCK_DGRAM, 0);
if (connection == INVALID_SOCKET)
ErrorMessage("socket", WSAGetLastError());
// lookup the IP address of the requested host
HOSTENT *phostent = gethostbyname(argc == 2 ? argv[1] : SERVER_HOSTNAME);
if (phostent == 0)
ErrorMessage("gethostbyname", WSAGetLastError());
// define a SOCKADDR to contain the IP address of the
// server and the port number of our application
SOCKADDR_IN serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = PF_INET;
serverAddress.sin_port
memcpy(&serverAddress.sin_addr, phostent->h_addr_list[0], phostent->h_length);
// Bind a well known port of 6000 to the socket
SOCKADDR_IN clientAddress;
memset(&clientAddress, 0, sizeof(clientAddress));
clientAddress.sin_family = PF_INET;
clientAddress.sin_port = htons(CLIENT_PORT);
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
if(!(bind(connection, (LPSOCKADDR)&clientAddress, sizeof(clientAddress))
==0))
{
ErrorMessage("bind", WSAGetLastError());
}
4-24
nugget
= htons(SERVER_PORT);
39