hello i'm using C++ for the first time on my course after a year of c#. We are suppose to make a client and server using pre existing code. One file was server one was of a client. There must be 3 classes a common class/ base class and a server and client class. (and the main ofc) It must also be OO.
I''m having real trouble sharing varibles or methods between classes and so far I have done a split the way i think is best. having everything common up until the bind for server and listen and the client connect for client. Can anyone suggest where i'm wrong. Like I say i'm not a very talented coder and have trouble understanding concepts and reading code sometimes. I believe that pointers maybe the best way to share the varibles. In c# i often shared the varibles using
class object = new class;
object.varible;
however this doesnt work with c++ it seems and i believe I must use pointers. However i'm unsure how to use pointer to pick uop data inside a function and give to another function of a seperate class
I get unidentified erorrs for the varibles
port
scscoket
acceptescoket
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
using namespace std;
class Base
{
public:
int Socket()
{
SOCKET scSocket;
SOCKET acceptSocket;
WSADATA wsaData;
int wsaerr;
WORD wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
int port;
int* Pport;
Pport = &port;
*Pport = 55555;
if (wsaerr != 0)
{
printf("The Winsock dll not found!\n");
return 0;
}
else
{
printf("The Winsock dll found!\n");
printf("The status: %s.\n", wsaData.szSystemStatus);
}
scSocket = INVALID_SOCKET;
scSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //AF_INET, SOCK_STREAM, IPPROTO_TCP
if (scSocket== INVALID_SOCKET)
{
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
else
{
printf("socket() is OK!\n");
}
}
};
class Client:Base
{
public:
int clientrun()
{
Base base;
base.Socket();
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(port);
if (connect(scSocket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR){
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
else {
printf("Client: connect() is OK.\n");
printf("Client: Can start sending and receiving data...\n");
}
system("pause");
WSACleanup();
return 0;
}
};
class Server:Base
{
public:
int serverrun()
{
Base base;
base.Socket();
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(port);
if (bind(scSocket, (SOCKADDR*)&service, sizeof(service))== SOCKET_ERROR)
{
printf("bind() failed: %ld.\n", WSAGetLastError());
closesocket(scSocket);
WSACleanup();
return 0;
}
else
{
printf("bind() is OK!\n");
}
if (listen(scSocket, 1) == SOCKET_ERROR)
printf("listen(): Error listening on socket %ld.\n", WSAGetLastError());
else
printf("listen() is OK, I'm waiting for connections...\n");
acceptSocket = accept(scSocket, NULL,NULL);
if (acceptSocket == INVALID_SOCKET)
{
fprintf(stderr, "accept failed: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("Accepted connection \n");
system("pause");
WSACleanup();
return 0;
}
};