int main() { printf("Welcome to redKlyde's Networking Tutorials!\n"); printf("Tutorial #5 : Multiple Hosts - ClientSide\n\n"); // My socket int mySocket; // Create our client mySocket = startupClient(7654, "localhost"); if (mySocket == -1) { // Hmm... an error occurred. Let's shutdown our client and bail. shutdownClient(mySocket); return 1; } // Number of bytes sent int nBytes; // I will define a message size so we will know how much data we will send and receive #define MAX_MESSAGE_SIZE 4096 char buffer[MAX_MESSAGE_SIZE]; printf("Input text to send and press Enter\nType 'quit' to exit\n"); // The main loop for (;;) { // Get some input from the command line gets(buffer); // Check if quit was typed, then exit if (strcmp(buffer, "quit") == 0) { break; } // Get our message size unsigned long messageSize = strlen(buffer); // Fix our byte ordering messageSize = htonl(messageSize); // Send the message size if ((nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } // Re-fix our byte ordering messageSize = ntohl(messageSize); // Send the actual message if ((nBytes = send(mySocket, buffer, messageSize, 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } } // Shutdown our client shutdownClient(mySocket); return 0; }
int main() { printf("Welcome to redKlyde's Networking Tutorials!\n"); printf("Tutorial # 3 : Sending and Receiving - ClientSide\n\n"); int mySocket; // Create our client mySocket = startupClient(7654, "localhost"); if (mySocket == -1) { // Hmm... an error occurred. Shutdown our client and bail shutdownClient(mySocket); return 1; } // The number of bytes I send/read ... will also serve as my error code int nBytes; // I will define a message size so we will know how much data we will send and receive #define MESSAGE_SIZE 24 // And the actual buffers char inMessage[MESSAGE_SIZE]; char outMessage[MESSAGE_SIZE] = "I sent you this message"; // Send the message to the server nBytes = send(mySocket, outMessage, sizeof(outMessage), 0); // check for errors if (nBytes == SOCKET_ERROR) { printf("Send Failed!\n"); } else { // Print the message we sent printf("Message Sent : %s\n", outMessage); } // Receive the reply from the server nBytes = recv(mySocket, inMessage, sizeof(inMessage), 0); // Check for errors if (nBytes == SOCKET_ERROR) { printf("Recv Failed!\n"); } else { // Print the received message printf("Message Received : %s\n", inMessage); } // Shutdown our client shutdownClient(mySocket); printf("Press any key to continue ...\n"); getchar(); return 0; }
int main() { printf("Welcome to redKlyde's Networking Tutorials!\n"); printf("Tutorial # 4 : Flexible Sending - ClientSide\n\n"); int mySocket; // Create our client mySocket = startupClient(7654, "localhost"); if (mySocket == -1) { // Hmm... an error occurred. Let's shutdown our client and bail. shutdownClient(mySocket); return 1; } // The number of bytes I send/read ... will also serve as my error code int nBytes; // Now let's make a buffer for sending our message. Better make it a big enough // size to send the largest message we would ever want to send. #define MAX_MESSAGE_SIZE 4096 char buffer[MAX_MESSAGE_SIZE]; // Let's fill in our buffer with some data. It doesn't matter what data we use, the important part // is that we do not pre-determine the size of the data. sprintf(buffer, "This is a test of my variable length send buffer"); // Calculate the size of that data unsigned long messageSize = strlen(buffer); // Remember to fix our byte ordering to network byte ordering messageSize = htonl(messageSize); // Send the message size nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0); // Check for errors if (nBytes == SOCKET_ERROR) { printf("Send Failed!\n"); } // Fix our message size back to host ordering because we will need it to send our message messageSize = ntohl(messageSize); // Send the actual message nBytes = send(mySocket, buffer, messageSize, 0); // Check for errors if (nBytes == SOCKET_ERROR) { printf("Send Failed!\n"); } // And that is that. This concludes the basics of sending and receiving. // If you think you know your stuff by now, try replacing the sprintf() with a gets() and type in your // message from the command line. This way you can see the variable length sending REALLY do its job. // If you get even more daring, alter the code on both client and server to have send () / recv() loops and // print out all the messages you are sending, instead of having to shutdown and restart the applications // each time. // Let's not forget to clean up properly. // Shutdown our client shutdownClient(mySocket); printf("Press any key to continue ...\n"); getchar(); return 0; }