Question
C-based Network Programming for two functions **There should be no changes needed to main().** Will rate and thanks. You are to write a client and
C-based Network Programming for two functions **There should be no changes needed to main().** Will rate and thanks.
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(). Also, submit a screenshot of program build and program execution that should look very similar to what is in Appendix A.
tcpFileRecv_template.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
//----- 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){ }
tcpFileSend_template.c
//========================================= 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
//----- 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){ }
Appendix A - Screenshots for tcpFileSend.c and tcpFileRecv.c VC++ cmd c:\work>cl tcpFileSend.c wsock32.1ib Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x86 Copyright (C) Microsoft Corporation. All rights reserved. tcpFileSend.c Microsoft (R) Incremental Linker Version 11.00.61030.e Copyright (C) Microsoft Corporation. All rights reserved. /out:tcpFileSend.exe tcpFileSend.obj wsock32.lib c:\work>cl tcpFileRecv.c wsock32.1ib Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x86 Copyright (C) Microsoft Corporation. All rights reserved tcpFileRecv.c Microsoft (R) Incremental Linker Version 11.00.61030.e Copyright (C) Microsoft Corporation. All rights reserved /out:tcpFileRecv.exe tcpFileRecv.obj wsock32.lib C work c:\work> c:\1type rec File. dat The system cannot find the file specified c: \1 tcpFileRecu Starting file transfer.. . File transfer is complete c:\1type recuFile. dat 123 TESTING 123 END C Work c:\2)type tes t Fi le . dat 123 TESTING 123 END c:\2tcpFi lesend testFile, dat 127. O . O 1 1050 Starting file transfer. . . File transfer is complete c:12> Appendix A - Screenshots for tcpFileSend.c and tcpFileRecv.c VC++ cmd c:\work>cl tcpFileSend.c wsock32.1ib Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x86 Copyright (C) Microsoft Corporation. All rights reserved. tcpFileSend.c Microsoft (R) Incremental Linker Version 11.00.61030.e Copyright (C) Microsoft Corporation. All rights reserved. /out:tcpFileSend.exe tcpFileSend.obj wsock32.lib c:\work>cl tcpFileRecv.c wsock32.1ib Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x86 Copyright (C) Microsoft Corporation. All rights reserved tcpFileRecv.c Microsoft (R) Incremental Linker Version 11.00.61030.e Copyright (C) Microsoft Corporation. All rights reserved /out:tcpFileRecv.exe tcpFileRecv.obj wsock32.lib C work c:\work> c:\1type rec File. dat The system cannot find the file specified c: \1 tcpFileRecu Starting file transfer.. . File transfer is complete c:\1type recuFile. dat 123 TESTING 123 END C Work c:\2)type tes t Fi le . dat 123 TESTING 123 END c:\2tcpFi lesend testFile, dat 127. O . O 1 1050 Starting file transfer. . . File transfer is complete c:12>Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started