1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #ifndef __CSOCK_H__
#define __CSOCK_H__
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <assert.h>
#include "ECSock.h"
#ifdef LINUX
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>
#include <iostream>
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
typedef unsigned short WORD;
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct hostent* LPHOSTENT;
typedef struct servent* LPSERVENT;
typedef struct in_addr* LPIN_ADDR;
typedef struct sockaddr* LPSOCKADDR;
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include "CSockCli.h"
#pragma comment(lib, "ws2_32.lib")
#endif
#define TIME_IN_SEC 5 // how long client will wait for server response in non-blocking mode
#define BUFFER_SIZE 153600 //81920 //10240 // SendData and RecvData buffers sizes
class CSock
{
public:
enum CSockProtocol
{
HTTP10 = 0,
HTTP11 = 1
};
CSock();
~CSock();
void AddHeader(const char *header);
const char* GetLocalHostIP() const;
const char* GetLocalHostName() const;
void Send();
void SendLine(const char* line);
void SetRemoteServer(const char* pHost,const unsigned short port=0);
void SetFilename(const char* filename);
const char* GetFilename() const;
void Binary();
void Ascii();
char* GetRcvData();
long GetDataLen();
void ClearHeaderData(void);
void SetProtocolVersion(CSockProtocol protocol);
void SetUrl(const char* url);
void SetTimeout(long timeout);
SOCKET TCPConnect(const char * pHost, const unsigned short nPort_=23);
long TCPReadLine();
void HTTPGet(const char * url);
void HTTPGetToFile(const char * filename, const char* url);
void HTTPGetToStream(const char* filename, const char* url, const char* pHost, const unsigned short port);
int HTTPGetHeaderInfo(std::string key, std::string& value);
void WriteDataToFile();
void WriteRawDataHTMLToFile();
unsigned short GetPort(void);
const char * GetHost() const;
int GetLastStatusCode(void);
private:
std::string m_sLocalHostName;
std::string m_sUrl;
std::string m_sHost;
std::stringstream( std::ios_base::binary | std::ios_base::out);
int m_nHTTPLastStatusCode;
unsigned short m_iRemoteSrvPort;
std::string m_sProtocolVersion;
long m_nRcvBufLen;
long m_nTimeout;
char *SendBuf;
char *RecvBuf;
bool m_bBinary;
bool m_bAscii;
SOCKET hSocket, hSocketSrv;
std::string m_sFileName;
std::string m_sPathName;
std::vector<std::string> HeaderData;
DWORD m_uiID;
HANDLE m_ulHandle;
HWND m_hWnd;
long ReceiveDataAscii();
long ReceiveDataBinary();
long ReceiveDataLine();
long ReceiveDataLine(SOCKET s);
void CleanBuffer(char * buffer);
void SendData();
void SendData(SOCKET& s);
int FileExists(const char *filename);
std::string GetPathFromFilename( const char* filename );
int HtmlXYZdigits();
SOCKET ConnectRemoteServer(const char* server, const unsigned short port=0);
void HTTPFormatHeader(char* header);
void HTTPGet(const char* url, const char* pHost, const unsigned short port=80);
void HTTPGetToFile(const char* filename, const char* url, const char* pHost, const unsigned short port=80);
};
#endif |
Partager