Upon completion of this short, you will be able to:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 2023
int main(int argc, char *argv[]) {
int sock;
struct sockaddr_in server;
char message[1000], server_reply[2000], file_contents[10000];
FILE *fp;
if (argc != 2) {
("Usage: %s <file name>\n", argv[0]);
printfreturn 1;
}
// Open the file
= fopen(argv[1], "r");
fp if (fp == NULL) {
("Error opening file");
perrorreturn 1;
}
// Read file contents
(file_contents, sizeof(char), sizeof(file_contents), fp);
fread(fp);
fclose
// Create socket
= socket(AF_INET, SOCK_STREAM, 0);
sock if (sock == -1) {
("Could not create socket");
printf}
// Prepare the sockaddr_in structure
.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server
// Connect to remote server
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
("connect failed. Error");
perrorreturn 1;
}
("Connected\n");
printf
// Send file contents to the server
if (send(sock, file_contents, strlen(file_contents), 0) < 0) {
("send failed");
printfreturn 1;
}
// Receive message from the server
if (recv(sock, server_reply, 2000, 0) < 0) {
("recv failed");
printfreturn 1;
}
("Server reply: %s\n", server_reply);
printf
(sock);
closereturn 0;
}
None collected yet. Let us know.