All pastes #975100 Raw Edit

Anonymous

public cpp v1 · immutable
·published 2008-04-07 09:02 UTC
rendered paste body
#include "stdafx.h"#include "TVServerConnection.h"TVServerConnection::TVServerConnection(){}int TVServerConnection::Connect(char *host){  int nret;  init();  // Store information about the server  LPHOSTENT hostEntry;	hostEntry = gethostbyname(host);  cout << "Host entry is... " << hostEntry << endl;  	if (!hostEntry) {		WSACleanup();    sock = NULL;		return NETWORK_ERROR;	}  cout << "About to make a socket\n";  sock = ::socket(AF_INET,			// Go over TCP/IP			   SOCK_STREAM,			// This is a stream-oriented socket			   IPPROTO_TCP);		// Use TCP rather than UDP  cout << "Sock is " << sock << endl;	if (sock == INVALID_SOCKET) {    ::WSACleanup();    sock = NULL;		return NETWORK_ERROR;	}  cout << "Socket successfull.. about to get in stuff..\n";	// Fill a SOCKADDR_IN struct with address information	SOCKADDR_IN serverInfo;	serverInfo.sin_family = AF_INET;	// At this point, we've successfully retrieved vital information about the server,	// including its hostname, aliases, and IP addresses.  Wait; how could a single	// computer have multiple addresses, and exactly what is the following line doing?	// See the explanation below.	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);  printf("Sin _ addr is %x\n", serverInfo.sin_addr.s_host);    serverInfo.sin_port = ::htons(this->PORT);		// Change to network-byte order and							// insert into port field  cout << "Sin _port is " << serverInfo.sin_port << endl;  cout << "About to connect\n";	// Connect to the server  nret = ::connect(sock,		       (LPSOCKADDR)&serverInfo,		       sizeof(struct sockaddr));  cout << "Connect returns " << nret << endl;	if (nret == SOCKET_ERROR) {		nret = WSAGetLastError();   		ReportError(nret, "connect()");    ::WSACleanup();    sock= NULL;		return NETWORK_ERROR;	}	// Successfully connected!  cout << "success connecting\n";  return NETWORK_OK;}void TVServerConnection::Disconnect(){	// Send/receive, then cleanup:  ::closesocket(sock);  ::WSACleanup();}int TVServerConnection::ReadData(char *data, int len){}int TVServerConnection::SendData(char *data, int len){  while(len > 0)  {    int nret = send(sock, data, len, 0);    if (nret == SOCKET_ERROR) {      nret = NETWORK_ERROR;	    // Get a specific code	    // Handle accordingly	    return NETWORK_ERROR;    } else {	    // nret contains the number of bytes sent      len -= nret;      data += nret;    }  }    return NETWORK_OK;}int TVServerConnection::init(){    WORD sockVersion;	WSADATA wsaData;	int nret;	sockVersion = MAKEWORD(2, 2);  	// We begin by initializing Winsock  ::WSAStartup(sockVersion, &wsaData);    return 0;}void TVServerConnection::ReportError(int errorCode, const char *whichFunc) {   char errorMsg[92];					// Declare a buffer to hold							// the generated error message      ZeroMemory(errorMsg, 92);				// Automatically NULL-terminate the string   // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer   sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);   cout << "ERROR IS " << errorMsg << endl;}