Well. You might do something like this:
Open a UDP port on the server side.
Wait for packets.
Have four major commands for storage access use a header in the UDP packet such as:
Code: Select all
struct tNetStorageHdr
{
uint8_t opcode;
};
Where if:
(opcode = 1) // OPEN
(opcode = 2) //CLOSE
(opcode = 3) // GET
(opcode = 4) // PUT
Then have a format for each opcode such that for OPEN:
Code: Select all
struct tNetStorageDataHdr
{
uint32_t fileHandle;
uint32_t filePos;
uint32_t length;
};
void GotPacket(void *data)
{
struct tNetStorageHdr *hdr = (struct tNetStorageHdr*)data;
switch(hdr->opcode)
{
case 1:
mode = *((uint32_t*)&hdr[1]);
filename = *((uint8_t*)( ((uintptr_t)&hdr[1]) + sizeof(uint32_t) );
// make sure it is okay to open it if someone else already has it opened.
// make entry who opened this file.
// send back packet with fileHandle
break;
case 2:
fileHandle = *((uint32_t*)&hdr[1]);
// close file with handle
break;
case 3:
struct tNetStorageDataHdr *dhdr = (struct tNetStorageDataHdr*)&hdr[1];
/// check if file handle is opened
/// read data specified
/// send packet back with data
break;
case 3:
/// same as READ, but in reverse.
}
}
The basic idea is to have:
CLIENT -> SERVER -> CLIENT
OPEN -> ... -> RESULT
CLOSE -> ... -> RESULT
READ -> ... -> RESULT
WRITE -> ... -> RESULT
Then keep reusing that NetStorageHdr so that you have a base in which to determine what type of packet something is. The client will need to implement a blocking until it gets a result packet.
// the client sends a packet with ...
send(socketDescriptor, buffer, bufferLength, 0);
// waits for the result ..
recvLen = recv(socketDescriptor, buffer, bufferMaxLength, 0);
// hands result back to core function
The OPEN and CLOSE help with:
- Access Control
- Earlier Error Control
- Increased Network And CPU Performance
Since they need to work with a descriptor which is a short identification for the actual filename you do not have to transmit the whole entire filename in a result or command packet. It allows a client to know there is a problem with a file before it has to allocate buffers and issue a read or write command. It allows clients to lock files or use certain modes to control access to that file while they are accessing it.
If you are working on linux you can at a console type:
man socket
This should get you started in the right direction to using sockets.