Description
Objectives
1. To use the copy one binary file to another
2. To develop client/ server applications using UDP/IP socket
Part 1: Copy a binary/ text file to another file
Problem: Copy a binary/text file to another file.
Analysis:
- Input: pass file names as arguments to main, so your main function needs to be defined as follows:
int main( int argc, char * argv[])
Your files: src.dat and dest.dat files.
- File reading can be accomplished by using either:
- Functions: fopen, fwrite, and fread for binary files or fprintf and fscanf for text files
- FILE *fopen(const char *filename, const char *mode)
- fwrite( ptr, int size, int n, FILE *fp ); or fprintf() (for text files)
- fread( ptr, int size, int n, FILE *fp ); or fscanf() (for text files)
- fclose(ptr);
- Functions: fopen, fwrite, and fread for binary files or fprintf and fscanf for text files
e.g.
FILE *fp;
fp = fopen(“src.dat”,”r”);
fp = fopen(“dest.dat”,”w”);
fwrite(&buf,sizeof(buf),1,fp);
fread(&buf,sizeof(buf),1,fp);
fclose(fp);
OR
- System calls: open, read, write
- int open (const char* Path, int flags [, int mode ]);
- size_t read (int fd, void* buf, size_t cnt);
- size_t write (int fd, void* buf, size_t cnt);
e.g.:
int fd = open(“foo.txt”, O_RDWR);
int nw = write(fd, buf, strlen(buf));
int nr = read(fd, buf, 40);
close (fd);
You need to include the following libraries:
- #include<sys/types.h>
- #include<sys/stat.h>
- #include <fcntl.h>
- Write your C program to copy files (binary and text) using functions, compile, debug, run, and test
- Write your C program to copy files (binary and text) using system calls, compile, debug, run, and test
Demonstrate your programs to the TA
Part 2: Client – Server with UDP/IP[1]
The following two programs give the source code of a UDP client and UDP server (you may download the .c files from Camino).
//UDP Client
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
int main(){
//Declare socket file descriptor. All Unix I/O streams are referenced by descriptors
int sockfd;
//Declare sending buffer of size 1k bytes
char sbuf[1024];
//Declare server address
struct sockaddr_in servAddr;
//Converts domain names into numerical IP addresses via DNS
struct hostent *host;
host = (struct hostent *)gethostbyname(“localhost”); //Local host runs the server. You may use “127.0.0.1”: loopback IP address
//Open a socket, if successful, returns a descriptor associated with an endpoint
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror(“socket”);
exit(1);
}
//Set the server address to send using socket addressing structure
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(5000);
servAddr.sin_addr = *((struct in_addr *)host->h_addr);
//Client to send messages to the server continuously using UDP socket
while(1){
printf(“Client: Type a message to send to Server\n”);
scanf(“%s”, sbuf);
sendto(sockfd, sbuf, strlen(sbuf), 0, (struct sockaddr *)&servAddr, sizeof(struct sockaddr));
}
return 0;
}
// UDP Server
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(){
//Declare socket file descriptor. All Unix I/O streams are referenced by descriptors
int sockfd;
//Declare receiving buffer of size 1k bytes
char rbuf[1024];
//Declare server address to which to bind for receiving messages and client address to fill in sending address
struct sockaddr_in servAddr, clienAddr;
socklen_t addrLen = sizeof(struct sockaddr);
//Open a socket, if successful, returns a descriptor associated with an endpoint
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror(“Failure to setup an endpoint socket”);
exit(1);
}
//Setup the server address to bind using socket addressing structure
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(5000); //Port 5000 is assigned
servAddr.sin_addr.s_addr = INADDR_ANY; //Local IP address of any interface is assigned (generally one interface IP address)
//Set address/port of server endpoint for socket socket descriptor
if ((bind(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr))) < 0){
perror(“Failure to bind server address to the endpoint socket”);
exit(1);
}
//Sever continuously waits for messages from client, then prints incoming messages.
while (1){
printf(“Server waiting for messages from client: \n”);
int nr = recvfrom(sockfd, rbuf, 1024, 0, (struct sockaddr *)&clienAddr, &addrLen);
rbuf[nr] = ‘\0’;
printf(“Client with IP: %s and Port: %d sent message: %s\n”, inet_ntoa(clienAddr.sin_addr),ntohs(clienAddr.sin_port), rbuf);
}
return 0;
}
- Compile and run. Note: you may use the IP address 0.0.1 (loop back IP address) for a local host, i.e. both of your client and server run on the same machine. Demonstrate your program to the TA:
- Your client and server on your same machine
- Your client and your neighbor’s server IP address
- Modify your UDP client and UDP server programs so that your UDP client reads from a “src.dat” file used in steps 1 and 2, then sends to your neighbor’s UDP server to copy to “dest.dat” file
- [Bonus] Use the code uploaded on Camino to demonstrate TCP client-server file transfer in Step 4.
Demonstrate to the TA and upload your source code to Camino.
Requirements to complete the lab
- Show the TA correct execution of the program you wrote for Part 1 and upload source code to Camino.
- Show the TA correct execution of the program you wrote for Part 2 and upload source code to Camino.
Be sure to retain copies (machine and/or printed) of your source code. You will want these for study purposes and to resolve any grading questions (should they arise)
Please start each program with a descriptive block that includes minimally the following information:
/*
* Name: <your name>
* Date:
* Title: Lab3 – Part ….
* Description: This program … <you should
* complete an appropriate description here.>
*/
[1] http://beej.us/guide/bgnet/