Fellixombc
June 17th, 2010, 19:40
Yes, this was terribly written by me, but, I'm looking for tips/advice.
It currently can handle multiple users and messaging.
Server.cpp
/*
* File: server.cpp
* Author: fellixombc
*
* Created on May 23, 2010, 2:17 PM
*/
#include "server.h"
/** Sets the needed info
* @param port to listen on
*/
Server::Server(int setport) {
mPort = setport;
}
/** Starts the server
*/
void Server::Start() {
mSocket = socket(AF_INET, SOCK_STREAM, 0);
cout << "Socket succesfully started." << endl;
if (setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, &mTrue, sizeof(int)) < 0) {
cout << "ERROR" << endl;
exit(1);
}
server.sin_family = AF_INET;
server.sin_port = htons(mPort);
server.sin_addr.s_addr = INADDR_ANY;
mSize = sizeof(server);
if(bind(mSocket, (struct sockaddr*)(&server), mSize) < 0) {
cout << "ERROR" << endl;
exit(1);
}
cout << "Starting server on port " << mPort << endl;
cout << "LIVE" << endl;
}
/** Listens to the incoming packets
*/
void Server::Listen() {
const int size = 512;
int status;
int flags;
int selectCon;
struct timeval timev;
timev.tv_sec = 0.5;
timev.tv_usec = 1;
fd_set fd;
listen(mSocket, 5);
cout << "Listening." << endl;
while(1) {
char buffer[size] = "\0";
if(mConnectionPool.size() < 1) {
mConnection = accept(mSocket, (struct sockaddr*)(&client), &mSize);
if(mConnection != -1) {
mConnectionPool.push_back(mConnection);
cout << "Connection " << mConnection << " handled and added." << endl;
cout << "Size of connection pool: " << mConnectionPool.size() << endl;
flags = fcntl(mConnection, F_GETFL, 0);
fcntl(mConnection, F_SETFL, flags | O_NONBLOCK);
}
} else if(mConnectionPool.size() > 0) {
FD_ZERO(&fd);
FD_SET(mSocket, &fd);
selectCon = select(FD_SETSIZE, &fd, NULL, NULL, &timev);
if(selectCon == 1) {
mConnection = accept(mSocket, (sockaddr*)(&client), &mSize);
flags = fcntl(mConnection, F_GETFL, 0);
fcntl(mConnection, F_SETFL, flags | O_NONBLOCK);
mConnectionPool.push_back(mConnection);
cout << "Connection handled and added." << endl;
cout << "Size of connection pool: " << mConnectionPool.size() << endl;
}
for(int i = 0; i < mConnectionPool.size(); i++) {
status = read(mConnectionPool[i], buffer, size);
if(status > 0) {
for(int e = 0; e < mConnectionPool.size(); e++) {
cout << "Handling message: " << endl
<< buffer
<< "dispatching to connection "
<< mConnectionPool[e] << endl;
send(mConnectionPool[e], &buffer, (socklen_t)sizeof(buffer), 0);
}
} else if(status == 0) {
mConnectionPool.erase(mConnectionPool.begin()+i, mConnectionPool.begin() + i + 1);
cout << "Connection " << mConnectionPool[i] << " Erased." << endl;
cout << "There are "
<< mConnectionPool.size()
<< " connection(s) remaining" << endl;
} else if(status < 0) { }
}
}
}
}
Server.h
/*
* File: server.h
* Author: fellixombc
*
* Created on May 23, 2010, 2:17 PM
*/
#ifndef _SERVER_H
#define _SERVER_H
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <cstdlib>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
using namespace std;
class Server {
public:
Server(int);
void Start();
void Listen();
private:
struct sockaddr_in server, client;
int mPort;
int mTrue;
int mConnection;
int mSocket;
socklen_t mSize;
vector<int> mConnectionPool;
};
#endif /* _SERVER_H */
main.cpp
/*
* File: main.cpp
* Author: fellixombc
*
* Created on May 23, 2010, 2:14 PM
*/
#include <iostream>
#include "server.h"
using namespace std;
int main() {
Server Server(6000);
Server.Start();
Server.Listen();
return 0;
}
It currently can handle multiple users and messaging.
Server.cpp
/*
* File: server.cpp
* Author: fellixombc
*
* Created on May 23, 2010, 2:17 PM
*/
#include "server.h"
/** Sets the needed info
* @param port to listen on
*/
Server::Server(int setport) {
mPort = setport;
}
/** Starts the server
*/
void Server::Start() {
mSocket = socket(AF_INET, SOCK_STREAM, 0);
cout << "Socket succesfully started." << endl;
if (setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, &mTrue, sizeof(int)) < 0) {
cout << "ERROR" << endl;
exit(1);
}
server.sin_family = AF_INET;
server.sin_port = htons(mPort);
server.sin_addr.s_addr = INADDR_ANY;
mSize = sizeof(server);
if(bind(mSocket, (struct sockaddr*)(&server), mSize) < 0) {
cout << "ERROR" << endl;
exit(1);
}
cout << "Starting server on port " << mPort << endl;
cout << "LIVE" << endl;
}
/** Listens to the incoming packets
*/
void Server::Listen() {
const int size = 512;
int status;
int flags;
int selectCon;
struct timeval timev;
timev.tv_sec = 0.5;
timev.tv_usec = 1;
fd_set fd;
listen(mSocket, 5);
cout << "Listening." << endl;
while(1) {
char buffer[size] = "\0";
if(mConnectionPool.size() < 1) {
mConnection = accept(mSocket, (struct sockaddr*)(&client), &mSize);
if(mConnection != -1) {
mConnectionPool.push_back(mConnection);
cout << "Connection " << mConnection << " handled and added." << endl;
cout << "Size of connection pool: " << mConnectionPool.size() << endl;
flags = fcntl(mConnection, F_GETFL, 0);
fcntl(mConnection, F_SETFL, flags | O_NONBLOCK);
}
} else if(mConnectionPool.size() > 0) {
FD_ZERO(&fd);
FD_SET(mSocket, &fd);
selectCon = select(FD_SETSIZE, &fd, NULL, NULL, &timev);
if(selectCon == 1) {
mConnection = accept(mSocket, (sockaddr*)(&client), &mSize);
flags = fcntl(mConnection, F_GETFL, 0);
fcntl(mConnection, F_SETFL, flags | O_NONBLOCK);
mConnectionPool.push_back(mConnection);
cout << "Connection handled and added." << endl;
cout << "Size of connection pool: " << mConnectionPool.size() << endl;
}
for(int i = 0; i < mConnectionPool.size(); i++) {
status = read(mConnectionPool[i], buffer, size);
if(status > 0) {
for(int e = 0; e < mConnectionPool.size(); e++) {
cout << "Handling message: " << endl
<< buffer
<< "dispatching to connection "
<< mConnectionPool[e] << endl;
send(mConnectionPool[e], &buffer, (socklen_t)sizeof(buffer), 0);
}
} else if(status == 0) {
mConnectionPool.erase(mConnectionPool.begin()+i, mConnectionPool.begin() + i + 1);
cout << "Connection " << mConnectionPool[i] << " Erased." << endl;
cout << "There are "
<< mConnectionPool.size()
<< " connection(s) remaining" << endl;
} else if(status < 0) { }
}
}
}
}
Server.h
/*
* File: server.h
* Author: fellixombc
*
* Created on May 23, 2010, 2:17 PM
*/
#ifndef _SERVER_H
#define _SERVER_H
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <cstdlib>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
using namespace std;
class Server {
public:
Server(int);
void Start();
void Listen();
private:
struct sockaddr_in server, client;
int mPort;
int mTrue;
int mConnection;
int mSocket;
socklen_t mSize;
vector<int> mConnectionPool;
};
#endif /* _SERVER_H */
main.cpp
/*
* File: main.cpp
* Author: fellixombc
*
* Created on May 23, 2010, 2:14 PM
*/
#include <iostream>
#include "server.h"
using namespace std;
int main() {
Server Server(6000);
Server.Start();
Server.Listen();
return 0;
}