Sending Score Messages in Network Applications: A Structured Approach, Study notes of Computers and Information technologies

How to send score messages in network applications using a structure as a buffer. It covers the need to send player and opponent scores, the use of a structure to simplify programming logic, and the mechanics of buffer use. An excerpt from a university lecture on systems programming.

Typology: Study notes

2010/2011

Uploaded on 09/10/2011

aristocrat
aristocrat 🇬🇧

5

(5)

240 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sending a (structure) message
(Client-side example)
struct Game_message Message; // Create a new message instance
Message.iMessage_Type = 1; // Set the type of message
// Initialise rest of message structure here
int iBytesSent = send( iSocket, (char*) &Message, sizeof(Message), 0 );
if(SOCKET_ERROR == iBytesSent)
{
MessageBox(“Send failed",“Game Client"); }
}
Systems Programming Richard Anthony, Computer Science, The University of Greenwich
This addendum illustrates how a structure can be used
directly as a buffer in TCP/UDP applications
This first slide is part of lecture 4 - Network Application
Development part 2
(it is included here to set the scene)
pf3
pf4
pf5

Partial preview of the text

Download Sending Score Messages in Network Applications: A Structured Approach and more Study notes Computers and Information technologies in PDF only on Docsity!

Sending a (structure) message

(Client-side example)

struct Game_message Message; // Create a new message instance

Message.iMessage_Type = 1; // Set the type of message

// Initialise rest of message structure here

int iBytesSent = send( iSocket, (char*) &Message, sizeof(Message)

if(SOCKET_ERROR == iBytesSent)

MessageBox(“Send failed",“Game Client"); }

Systems Programming Richard Anthony, Computer Science, The University of Greenwich

This addendum illustrates how a structure can be used

directly as a buffer in TCP/UDP applications

This first slide is part of lecture 4 - Network Application Development part 2 (it is included here to set the scene)

Consider the need to send the following information:

The current player score (and name for confirmation), and the

opponent’s score and also their name for confirmation).

First, we can represent this in the form of a structure, making our

program logic, and thus coding, simpler :

struct Score_message {

char cPlayerName[20];

int iPlayerScore;

char cOpponentName[20];

int iOpponentScore;

When to send? – after a player makes a move,

or after a game

has ended

What sends this message? – the server, because it holds

In practice, we can use the structure itself as the buffer, since it is a pre- allocated, fixed-size block of memory: cPlayerName iPlayerScore cOpponentName iOpponentScore 20 bytes 4 bytes 20 bytes (^) 4 bytes struct Score_message Message; will create an instance of the structure The notation (char*) &Message is equivallent to the addrerss of the start of the sizeof(Score_message) will return the value 28 (the length of the message i.e Therefore we can use the structure directly as the buffer, in for example the ca int iBytesSent = send( iSocket, (char*) &Message , sizeof(Message) , 0 ); (i.e. The buffer is defined precisely, by the combination of start address and le

Buffer – memory space allocated, large enough to hold the data expected Buffer containing data to be sent Operating- system maintained communication buffer – holds received messages until passed to owner process The network separates the sender and Application process (in this example this is the server, sending a Score-message to a client) ‘send’ call ‘recv’ call Applicati on process (in this example this is the client) Transfer

Transfer