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