#include #include #include #include #include #include #include #include #include int main(int argc, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(5143); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; int skt = socket(AF_INET, SOCK_STREAM, 0); // Step 1 if(skt == -1){ perror("socket"); return 1; } if( bind(skt, (struct sockaddr*)(&sad), sizeof(struct sockaddr_in)) ){ // step 2 perror("bind"); return 1; } if( listen(skt, 5) ){ // step 3 perror("listen"); return 1; } int file_fd = open("picture.jpg", O_RDONLY); char *file_space = malloc(100000000); // 10 megs size_t file_size = read(file_fd, file_space, 10000000); /* Note: This might not be the file size if the file is bigger than 10 mb */ close(file_fd); while(1){ int client_fd; struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); // step 4 printf("Connection made from address %s\n", inet_ntoa(client_address.sin_addr)); // Send file size first write(client_fd, &file_size, 8); // Then send actual file write(client_fd, file_space, file_size); close(client_fd); } return 0; }