Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<sys/socket.h>
- #include<arpa/inet.h>
- #include<unistd.h>
- #define QUEUE 1 //Queue for incoming connections
- #define BUFFLENGTH 512 //Send buffer length
- void fileops(int clientsock);
- int main(int argc , char *argv[])
- {
- int sock_fd, len, newsock;
- struct sockaddr_in server, client;
- if(argc < 2)
- {
- printf("Too few arguments. \nUsage : myftpd <port>\n");
- exit(1);
- }
- int port = atoi(argv[1]);
- sock_fd = socket(AF_INET , SOCK_STREAM , 0);
- if (sock_fd == -1)
- {
- printf("Could not create socket!\n");
- }
- //Prepare the sockaddr_in structure
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_port = htons(port);
- //Bind
- if( bind(sock_fd,(struct sockaddr *)&server , sizeof(server)) < 0)
- {
- printf("Server bind failed!\n");
- }
- listen(sock_fd , QUEUE);
- printf("Waiting for SYN\n");
- while (newsock = accept(sock_fd, (struct sockaddr *)&client, (socklen_t*)&len)){
- printf("New connection ....\n");
- fileops(newsock);
- }
- if (newsock < 0 ){
- printf("Something went wrong\n");
- }
- printf("File sent. So long, and thanks for the fish... \n");
- close(sock_fd);
- close(newsock);
- }
- void fileops(int newsock)
- {
- FILE *fp;
- char *file = "oven.f";
- char buffr[BUFFLENGTH];
- int readsize;
- fp = fopen(file, "r");
- if(fp == NULL)
- {
- printf("File 404'd \n");
- return;
- }
- bzero(buffr, BUFFLENGTH);
- while((readsize = fread(buffr, sizeof(char), BUFFLENGTH, fp)) > 0)
- {
- if((send(newsock, buffr, readsize, 0)) < 0)
- {
- printf("Send to client failed.\n");
- }
- printf("Size = %d\n", readsize);
- printf("Items = %c\n", buffr[2]);
- bzero(buffr, BUFFLENGTH);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement