You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libUDPTCPNetwork/UDPTCPNetwork.h

106 lines
2.3 KiB

#ifndef _UDPTCPNETWORK_H_
#define _UDPTCPNETWORK_H_
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string>
using namespace std;
#define NET_HOSTLEN 256
#define NET_PORTLEN 6
#define NET_BUFFERSIZE 1024
#define NET_MAX_RETRY 5 // retry to send data EINTR
#define NET_MAX_TIMEOUT 30000 // timeout in ms
/************************************************************************
*
* global functions needed for networking
*
*/
int dns_filladdr (string host, string port, int ai_family,
struct sockaddr_storage *sAddr);
char *itoa(char* buffer, int number, int size);
/************************************************************************
*
* udp related functions
*
*/
class UDP {
private:
int sock;
int localport;
size_t readcnt;
size_t writecnt;
public:
UDP();
~UDP();
int Listen(int port);
long int ReadTimeout(string *srcaddr, char *buffer, long int len, int timeout_ms);
long int Read(string *srcaddr, char *buffer, long int len);
long int Write(string destaddr, char *buffer, long int len);
void Close();
int IsListen();
int IsData(int timeout_ms); // timeout in ms;
int GetSocket() { return sock; };
};
/************************************************************************
* tcp related functions
*/
class TCP {
private:
int sock;
// struct sockaddr_storage localaddr;
// struct sockaddr_storage remoteaddr;
string remote_host;
string remote_port;
int readcnt;
int writecnt;
public:
TCP();
TCP(int s);
TCP(string h, string p);
TCP(string hostport, int defaultport);
~TCP();
int Connect();
int Connect(string h, string p);
int Connect(string host, int defaultport);
long int ReadPop (char *buffer, long int pktlen, long int bufferlen);
long int ReadTimeout(char *buffer, long int len, int timeout);
long int Read(char *buffer, long int len);
long int Write(char *buffer, long int len);
void Close();
int IsConnected();
int IsData(int timeout); // timeout in ms;
int Listen(int port);
TCP* Accept();
int GetSocket() { return sock; };
void SetSocket(int s, struct sockaddr_storage *saddr, int saddrlen);
const string WebGetURLHost (string url);
const string WebGetURLPort (string url);
const string WebGetURLFile (string url);
int WebGetFile (string url, char *buffer, int maxsize);
};
#endif