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)