From ae47a937601934d2038b703fff296380b58500c0 Mon Sep 17 00:00:00 2001 From: u8sand_cp Date: Sat, 17 Jul 2010 06:17:25 -0700 Subject: [PATCH] Some networking stuff in C++. If its possible to use it because dlls can be in any language, else I'm working on a C# version. --- Networking/readme.txt | 19 +++++ Networking/sock.h | 178 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 Networking/readme.txt create mode 100644 Networking/sock.h diff --git a/Networking/readme.txt b/Networking/readme.txt new file mode 100644 index 0000000..8eeff0d --- /dev/null +++ b/Networking/readme.txt @@ -0,0 +1,19 @@ + _____________ _____________ ___ __ +| | | | | / \ |\ | | \ +| | | | | / \ | \ | | \ +| | | | | / \ | \ | | \ +| | |-----------| |___________ /_________\ | \ | | | +| | | | | | | | \ | | | +| | | | | | | | \ | | / + \ / | | | | | | \ | | / + --------- |___________| ____________| | | | \| |__/ + +Well, this project is in C# but i've heard that we're using the a .dll for + the MUD Engine, and thats what I'll be coding. So I was wondering: I'm + most experienced with C++ and actually would have to go look for it if I + did C#, maybe I could simply code the dll in C++. You see, it doesnt matter + what the dll is coded in, when you link it you can link it to whatever + language. I actually already have a Socket class made in C++ making it even + EASIER for me. So for now I have the C++ Socket header here but I will look + and work on coding it in C#. + - u8sand \ No newline at end of file diff --git a/Networking/sock.h b/Networking/sock.h new file mode 100644 index 0000000..b8b7597 --- /dev/null +++ b/Networking/sock.h @@ -0,0 +1,178 @@ +#pragma once + +#include +#include + +namespace // unnammed namespace +{ + class Socket // socket class, used for all your socket needs. + { + public: + Socket() // constructor, dw about it + { + port = 0; + ip = 0; + type = 0; + sockAddrLen = sizeof(sockAddr); + ready = false; + } + ~Socket() // destructor, dw about it + { + port = 0; + ip = 0; + type = 0; + sockAddrLen = 0; + ready = false; + } + int init(int p,unsigned long i,int t) // init, always called first parameter is the socket's port, second + { // is the ip(if you want to use a String version see FindHostIP() @ the bottom + port = p; // for server's the ip to use is INADDR_ANY last is type, IPPROTO_UDP or IPPROTO_TCP + ip = i; // depending on what type you want UDP is used for any game and is allot more anoying + type = t; // to code while TCP is easy to code but never used for games. D: + if(port <= 0 || (type != IPPROTO_UDP && type != IPPROTO_TCP)) + { + ready = false; + return -1; + } + sockAddr.sin_family = AF_INET; + sockAddr.sin_port = htons(port); + sockAddr.sin_addr.S_un.S_addr = i; + ready = true; + return 1; + } + int start() // called after init, will "start" your socket + { + if(!ready) + return 0; + if(type == IPPROTO_UDP) + hSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); + else if(type == IPPROTO_TCP) + hSocket = hSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); + if(hSocket == INVALID_SOCKET) + throw -1; + return 1; + } + int bind() // binding for servers, will bind the socket to the port you specified earlier + { + if(!ready) + return 0; + if(::bind(hSocket,(sockaddr*)(&sockAddr),sizeof(sockAddr)) != 0) + return -1; + return 1; + } + int listen() // listen, only for TCP sockets, sets the server up to receive clients + { + if(!ready) + return 0; + if(::listen(hSocket, SOMAXCONN) != 0) + return -1; + return 1; + } + int connect() // for clients only, will connect to the ip specified earlier + { + if(!ready) + return 0; + if(::connect(hSocket, reinterpret_cast(&sockAddr),sizeof(sockAddr)) != 0) + return -1; + return 1; + } + int accept(Socket& sr) // accept will accept a new client connection (only technechly for TCP + { // but using it w/ UDP will accept the way UDP sockets do. + if(!ready) + return 0; + if(type == IPPROTO_TCP) + sr.hSocket = ::accept(hSocket, (struct sockaddr*)&sr.sockAddr, &sr.sockAddrLen); + else + sr.hSocket = hSocket; + if(sr.hSocket == INVALID_SOCKET) + return -1; + return 1; + } + int send(char* buf,int size) // send a packet of a character array, second parameter specify the + { // size of the packet(generally 256 or something) (TCP ONLY) + if(!ready) + return 0; + int val = ::send(hSocket, buf, size, 0); + if(val == SOCKET_ERROR) + return -1; + return 1; + } + int recv(char* &buf, int size) // recv a packet of a character array, second parameter specify the + { // size of the packet(generally 256 or something) (TCP ONLY) + if(!ready) + return 0; + int val = ::recv(hSocket, buf, size, 0); + if(val == SOCKET_ERROR) + return -1; + return 1; + } + int sendto(Socket& sr,char* buf,int size) // first parameter is the socket holding the data of the socket your + { // sending the data to, second is the packet, third is the size of your packet + if(!ready) + return 0; + int val = ::sendto(sr.hSocket, buf, size, 0, (struct sockaddr *)&sr.sockAddr, sr.sockAddrLen); + if(val == SOCKET_ERROR) + return -1; + return 1; + } + int recvfrom(Socket& sr,char* &buf,int size) // first parameter is the socket holding the data of the socket your + { // recieving the data from, second is the packet to hold the data, third is + if(!ready) // size of your packet + return 0; + int val = ::recvfrom(sr.hSocket,buf, size, 0, (struct sockaddr *)&sr.sockAddr, &sr.sockAddrLen); + if(val == SOCKET_ERROR) + return -1; + return 1; + } + int end() // closes the socket, call end after your finished using a socket + { + ready = false; + if(::closesocket(hSocket) == SOCKET_ERROR) + return -1; + return 1; + } + bool operator==(const Socket& csr) // == operator, for logic test between two sockets checks if the IPs are the same + { + if(sockAddr.sin_addr.S_un.S_addr == csr.sockAddr.sin_addr.S_un.S_addr) + return true; + return false; + } + SOCKET hSocket; + struct sockaddr_in sockAddr; + int sockAddrLen; + private: + int port; + int type; + unsigned long ip; + bool ready; + }; + int initWinsock(WSADATA& wsaData,const int iReqWinsockVer) // call this at the top of your program, initiallizes winsock + { + if(WSAStartup(MAKEWORD(iReqWinsockVer,0),&wsaData) == 0) + { + if(LOBYTE(wsaData.wVersion) >= iReqWinsockVer) + return 1; + else + return -2; + } + else + return -1; + } + int endWinsock() // call this at the end of your program, cleans up winsock + { + return WSACleanup(); + } + u_long FindHostIP(const char *pServerName) // accepts a string of your ip and it converts it to unsigned long + {// example FindHostIP("127.0.0.1") + HOSTENT *pHostent; + + if (!(pHostent = gethostbyname(pServerName))) + return 0; + else + { + if (pHostent->h_addr_list && pHostent->h_addr_list[0]) + return *reinterpret_cast(pHostent->h_addr_list[0]); + } + return 0; + } +}