2 registered members (TipmyPip, AndrewAMD),
911
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
[wanted_to_share_my] C++ Custom Client And Server Socket Class
#232183
10/20/08 00:40
10/20/08 00:40
|
Joined: Aug 2008
Posts: 61
Neurosys
OP
Junior Member
|
OP
Junior Member
Joined: Aug 2008
Posts: 61
|
Hello, My next step in my prototype was to implement some old socket code I had laying around in the guts of an irc bot I wrote. MySocket.h defines the MySock class and I've given it all the functions it needs to demonstrate the sockets. Here is the Main Program So you can see all my includes and whatnot. #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <winsock.h>
#include <iostream>
#include <string>
using namespace std;
#define socklen_t int
#include "adll.h" // Include the engine data types, variables, and functions
#include "mtlFX.h" //Only the ones in .fx files work the ones defined in quotes dont. idk why.
#include "NewGoo.h"
#include "NeuroSocket.h"
//Prototypes
var rad2deg(float radians);
float deg2rad(var degrees);
var abz(var val);
//Globals
ENTITY* skycube;
BMAP* mcursor;
TEXT* tDebug;
TEXT* tDebug2;
STRING* indata;
bool servermode;
var abz(var val) {
var retval;
if (val >= _VAR(0)) { return val; }
retval = _VAR(0) - val;
return retval;
}
float deg2rad(var degrees) {
float deg2rad = (float)(degrees / 57.29578);
return deg2rad;
}
var rad2deg(float radians) {
var rad2deg = _VAR(radians * 57.29578);
return rad2deg;
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) {
ev = engine_open(NULL);
if (!ev) return 1; // acknex.dll not found
//Add Content Folders Paths
add_folder("terrains");
add_folder("images");
add_folder("images\\newgoo");
add_folder("models");
add_folder("code");
//Load Blank Level
level_load("");
engine_frame();
//Initialize The Graphics Environment
video_set(_VAR(1280),_VAR(1024),_VAR(32),_VAR(0));
//Make a Pretty Sky
skycube = (ENTITY *)ent_createlayer("Sky_2+6.tga", _VAR(SKY | CUBE | VISIBLE), _VAR(0));
//Bring Up The Mouse
mcursor = bmap_create("brokenarrow.pcx");
ev->mouse_map = mcursor;
v(mouse_mode)=2;
tDebug=txt_create(_VAR(5),_VAR(3));
tDebug->flags |= VISIBLE;
tDebug2=txt_create(_VAR(5),_VAR(3));
tDebug2->flags |= VISIBLE;
tDebug2->pos_x = _VAR(600);
tDebug2->pos_y = _VAR(0);
//SETUP WSA
WSADATA wsaData; // if this doesn't work
//WSAData wsaData; // then try this instead
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
}
MySock tester("localhost",333);
str_cat(tDebug->pstring[0],_chr(tester.MyBuf));
str_cpy(tester.MyBuf,"");
MySock testserver("localhost",333,10); //<SC>
str_cat(tDebug2->pstring[0],_chr(testserver.sBuf)); //<SC>
str_cpy(testserver.sBuf,""); //<SC>
tester.Connect();
str_cat(tDebug->pstring[0],_chr(tester.MyBuf));
str_cpy(tester.MyBuf,"");
//Main Loop
while (engine_frame()) {
//Main Loop
if (tester.getz() != -1) {
if (str_len(_chr(tester.MyBuf)) > 0 ) {
str_cat(tDebug->pstring[1],_chr(tester.MyBuf));
str_cpy(tester.MyBuf,"");
}
}
testserver.ServerProcess(); //<SC>
str_cat(tDebug2->pstring[0],_chr(testserver.sBuf)); //<SC>
str_cpy(testserver.sBuf,""); //<SC>
v(mouse_pos).x = v(mouse_cursor).x;
v(mouse_pos).y = v(mouse_cursor).y;
if (v(key_esc)) { sys_exit(""); }
}//Main Loop
engine_close();
return 0;
} and here is the contents of mysocket.h where the class is actually defined. //MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket
//MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket
//MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket
//MySocket MySocket MySocket MySocket MySocket MySocket MySocket MySocket
class MySock {
public:
MySock(char* host,int port);
MySock(char* host,int port,int maxq);
STRING* Getip(STRING* hname);
//STRING* Getaddr(STRING* hip); //Doesnt Work As Expected. TODO
int Connect();
int getz();
int putz(char putdata[]);
int ServerProcess();
struct timeval tv;
int MyPort;
STRING* MyHostname;
bool CONNECTED;
int packetsize;
int sockfd, numbytes;
char buf[4096];
STRING* MyBuf;
STRING* sBuf;
char sendbuf[1024];
struct hostent *he;
struct sockaddr_in their_addr;
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
struct sockaddr_in myaddr; // server address
struct sockaddr_in remoteaddr; // client address
int fdmax; // maximum file descriptor number
int listener; // listening socket descriptor
int newfd; // newly accept()ed socket descriptor
char sbuf[4096]; // buffer for server/client data
int nbytes;
int yes; // for setsockopt() SO_REUSEADDR, below
socklen_t addrlen;
int i, j;
};
MySock::MySock(char* host,int port) {
tv.tv_sec =0;
tv.tv_usec=0;
CONNECTED = 0; //Not Connected Yet, Duh!
MyBuf=str_create("");
sBuf=str_create("");
//Create Socket And Get the FILE DESCRIPTOR(fd)/Handle
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
str_cat(MyBuf,":ERROR> Cannot Create Socket (fd)\n");
//perror("socket");
}
//Setup Host And Port
MyHostname = str_create(" ");
MyHostname = str_cpy(MyHostname,host);
MyPort = port;
packetsize=4096;
str_cat(MyBuf,":CLIENTSOCKET> Hostname: \'");
str_cat(MyBuf,host);
str_cat(MyBuf,"\' Port: \'");
str_cat_num(MyBuf,"%0.f",_VAR(port));
str_cat(MyBuf,"\'\n");
};
STRING* MySock::Getip(STRING* hname){
STRING* ipaddress;
ipaddress=str_create(" ");
struct hostent *hhh;
if ((hhh=gethostbyname(_chr(hname))) == NULL) { // get the host info
//cout<<":SOCKET> Error Getting Host\n";
str_cat(ipaddress,":SOCKET-getip> Could Not Resolve Hostname.\n");
str_cat(MyBuf,":SOCKET-getip> Could Not Resolve Hostname.\n");
} else {
ipaddress = str_cpy(ipaddress,(inet_ntoa(*((struct in_addr *)hhh->h_addr))));
}
//ipaddress = (inet_ntoa(*((struct in_addr *)h->h_addr)));
return ipaddress;
};
//STRING* MySock::Getaddr(STRING* hip){
// STRING* ipaddress;
// ipaddress=str_create(" ");
// struct hostent *hhh;
// if ((hhh=gethostbyaddr(_chr(hip),(int)str_len(_chr(hip)), AF_INET)) == NULL) { // get the host info
// //cout<<":SOCKET> Error Getting Host\n";
// str_cat(ipaddress,":SOCKET-getaddr> Could Not Resolve Host IP.\n");
// str_cat(MyBuf,":SOCKET-getaddr> Could Not Resolve Host IP.\n");
// } else {
// ipaddress = str_cpy(ipaddress,(inet_ntoa(*((struct in_addr *)hhh->h_name))));
// }
// //ipaddress = (inet_ntoa(*((struct in_addr *)h->h_addr)));
// return ipaddress;
//};
int MySock::Connect() {
STRING* ipaddress;
ipaddress = Getip(MyHostname);
//if(debugmode){cout<<":SOCKET> Hostname "<<Hostname.c_str()<<" Resolved To "<<ipaddress<<"\n";}
//if(debugmode){cout<<":SOCKET> Connecting To "<<ipaddress<<" On Port "<<Port<<endl;}
if ((he=gethostbyname(_chr(MyHostname))) == NULL) { // get the host info
//cout<<":SOCKET> Error Getting Host\n";
str_cat(MyBuf,":SOCKET-connect> Could Not Resolve Hostname.\n");
} else {
str_cat(MyBuf,":SOCKET> Attempting To Connect To \'");
str_cat(MyBuf,_chr(ipaddress));
str_cat(MyBuf,"\'\n");
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(MyPort); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof their_addr) == -1) {
//perror("connect");
str_cat(MyBuf,":ERROR> Cannot Connect!!\n");
return -1;
} else {
//if(debugmode){cout<<":SOCKET> Connected!\n\n";}
str_cat(MyBuf,":SUCCESS> CONNECTED!\n");
CONNECTED = 1;
return 0;
}
}
};
int MySock::getz() {
int fdmax;
fd_set master;
fd_set read_fds;
FD_ZERO(&read_fds);
FD_ZERO(&master);
FD_SET(sockfd, &master);
fdmax = sockfd;
int nbytes;
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, &tv) == -1) {
perror("select");
return -1;
}
if (FD_ISSET(sockfd, &read_fds)) {
// handle data from a client
if ((nbytes = recv(sockfd, buf, sizeof buf, 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
// connection closed
//if(debugmode){cout<<":SOCKET> Server Hung up On Socket "<<sockfd<<endl;}
} else {
//if(debugmode){cout<<":SOCKET> Server Connection Error.\n";}
//perror("recv");
}
closesocket(sockfd); // bye!
FD_CLR(sockfd, &master); // remove from master set
return -1;
} else {
//finally got some data.
buf[nbytes] = '\0';
//getdata = str_cpy(getdata,buf);
str_cat(MyBuf,buf);
}
}
return 0;
};
int MySock::putz(char putdata[]) {
int cnt = 0;
while ( cnt < strlen(putdata) ) {
sendbuf[cnt] = putdata[cnt];
cnt+=1;
}
sendbuf[cnt] = '\0';
//str_cat(MyBuf,sendbuf);
//sendbuf[str_len(_chr(putdata))] = '\0';
//cout<<"SENDBUF = " << sendbuf<<endl;
if (send(sockfd,sendbuf,strlen(putdata), 0) == -1) {
closesocket(sockfd); // bye!
return -1;
} else {
return 0;
}
};
MySock::MySock(char* host,int port,int maxq) {
tv.tv_sec =0;
tv.tv_usec=0;
MyHostname = str_create(" ");
MyHostname = str_cpy(MyHostname,host);
MyPort = port;
packetsize=4096;
MyBuf=str_create("");
yes = 1;
sBuf=str_create("");
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
// get the listener
if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
//perror("socket");
//exit(1);
str_cat(sBuf,"ERROR SETTING UP SOCKET\n");
}
// lose the pesky "address already in use" error message
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes,sizeof(int)) == -1) {
//perror("setsockopt");
//exit(1);
str_cat(sBuf,"ERROR SETTING SOCKET OPTIONS\n");
}
// bind
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = INADDR_ANY;
myaddr.sin_port = htons(port);
memset(&(myaddr.sin_zero), '\0', 8);
if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
//perror("bind");
//exit(1);
str_cat(sBuf,"ERROR BINDING\n");
}
// listen
if (listen(listener, maxq) == -1) {
//perror("listen");
//exit(1);
str_cat(sBuf,"ERROR LISTENING\n");
} else {
str_cat(sBuf,"Listening For New Clients...\n");
}
// add the listener to the master set
FD_SET(listener, &master);
// keep track of the biggest file descriptor
fdmax = listener; // so far, it’s this one
};
int MySock::ServerProcess() {
read_fds = master;
if (select(fdmax+1, &read_fds, NULL, NULL, &tv) == -1) {
str_cpy(sBuf,"ERROR POLLING SELECT/read_fds\n");
return -1;
}
// run through the existing connections looking for data to read
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!
if (i == listener) {
// handle new connections
addrlen = sizeof(remoteaddr);
if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr,&addrlen)) == -1) {
str_cat(sBuf,":ERROR> New Client Connection FAILED TO ACCEPT\n");
} else {
FD_SET(newfd, &master); // add to master set
if (newfd > fdmax) { // keep track of the maximum
fdmax = newfd;
}
//printf("selectserver: new connection from %s on socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
str_cat(sBuf,":SERVER> New Client Connection ACCEPTED From IP \'");
str_cat(sBuf,inet_ntoa(remoteaddr.sin_addr));
str_cat(sBuf,"\' On Socket #");
str_cat_num(sBuf,"%0.f",_VAR(newfd));
str_cat(sBuf,"\n");
strcpy(buf,"Welcome To The Master Control Server (MCS)\n");
if (send(newfd, buf,strlen(buf), 0) == -1) {
str_cat(sBuf,":ERROR> SEND ERROR\n");
}
}
} else {
// handle data from a client
if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
// connection closed
//printf("selectserver: socket %d hung up\n", i);
str_cat(sBuf,":SERVER> Connection Hung Up (\'");
str_cat(sBuf,inet_ntoa(remoteaddr.sin_addr));
str_cat(sBuf,"\' On Socket #");
str_cat_num(sBuf,"%0.f",_VAR(i));
str_cat(sBuf,")\n");
} else {
str_cat(sBuf,":ERROR> RECV ERROR!!\n");
}
closesocket(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
// we got some data from a client
for(j = 0; j <= fdmax; j++) {
// send to everyone!
if (FD_ISSET(j, &master)) {
// except the listener and ourselves
if (j != listener && j != i) {
if (send(j, buf, nbytes, 0) == -1) {
str_cat(sBuf,":ERROR> SEND ERROR\n");
}
}
}
}
return 0;
}
} // it’s SO UGLY! //No Beej, It Is Beautiful.
}
}
return -1;
}; I'll have more examples soon. I have a job now so I only get to do this on the weekends now. Neurosys
Last edited by Neurosys; 10/20/08 00:43.
|
|
|
|