-
help with client program
i'm coding a client in c++ using winsock, and it connects to a server and is an irc type chat program. i have so far got the cleint to connecto to the server but not send or receive anything, how can i object orient what i have in the best way?
Code:
#include <winsock2.h>
#include <stdio.h>
#include <iostream>
using namespace std;
void main()
{
int error;
WSAData wsaData;
error = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (error == SOCKET_ERROR) {
cout << "Could Not Start Up Winsock" << endl;
return;
}
cout << "WinSocket started" << endl;
int mySocket;
mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR) {
cout << "Error Opening Socket" << endl;
}
cout << "Socket Opened!" << endl;
char server_name[40] = "localhost";
struct hostent *host_entry;
host_entry = gethostbyname(server_name);
if (host_entry == NULL) {
cout << "Could not find host" << endl;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons((unsigned short) 23);
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
error = connect(mySocket, (sockaddr*)&server, sizeof(server));
if (error == SOCKET_ERROR) {
cout << "Error connecting to server" << endl;
}
closesocket(mySocket);
cout << "Socket Closed" << endl;
WSACleanup();
cout << "WinSocket Shutdown" << endl;
}
i think it needs to go into a connection class really, but i am having trouble getting it to work
-
> void main()
Wrong - you've been here long enough to know that by now.
Also, its [code] and not [quote] tags when posting code
-
i haven't coded for a while as i have been busy so i have forgotten a few things, and thats for the code warning
so it wants to be int main then, but am i thinking right for getting it object orientated?
-
Here's an irc bot I designed that uses threads for it. It's a simple bot that shows debugging info and can do a couple commands (like help and rolldice)
Rename this file to .zip to open it.
-
to o-o the code is the best way to encompass most of what is there under say a connection class?
-
You need to step back and think about what you want to achieve, then think about what sort of objects (and the actions they perform) are the most appropriate for the problem.
Hacking a "OO" program at the keyboard is a sure way of making a series of messy objects with messy interfaces.
-
To make a project object-oriented, just think about what kinds of data you have, and how you would organize that data, to take advantage of OOP. OO languages allow you to model data like you categorize it in the real world, so just pick how you would organize your data, and then define that in classes. Using your current code as a model, rewrite the program with your new data structures.
-
Something which always helps me, is to think about what would make the best since in being an object. To me it would encompass most, if not all the functionality the object needs (this sounds pretty basic) but you would be surprised how many people I've seen make something OO too complex by making too many classes, or too few classes. Think about it in terms of say boxes, each box contains a certain aspect of the whole. If this is a connection class it should be able to keep track of the connection, it should know everything about that connection, and should be able to operate on that connection. Seems simple I admit, but sometimes it can help to have it pointed out. I am not trying to be condesceding (sorry if I seem so), but basically think about each object and its role in the whole. Hopefully that ramble helps :)
-
yeah that makes sense, i think i am just gonna have a few classes to keep track of things
prob just a connect and disconnect clas, a send an receive and then a listen function but i may put that into the receive class