Thread: help with client program

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    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
    Last edited by chris285; 01-02-2005 at 06:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main()
    Wrong - you've been here long enough to know that by now.

    Also, its [code] and not [quote] tags when posting code

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    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?
    Last edited by chris285; 01-02-2005 at 06:03 AM.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    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.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    84
    to o-o the code is the best way to encompass most of what is there under say a connection class?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    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

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    84
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server and client in the same program
    By smithx in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-25-2008, 09:30 PM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Inserting text into MDI program
    By Rutabega in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2005, 11:25 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM