PDA

View Full Version : [C++] Chat Server



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;
}

Wise Old Man
June 17th, 2010, 20:58
Nice. By "Chat Server" do you mean something like xat or ventrillo?

Fellixombc
June 17th, 2010, 21:20
lick irc

dahunta97
June 18th, 2010, 15:15
C++ is an interesting language, and the ability to compile it straight to an executable file is a luxury, but I still prefer the Java simplicity and large API library -- despite the fact that it needs a JVM to run. Good tut though, you didn't really show us HOW to do it as much as you did just give is ready code.

Aaron
June 18th, 2010, 15:22
C++ is an interesting language, and the ability to compile it straight to an executable file is a luxury, but I still prefer the Java simplicity and large API library -- despite the fact that it needs a JVM to run. Good tut though, you didn't really show us HOW to do it as much as you did just give is ready code.

Agreed of how interesting it is, It's also very hard to me on the other side.

dahunta97
June 18th, 2010, 15:27
What I dislike mostly about Java is the really confusing public/private void/etc. thing. Sometimes, you'll examine tutorials, and people will use public/private void, no problem. Then you'll seem the use public/private. And then they mix in static. I don't really get that yet. It confuses me a lot: I wonder, "should I use this for this feature? it sounds more appopriate for that...but still"

Fellixombc
June 18th, 2010, 16:55
What I dislike mostly about Java is the really confusing public/private void/etc. thing. Sometimes, you'll examine tutorials, and people will use public/private void, no problem. Then you'll seem the use public/private. And then they mix in static. I don't really get that yet. It confuses me a lot: I wonder, "should I use this for this feature? it sounds more appopriate for that...but still"

They use public, private, and static if iirc for coding conventions, so if someone goes in, they can tell easily what is what.

btw, this wans't a tutorial.

Equinox
June 18th, 2010, 17:08
why dont u just set up a IRC chatbox?
it would be a lot easier and lot less hassle

The Soul
June 18th, 2010, 17:09
why dont u just set up a IRC chatbox?
it would be a lot easier and lot less hassle

Because he did this for learning purposes and probably to learn the io (nio?) API.

Equinox
June 18th, 2010, 17:11
yerr proberly

The Soul
June 18th, 2010, 17:11
yerr proberly

No, I know he did. :D

Aaron
June 18th, 2010, 17:13
why dont u just set up a IRC chatbox?
it would be a lot easier and lot less hassle

Learning Purposes is probably why, as he even posted in his thread:)

Fellixombc
June 18th, 2010, 17:27
Yeh, learning purposes. The Soul knew cause I told him over msn.

Pie`
June 18th, 2010, 18:43
if(mConnectionPool.size() < 1) {
} else if(mConnectionPool.size() > 0) {

why not, .size() == 0
and } else {

unless the size is likely to be < 0?

The Soul
June 18th, 2010, 18:46
The only thing I have to comment on at the moment is the unused if-else blocks, where you're checking something but you're not even carrying out any actions if the condition is met thus rendering them pointless.

Fellixombc
June 18th, 2010, 18:57
The only thing I have to comment on at the moment is the unused if-else blocks, where you're checking something but you're not even carrying out any actions if the condition is met thus rendering them pointless.
I put them there for future use.


if(mConnectionPool.size() < 1) {
} else if(mConnectionPool.size() > 0) {

why not, .size() == 0
and } else {

unless the size is likely to be < 0?
No, size shouldn't be less then 0, and I can't honestly remember why I did that, I'll go ahead and change it though, thanks.
also: long time no see.

edit: I did that to make sure it doesn't loop through it unless there is a connection.

Canownueasy`
June 19th, 2010, 08:14
Thanks, I'll add some more things to it.

Mish
June 19th, 2010, 10:14
Looks cool Tbh:p