Description You will create a DropboxSugarsyncGoogleDriveMSA
Solution
server
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
FILE *fp;
int main()
{
int retv;
int i;
int counter = 0;
unsigned char *buf;
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int filesize;
fp = fopen(\"./qq.mp3\",\"rb\");
if(fp==NULL)
{
printf(\"fail to open qq.mp3 !\ \");
return 0;
}
if(fseek(fp,0,SEEK_END)==-1){
printf(\"fail to seek fp !\ \");
fclose(fp);
return 0;
}
filesize = ftell(fp);
rewind (fp);
printf(\"filesize size : %d bytes\ \", filesize);
buf = (unsigned char *) malloc(sizeof(unsigned char)*filesize);
/* Create an unnamed socket for the server. */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket. */
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(\"127.0.0.1\");
server_address.sin_port = 9734;
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
/* Create a connection queue and wait for clients. */
listen(server_sockfd, 5);
printf(\"server waiting\ \");
/* Accept a connection. */
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);
/* We can now read/write to client on client_sockfd. */
fread(buf, 1 , filesize , fp);
while((counter*4) < filesize)
{
write(client_sockfd, buf+(counter*4), sizeof(buf));
counter+=1;
}
fclose(fp);
close(server_sockfd);
close(client_sockfd);
}
client
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
FILE *fp;
int main()
{
unsigned char *buf;
buf = (unsigned char *) malloc(1024);
int sockfd,numbytes;
int len;
struct sockaddr_in address;
int result;
/* create or build a socket for the client. */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket, as concurred with the server. */
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(\"127.0.0.1\");
address.sin_port = 9734;
len = sizeof(address);
/* connect our socket to the server\'s socket. */
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1) {
perror(\"oops: client2\");
exit(1);
}
/* We can now read/write by means of sockfd. */
//Open file
if ( (fp = fopen(\"GET.mp3\", \"wb\")) == NULL){
perror(\"fopen\");
exit(1);
}
//get file from server
while(1){
if((read(sockfd, buf, sizeof(buf))) == 0) break;
fwrite(buf, 1 , sizeof(buf), fp);
}
fclose(fp);
close(sockfd);
exit(0);
}


