I am trying to understand how sockets work with winsock, but I just don't get it. I basically want to be able to create these functions:

SomeDataType ConnectToComputer(const char *ConnectionName);//connect to the connection named ConnectionName
SomeDataType CreateConnection(const char *ConnectionName);//create a connection called ConnectionName
bool ReadData(SomeDataType, unsigned char *outdata, int outsize);//read outsize bytes into outdata from the connection and return on success
void SendData(SomeDataType, unsigned char *data, int datalen);//send the data to the connection

Basically this is what I want to be able to do with those functions:

//This is called by the first computer to 'join' the room
SomeDataType myConnection=CreateConnection("My Room Name");
SendData(myConnection,"Connection up!",14);
vector<string> names;
unsigned char *buffer=new unsigned char[255];
while (ReadData(myConnection,buffer,255))
{
    names.push_back(string(buffer));
}
cout<<"The names of those in this connection are:"<<endl;
for (int i=0; i<names.size(); ++i)
    cout<<"\t"<<names[i]<<endl;


//This is called by the other computers to 'join' the room
SomeDataType myConnection=ConnectToComputer("My Room Name");
unsigned char *buffer=new unsigned char[255];
while (!ReadData(myConnection,buffer,14));
if (!stringsAreEqual(buffer,"Connection up!"))
    return -1;//this is the wrong room! It is not following the rules!
SendData(myConnection,"myName",6);//now the host will say my name to the screen

How do you do this? (I am willing to modify the functions)

Recommended Answers

Have you worked through some winsock tutorials? (click this)

Jump to Post

All 3 Replies

Have you worked through some winsock tutorials? (click this)

I have looked at a few winsock tutorials, I just do not understand them at all. None of them really give great explanations of how sockets work. For example how can I send data to a bunch of different computers at once. And how can I check if they have read it. I have very little understanding of how wireless stuff works, any explanation would be greatly appreciated.

I thought I should clarify a bit. I want to understand how to do this (or how to change it to do a similar task):

class Server
{
    private:
    //what?
    public:
    Server(string name);//create server with name 'name'
    ~Server();//close the server
    void send(unsigned char *data, int datalen);//send data to all the people connected to this server
    bool recieve(unsigned char *data, int &datalen);//recieve whatever data has been sent to the server, return true if there has been data recieved
};
class Client
{
    private:
    //what?
    public:
    Client(string name);//connect to the server named 'name'
    ~Client();//close the client
    void send(unsigned char *data, int datalen);//send data to all the people on the same server
    bool recieve(unsigned char *data, int &datalen);//recieve whatever data has been sent over the server, return true if there has been data recieved
};

The goal is to be able to do something like this:

Computer 1 server.cpp:

Server myServer("Computer1servername");
myServer.send("ONLINE",6);
bool quit=false;
while (!quit)
{
    unsigned char *data;
    int datalen;
    while (myServer.recieve(data,datalen))
    {
        if (compare(data,datalen,"QUIT",4))//assume compare already exists
            quit=true;
    }
}
myServer.send("OFFLINE",7);

Computer 2 client.cpp:

Client myClient("Computer1servername");
unsigned char *data;
int datalen;
while (!myClient.recieve(data,datalen));
if (compare(data,datalen,"ONLINE",6))//assume compare already exists
{
    myClient.send("QUIT",4);
}

I do not want raw source code. I want an understanding of what would be required.

Be a part of the DaniWeb community

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