Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to write a client and server program to transfer a file from a sender to a receiver using TCP (the receiver is the

You are to write a client and server program to transfer a file from a sender to a receiver using TCP (the receiver is the server, the sender is the client). You are given a template for the sender and receiver and your task is to write the following two functions.

int recvFile(char *fileName, int portNum, int maxSize, int options);

int sendFile(char *fileName, char *destIpAddr, int destPortNum, int options);

There should be no changes needed to main().

//========================================= file = tcpFileSend_template.c ===== //= A file transfer program using TCP - this is the file send = //============================================================================= //= Notes: = //= 1) This program conditionally compiles for Winsock and BSD sockets. = //= Set the initial #define to WIN or BSD as appropriate. = //= 2) This program sends a file to tcpFileRecv = //= 3) This program take command line input as shown below = //= 4) Ignore build warnings on unused retcode and options. = //=---------------------------------------------------------------------------= //= Example execution: (for tcpFileSend sendFile.dat 127.0.0.1 1050) = //= Starting file transfer... = //= File transfer is complete = //=---------------------------------------------------------------------------= //= Build: cl tcpFileSend.c wsock32.lib for Winsock = //= gcc tcpFileSend.c -lnsl for BSD = //=---------------------------------------------------------------------------= //= Execute: tcpFileSend = //============================================================================= #define WIN // WIN for Winsock and BSD for BSD sockets

//----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for memcpy() and strcpy() #include // Needed for exit() #include // Needed for file i/o constants #include // Needed for file i/o constants #include // Needed for open(), close(), and eof() #ifdef WIN #include // Needed for all Winsock stuff #endif #ifdef BSD #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #endif

//----- Defines --------------------------------------------------------------- #define PORT_NUM 1050 // Port number used at the server #define SIZE 256 // Buffer size

//----- Prototypes ------------------------------------------------------------ int sendFile(char *fileName, char *destIpAddr, int destPortNum, int options);

//===== Main program ========================================================== int main(int argc, char *argv[]) { char sendFileName[256]; // Send file name char recv_ipAddr[16]; // Reciver IP address int recv_port; // Receiver port number int options; // Options int retcode; // Return code

// Usage and parsing command line arguments if (argc != 4) { printf("usage: 'tcpFileSend sendFile recvIpAddr recvPort' where "); printf(" sendFile is the filename of an existing file to be sent "); printf(" to the receiver, recvIpAddr is the IP address of the "); printf(" receiver, and recvPort is the port number for the "); printf(" receiver where tcpFileRecv is running. "); return(0); } strcpy(sendFileName, argv[1]); strcpy(recv_ipAddr, argv[2]); recv_port = atoi(argv[3]);

// Initialize parameters options = 0; // This parameter is unused in this implementation

// Send the file printf("Starting file transfer... "); retcode = sendFile(sendFileName, recv_ipAddr, recv_port, options); printf("File transfer is complete ");

// Return return(0); }

//============================================================================= //= Function to send a file using TCP = //============================================================================= //= Inputs: = //= fileName ----- Name of file to open, read, and send = //= destIpAddr --- IP address or receiver = //= destPortNum -- Port number receiver is listening on = //= options ------ Options (not implemented) = //=---------------------------------------------------------------------------= //= Outputs: = //= Returns -1 for fail and 0 for success = //=---------------------------------------------------------------------------= //= Side effects: = //= None known = //=---------------------------------------------------------------------------= //= Bugs: = //=---------------------------------------------------------------------------= int sendFile(char *fileName, char *destIpAddr, int destPortNum, int options) {

// need to be implemented in C }

//============================== file = tcpFileRecv_template.c ================ //= A file transfer program using TCP - this is the file receiver = //============================================================================= //= Notes: = //= 1) This program conditionally compiles for Winsock and BSD sockets. = //= Set the initial #define to WIN or BSD as appropriate. = //= 2) This program receives and writes a file sent from tcpFileSend. = //= 3) This program must be running first for tcpFileSend to connect to. = //= 4) Ignore build warnings on unused retcode, maxSize, and options. = //=---------------------------------------------------------------------------= //= Example execution: = //= Starting file transfer... = //= File transfer is complete = //=---------------------------------------------------------------------------= //= Build: cl tcpFileRecv.c wsock32.lib for Winsock = //= gcc tcpFileRecv.c -lnsl for BSD = //=---------------------------------------------------------------------------= //= Execute: tcpFileRecv = //=---------------------------------------------------------------------------= //============================================================================= #define WIN // WIN for Winsock and BSD for BSD sockets

//----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for memcpy() and strcpy() #include // Needed for exit() #include // Needed for file i/o constants #include // Needed for file i/o constants #include // Needed for open(), close(), and eof() #ifdef WIN #include // Needed for all Winsock stuff #endif #ifdef BSD #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #endif

//----- Defines --------------------------------------------------------------- #define PORT_NUM 1050 // Arbitrary port number for the server #define SIZE 256 // Buffer size #define RECV_FILE "recvFile.dat" // File name of received file

//----- Prototypes ------------------------------------------------------------ int recvFile(char *fileName, int portNum, int maxSize, int options);

//===== Main program ========================================================== int main() { int portNum; // Port number to receive on int maxSize; // Maximum allowed size of file int timeOut; // Timeout in seconds int options; // Options int retcode; // Return code

// Initialize parameters portNum = PORT_NUM; maxSize = 0; // This parameter is unused in this implementation options = 0; // This parameter is unused in this implementation

// Receive the file printf("Starting file transfer... "); retcode = recvFile(RECV_FILE, portNum, maxSize, options); printf("File transfer is complete ");

// Return return(0); }

//============================================================================= //= Function to receive a file using TCP = //============================================================================= //= Inputs: = //= fileName -- Name of file to create and write = //= portNum --- Port number to listen and receive on = //= maxSize --- Maximum size in bytes for written file (not implemented) = //= options --- Options (not implemented) = //=---------------------------------------------------------------------------= //= Outputs: = //= Returns -1 for fail and 0 for success = //=---------------------------------------------------------------------------= //= Side effects: = //= None known = //=---------------------------------------------------------------------------= //= Bugs: = //=---------------------------------------------------------------------------= int recvFile(char *fileName, int portNum, int maxSize, int options) {

needs to be implemented in C }

Sample output

image text in transcribed

c:\work>cl tcpFileSend.c wsock32.1ib crosoft (R) C/C++ Optimizing Compiler Version 17.00.6103e for x86 ight (C) Microsoft Corporation. All rights reserved cpFilesend.c crosoft (R) Incremental Linker Version 11.00.6103e.e right (C) Microsoft Corporation. All rights reserved out:tcpFileSend.exe cpFileSend.obj sock32.lib c: \work>cl tcpFileRecv.c wsock32.1ib crosoft (R) C/C++ Optimizing Compiler Version 17.00.6103e for x86 ight (C) Microsoft Corporation. All rights reserved tcpFileRecv.c Microsoft (R) Incremental Linker Version 11.00.61030.e right (C) Microsoft Corporation. All rights reserved out:tcpFileRecv.exe cpFileRecv.obj sock32.lib n work c: \work> :\1type recuFile.dat The system cannot find the file specified c:\1>tcpFileRecu Starting file transfer ile transfor is complete c:\1type recuFile.dat 123 TESTING 123 END WOrk c:12>type testFile.dat 123 TESTING 123 END c:12>tcpFileSend testFile.dat 127.0.0.1 1050 Starting file transfer File transfer is complete c:\2> c:\work>cl tcpFileSend.c wsock32.1ib crosoft (R) C/C++ Optimizing Compiler Version 17.00.6103e for x86 ight (C) Microsoft Corporation. All rights reserved cpFilesend.c crosoft (R) Incremental Linker Version 11.00.6103e.e right (C) Microsoft Corporation. All rights reserved out:tcpFileSend.exe cpFileSend.obj sock32.lib c: \work>cl tcpFileRecv.c wsock32.1ib crosoft (R) C/C++ Optimizing Compiler Version 17.00.6103e for x86 ight (C) Microsoft Corporation. All rights reserved tcpFileRecv.c Microsoft (R) Incremental Linker Version 11.00.61030.e right (C) Microsoft Corporation. All rights reserved out:tcpFileRecv.exe cpFileRecv.obj sock32.lib n work c: \work> :\1type recuFile.dat The system cannot find the file specified c:\1>tcpFileRecu Starting file transfer ile transfor is complete c:\1type recuFile.dat 123 TESTING 123 END WOrk c:12>type testFile.dat 123 TESTING 123 END c:12>tcpFileSend testFile.dat 127.0.0.1 1050 Starting file transfer File transfer is complete c:\2>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions