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;


	}
};

Recommended Answers

All 8 Replies

port (for example) is just a local variable inside the function.

If you want it to be visible anywhere else, then it needs to be a class member variable.

I tried that before
and got and error


Error 1 error C2864: 'Base::port' : only static const integral data members can be initialized within a class j:\trying\trying\trying.cpp 23

class Base
{

	public:
SOCKET scSocket;
		SOCKET acceptSocket;
	
	
		
		int wsaerr;
	
		int port = 55555;
	

	int Socket()

Start with a simple class, one variable, one function - and read your C++ book some more.

When you know how it works, then expand that knowledge into the code you have.

unfortunetly this is work for uni and therefore time to read is not really a given. This is the kind of work they expect after 5 hours of learning and only 2 of them actually using a computer

will you do yourself a favor and check this I'm not maverick at sockets but what I know is you have to
1.Create a socket
2.Bind it to an address
3.connect to server
4.create buffer and fill data
5.send data
6. close sockets

and another thing, It is done differently in different platforms and toolkit. I would use wxidgets toolkit, if i was you, but then time isn't enough to learn anything

there is also short tutorial here. try and see if it will help you

I cracked it

class Server:Base

doesn't work it should just be the class name then inside you can call the base class
Base base;

that allows you to do base. variable your after

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.