This commit is contained in:
2025-11-03 03:43:07 +00:00
parent 95d1e62323
commit a2ec6f8841
11 changed files with 408 additions and 411 deletions
+22 -22
View File
@@ -1,22 +1,22 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the // For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu // README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{ {
"name": "Ubuntu", "name": "Ubuntu",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:noble" "image": "mcr.microsoft.com/devcontainers/base:noble"
// Features to add to the dev container. More info: https://containers.dev/features. // Features to add to the dev container. More info: https://containers.dev/features.
// "features": {}, // "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally. // Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [], // "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created. // Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a", // "postCreateCommand": "uname -a",
// Configure tool-specific properties. // Configure tool-specific properties.
// "customizations": {}, // "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root" // "remoteUser": "root"
} }
+53 -55
View File
@@ -1,56 +1,54 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> //strlen #include <string.h> //strlen
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> //inet_addr #include <arpa/inet.h> //inet_addr
#include <unistd.h> //write #include <unistd.h> //write
#include <netdb.h> #include <netdb.h>
#define EXPECTED_CHECKSUM "9f1b7e4717bffb60d6d6c39b427932e1" struct sockaddr_in sa;
int main()
struct sockaddr_in sa; {
int main() int chunkSize;
{ int fileSize;
int chunkSize;
int fileSize; int my_sock = socket(AF_INET, SOCK_STREAM, 0);
sa.sin_family = AF_INET;
int my_sock = socket(AF_INET, SOCK_STREAM, 0); sa.sin_port = htons(10059);
sa.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr));
sa.sin_port = htons(10059); int x = connect(my_sock, (struct sockaddr *)&sa, sizeof(sa));
inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr));
int x = connect(my_sock, (struct sockaddr *)&sa, sizeof(sa)); // Request File Size
perror("Connect"); printf("\nRequesting File Size\n");
send(my_sock, "FS", 2, 0);
// Request File Size char size_buff[13];
printf("\nRequesting File Size\n"); int recv_size = recv(my_sock, size_buff, 13, 0);
send(my_sock, "FS", 2, 0); fileSize = atoi(strtok(size_buff, "|"));
char size_buff[13]; chunkSize = atoi(strtok(NULL, "|"));
int recv_size = recv(my_sock, size_buff, 13, 0);
fileSize = atoi(strtok(size_buff, "|")); // Server Transfer Settings
chunkSize = atoi(strtok(NULL, "|")); printf("File Size: %d\n", fileSize);
printf("Chunk Size: %d\n", chunkSize);
// Server Transfer Settings
printf("File Size: %d\n", fileSize); // Request File
printf("Chunk Size: %d\n", chunkSize); printf("\nRequesting File Transfer\n");
send(my_sock, "RX", 2, 0);
// Request File
printf("\nRequesting File Transfer\n"); int total_bytes_received = 0;
send(my_sock, "RX", 2, 0); FILE *fp = fopen("rx.txt", "w");
char file_buffer[chunkSize];
int total_bytes_received = 0; while(total_bytes_received < fileSize) {
if (access("rx.txt", F_OK) == 0) {remove("rx.txt");} int bytes_received = recv(my_sock, file_buffer, chunkSize, 0);
FILE *fp = fopen("rx.txt", "a"); if (bytes_received <= 0) {break;}
while(total_bytes_received < fileSize) { total_bytes_received += bytes_received;
char file_buffer[chunkSize]; fwrite(file_buffer, 1, bytes_received, fp);
int bytes_received = recv(my_sock, file_buffer, chunkSize, 0); printf("Received %d bytes, Total: %d bytes\n", bytes_received, total_bytes_received);
if (bytes_received <= 0) {break;} }
total_bytes_received += bytes_received; // Calculate MD5 checksum using system command
fwrite(file_buffer, 1, bytes_received, fp); printf("\nCalculating MD5 checksum...\n");
printf("Received %d bytes, Total: %d bytes\n", bytes_received, total_bytes_received); printf("Expected Checksum:\n");
} // system("md5sum bits.txt | awk '{ print $1 }'");
// Calculate MD5 checksum using system command printf("Received File Checksum: \n");
printf("\nCalculating MD5 checksum...\n"); // system("md5sum rx.txt | awk '{ print $1 }'");
printf("Expected Checksum:\n%s\nReceived File Checksum: \n", EXPECTED_CHECKSUM); fclose(fp);
system("md5sum rx.txt | awk '{ print $1 }'");
fclose(fp);
} }
+94 -96
View File
@@ -1,96 +1,94 @@
#include <stdio.h>
#include <string.h>
#include <stdio.h> #include <sys/socket.h>
#include <string.h> #include <arpa/inet.h>
#include <sys/socket.h> #include <unistd.h>
#include <arpa/inet.h> #include <netdb.h>
#include <unistd.h> #include <errno.h>
#include <netdb.h> #include <signal.h>
#include <errno.h>
#include <signal.h> #define CHUNK_SIZE 2048
#define CHUNK_SIZE 8192 int send_size, recv_size;
int main(int argc, char *argv[])
int send_size, recv_size; {
int main(int argc, char *argv[]) int socket_desc, client_sock, c, read_size; // sockets and such
{ struct sockaddr_in server, client; // one for listening one for connection with client(s)
int socket_desc, client_sock, c, read_size; // sockets and such char client_message[20000];
struct sockaddr_in server, client; // one for listening one for connection with client(s) socket_desc = socket(AF_INET, SOCK_STREAM, 0);
char client_message[20000]; client_sock = socket(AF_INET, SOCK_STREAM, 0);
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
client_sock = socket(AF_INET, SOCK_STREAM, 0); printf("Sockets created\n");
printf("Sockets created\n"); server.sin_family = AF_INET;
server.sin_port = htons(10059);
server.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &(server.sin_addr));
server.sin_port = htons(10059);
inet_pton(AF_INET, "127.0.0.1", &(server.sin_addr)); if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) perror("bind failed. Error"); // perror is a very helpful function for tracking down errors
{ return 1;
perror("bind failed. Error"); // perror is a very helpful function for tracking down errors }
return 1; printf("bind completed \n");
}
printf("bind completed \n"); c = sizeof(struct sockaddr_in);
c = sizeof(struct sockaddr_in); listen(socket_desc, 1);
listen(socket_desc, 1); printf("Waiting for incoming connections...\n");
printf("Waiting for incoming connections...\n"); // Start waiting for any connections inbound
while (1)
// Start waiting for any connections inbound {
while (1) client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
{ if (client_sock < 0)
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c); {
if (client_sock < 0) perror("Accept Failed");
{ }
perror("Accept Failed"); printf("\nClient Connected\n");
} while (1)
printf("\nClient Connected\n"); {
while (1) recv_size = recv(client_sock, client_message, 2, 0);
{ long fileSize;
recv_size = recv(client_sock, client_message, 2, 0); if (recv_size == 0)
long fileSize; {
if (recv_size == 0) printf("Client disconnected\n");
{ break;
printf("Client disconnected\n"); }
break; client_message[recv_size] = '\0';
} printf("Received %d bytes. Msg is %s \n", recv_size, client_message);
client_message[recv_size] = '\0'; // Determine type of message
printf("Received %d bytes. Msg is %s \n", recv_size, client_message); if (strncmp(client_message, "FS", 2) == 0)
// Determine type of message {
if (strncmp(client_message, "FS", 2) == 0) // Send size of the file
{ FILE *fp = fopen("bits.txt", "rb");
// Send size of the file fseek(fp, 0, SEEK_END);
FILE *fp = fopen("bits.txt", "rb"); fileSize = ftell(fp);
fseek(fp, 0, SEEK_END); fclose(fp);
fileSize = ftell(fp); sprintf(client_message, "%ld|%d", fileSize, CHUNK_SIZE);
fclose(fp); send_size = send(client_sock, client_message, strlen(client_message), 0);
sprintf(client_message, "%ld|%d", fileSize, CHUNK_SIZE); if (send_size < 0)
send_size = send(client_sock, client_message, strlen(client_message), 0); perror("send failed");
if (send_size < 0) }
perror("send failed"); else if (strncmp(client_message, "RX", 2) == 0)
} {
else if (strncmp(client_message, "RX", 2) == 0) // Client ready to receive file
{ FILE *fp = fopen("bits.txt", "rb");
// Client ready to receive file char buffer[CHUNK_SIZE];
FILE *fp = fopen("bits.txt", "rb"); for(int i = 0; i < fileSize; i += CHUNK_SIZE) {
for(int i = 0; i < fileSize; i += CHUNK_SIZE) { size_t bytesRead = fread(buffer, 1, CHUNK_SIZE, fp);
char buffer[CHUNK_SIZE]; if (bytesRead > 0) {
size_t bytesRead = fread(buffer, 1, CHUNK_SIZE, fp); send_size = send(client_sock, buffer, bytesRead, 0);
if (bytesRead > 0) { }
send_size = send(client_sock, buffer, bytesRead, 0); }
} }
} else
} {
else printf("Unknown message type\n");
{ }
printf("Unknown message type\n"); }
} }
} close(socket_desc);
} close(client_sock);
close(socket_desc); return 0;
close(client_sock); }
return 0;
}
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
+55 -55
View File
@@ -1,56 +1,56 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> //strlen #include <string.h> //strlen
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> //inet_addr #include <arpa/inet.h> //inet_addr
#include <unistd.h> //write #include <unistd.h> //write
#include <netdb.h> #include <netdb.h>
#define EXPECTED_CHECKSUM "9f1b7e4717bffb60d6d6c39b427932e1" #define EXPECTED_CHECKSUM "9f1b7e4717bffb60d6d6c39b427932e1"
struct sockaddr_in sa; struct sockaddr_in sa;
int main() int main()
{ {
int chunkSize; int chunkSize;
int fileSize; int fileSize;
int my_sock = socket(AF_INET, SOCK_STREAM, 0); int my_sock = socket(AF_INET, SOCK_STREAM, 0);
sa.sin_family = AF_INET; sa.sin_family = AF_INET;
sa.sin_port = htons(10059); sa.sin_port = htons(10059);
inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr)); inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr));
int x = connect(my_sock, (struct sockaddr *)&sa, sizeof(sa)); int x = connect(my_sock, (struct sockaddr *)&sa, sizeof(sa));
perror("Connect"); perror("Connect");
// Request File Size // Request File Size
printf("\nRequesting File Size\n"); printf("\nRequesting File Size\n");
send(my_sock, "FS", 2, 0); send(my_sock, "FS", 2, 0);
char size_buff[13]; char size_buff[13];
int recv_size = recv(my_sock, size_buff, 13, 0); int recv_size = recv(my_sock, size_buff, 13, 0);
fileSize = atoi(strtok(size_buff, "|")); fileSize = atoi(strtok(size_buff, "|"));
chunkSize = atoi(strtok(NULL, "|")); chunkSize = atoi(strtok(NULL, "|"));
// Server Transfer Settings // Server Transfer Settings
printf("File Size: %d\n", fileSize); printf("File Size: %d\n", fileSize);
printf("Chunk Size: %d\n", chunkSize); printf("Chunk Size: %d\n", chunkSize);
// Request File // Request File
printf("\nRequesting File Transfer\n"); printf("\nRequesting File Transfer\n");
send(my_sock, "RX", 2, 0); send(my_sock, "RX", 2, 0);
int total_bytes_received = 0; int total_bytes_received = 0;
if (access("rx.txt", F_OK) == 0) {remove("rx.txt");} if (access("rx.txt", F_OK) == 0) {remove("rx.txt");}
FILE *fp = fopen("rx.txt", "a"); FILE *fp = fopen("rx.txt", "a");
while(total_bytes_received < fileSize) { while(total_bytes_received < fileSize) {
char file_buffer[chunkSize]; char file_buffer[chunkSize];
int bytes_received = recv(my_sock, file_buffer, chunkSize, 0); int bytes_received = recv(my_sock, file_buffer, chunkSize, 0);
if (bytes_received <= 0) {break;} if (bytes_received <= 0) {break;}
total_bytes_received += bytes_received; total_bytes_received += bytes_received;
fwrite(file_buffer, 1, bytes_received, fp); fwrite(file_buffer, 1, bytes_received, fp);
printf("Received %d bytes, Total: %d bytes\n", bytes_received, total_bytes_received); printf("Received %d bytes, Total: %d bytes\n", bytes_received, total_bytes_received);
} }
// Calculate MD5 checksum using system command // Calculate MD5 checksum using system command
printf("\nCalculating MD5 checksum...\n"); printf("\nCalculating MD5 checksum...\n");
printf("Expected Checksum:\n%s\nReceived File Checksum: \n", EXPECTED_CHECKSUM); printf("Expected Checksum:\n%s\nReceived File Checksum: \n", EXPECTED_CHECKSUM);
system("md5sum rx.txt | awk '{ print $1 }'"); system("md5sum rx.txt | awk '{ print $1 }'");
fclose(fp); fclose(fp);
} }
+7 -7
View File
@@ -1,7 +1,7 @@
## cos440-hw3 ## ## cos440-hw3 ##
[![](https://gitea-actions.nicholaspease.com/actions/npease/cos440-hw3/badge?label=build&style=flat&branch=main)](https://gitea-actions.nicholaspease.com/latest-log?branch=main) [![](https://gitea-actions.nicholaspease.com/actions/npease/cos440-hw3/badge?label=build&style=flat&branch=main)](https://gitea-actions.nicholaspease.com/latest-log?branch=main)
[![](https://drone.nicholaspease.com/api/badges/npease/cos440-hw3/status.svg)](https://drone.nicholaspease.com/npease/cos440-hw3) [![](https://drone.nicholaspease.com/api/badges/npease/cos440-hw3/status.svg)](https://drone.nicholaspease.com/npease/cos440-hw3)
[![](https://wakaapi.nicholaspease.com/api/badge/LAX18/interval:any/project:cos440-hw3)](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos440-hw3) [![](https://wakaapi.nicholaspease.com/api/badge/LAX18/interval:any/project:cos440-hw3)](https://wakaapi.nicholaspease.com/summary?interval=any&project=cos440-hw3)
![](https://server1.nicholaspease.com/badges/cloc/npease/cos440-hw3.svg) ![](https://server1.nicholaspease.com/badges/cloc/npease/cos440-hw3.svg)
<hr> <hr>
+86 -86
View File
@@ -1,86 +1,86 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <netdb.h> #include <netdb.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#define CHUNK_SIZE 8192 #define CHUNK_SIZE 8192
int send_size, recv_size; int send_size, recv_size;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int socket_desc, client_sock, c, read_size; // sockets and such int socket_desc, client_sock, c, read_size; // sockets and such
struct sockaddr_in server, client; // one for listening one for connection with client(s) struct sockaddr_in server, client; // one for listening one for connection with client(s)
char client_message[CHUNK_SIZE]; // Maximum size of the message buffer char client_message[CHUNK_SIZE]; // Maximum size of the message buffer
socket_desc = socket(AF_INET, SOCK_STREAM, 0); socket_desc = socket(AF_INET, SOCK_STREAM, 0);
client_sock = socket(AF_INET, SOCK_STREAM, 0); client_sock = socket(AF_INET, SOCK_STREAM, 0);
printf("Sockets Created\n"); printf("Sockets Created\n");
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons(10059); server.sin_port = htons(10059);
inet_pton(AF_INET, "127.0.0.1", &(server.sin_addr)); inet_pton(AF_INET, "127.0.0.1", &(server.sin_addr));
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{ {
perror("Bind Failed"); // perror is a very helpful function for tracking down errors perror("Bind Failed"); // perror is a very helpful function for tracking down errors
return 1; return 1;
} }
printf("Bind completed \n"); printf("Bind completed \n");
c = sizeof(struct sockaddr_in); c = sizeof(struct sockaddr_in);
listen(socket_desc, 1); listen(socket_desc, 1);
printf("Waiting for incoming connections...\n"); printf("Waiting for incoming connections...\n");
// Start waiting for any connections inbound // Start waiting for any connections inbound
while (1) while (1)
{ {
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c); client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
printf("\nClient Connected\n"); printf("\nClient Connected\n");
while (1) while (1)
{ {
recv_size = recv(client_sock, client_message, 2, 0); recv_size = recv(client_sock, client_message, 2, 0);
long fileSize; long fileSize;
if (recv_size == 0) if (recv_size == 0)
{ {
printf("Client disconnected\n"); printf("Client disconnected\n");
break; break;
} }
client_message[recv_size] = '\0'; client_message[recv_size] = '\0';
printf("Received %d bytes. Msg is %s \n", recv_size, client_message); printf("Received %d bytes. Msg is %s \n", recv_size, client_message);
// Determine type of message // Determine type of message
if (strncmp(client_message, "FS", 2) == 0) if (strncmp(client_message, "FS", 2) == 0)
{ {
// Send size of the file // Send size of the file
FILE *fp = fopen("bits.txt", "rb"); FILE *fp = fopen("bits.txt", "rb");
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
fileSize = ftell(fp); fileSize = ftell(fp);
fclose(fp); fclose(fp);
sprintf(client_message, "%ld|%d", fileSize, CHUNK_SIZE); sprintf(client_message, "%ld|%d", fileSize, CHUNK_SIZE);
send_size = send(client_sock, client_message, strlen(client_message), 0); send_size = send(client_sock, client_message, strlen(client_message), 0);
} }
else if (strncmp(client_message, "RX", 2) == 0) else if (strncmp(client_message, "RX", 2) == 0)
{ {
// Client ready to receive file // Client ready to receive file
FILE *fp = fopen("bits.txt", "rb"); FILE *fp = fopen("bits.txt", "rb");
for(int i = 0; i < fileSize; i += CHUNK_SIZE) { for(int i = 0; i < fileSize; i += CHUNK_SIZE) {
char buffer[CHUNK_SIZE]; char buffer[CHUNK_SIZE];
size_t bytesRead = fread(buffer, 1, CHUNK_SIZE, fp); size_t bytesRead = fread(buffer, 1, CHUNK_SIZE, fp);
if (bytesRead > 0) { if (bytesRead > 0) {
send_size = send(client_sock, buffer, bytesRead, 0); send_size = send(client_sock, buffer, bytesRead, 0);
} }
} }
} }
} }
} }
close(socket_desc); close(socket_desc);
close(client_sock); close(client_sock);
return 0; return 0;
} }
+27 -27
View File
@@ -1,28 +1,28 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include<string.h> //strlen #include<string.h> //strlen
#include<sys/socket.h> #include<sys/socket.h>
#include<arpa/inet.h> //inet_addr #include<arpa/inet.h> //inet_addr
#include<unistd.h> //write #include<unistd.h> //write
#include<netdb.h> #include<netdb.h>
struct sockaddr_in sa ; struct sockaddr_in sa ;
int main() int main()
{ int err; { int err;
char my_buf[1024] ; char my_buf[1024] ;
char *msg = "I am your main socket squeeze" ; char *msg = "I am your main socket squeeze" ;
int my_sock = socket(AF_INET, SOCK_STREAM, 0) ; int my_sock = socket(AF_INET, SOCK_STREAM, 0) ;
sa.sin_family = AF_INET ; sa.sin_family = AF_INET ;
sa.sin_port = htons(10059) ; sa.sin_port = htons(10059) ;
inet_pton(AF_INET, "130.111.216.48" , &(sa.sin_addr)) ; inet_pton(AF_INET, "130.111.216.48" , &(sa.sin_addr)) ;
printf("BEFOrE CONNECT\n") ; printf("BEFOrE CONNECT\n") ;
int x = connect(my_sock, (struct sockaddr *) &sa, sizeof(sa)) ; int x = connect(my_sock, (struct sockaddr *) &sa, sizeof(sa)) ;
perror("Connect") ; perror("Connect") ;
printf ("Sending Message to Server!\n") ; printf ("Sending Message to Server!\n") ;
send (my_sock, msg, 32, 0) ; send (my_sock, msg, 32, 0) ;
printf ("Receiving Message from Server!\n") ; printf ("Receiving Message from Server!\n") ;
int recv_size = recv(my_sock , my_buf , 32 , 0) ; int recv_size = recv(my_sock , my_buf , 32 , 0) ;
printf("Here is the message: \n%s\n", my_buf) ; printf("Here is the message: \n%s\n", my_buf) ;
} }
+63 -63
View File
@@ -1,63 +1,63 @@
#include<stdio.h> #include<stdio.h>
#include<string.h> #include<string.h>
#include<sys/socket.h> #include<sys/socket.h>
#include<arpa/inet.h> #include<arpa/inet.h>
#include<unistd.h> #include<unistd.h>
#include<netdb.h> #include<netdb.h>
#include <errno.h> #include <errno.h>
int send_size, recv_size ; int send_size, recv_size ;
int main(int argc , char *argv[]) int main(int argc , char *argv[])
{ {
int socket_desc , client_sock , c , read_size; //sockets and such int socket_desc , client_sock , c , read_size; //sockets and such
struct sockaddr_in server , client ; //one for listening one for connection with client(s) struct sockaddr_in server , client ; //one for listening one for connection with client(s)
char client_message[20000]; char client_message[20000];
socket_desc = socket(AF_INET , SOCK_STREAM , 0); socket_desc = socket(AF_INET , SOCK_STREAM , 0);
client_sock = socket(AF_INET , SOCK_STREAM , 0); client_sock = socket(AF_INET , SOCK_STREAM , 0);
printf("Sockets created\n"); printf("Sockets created\n");
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons( 10059 ); server.sin_port = htons( 10059 );
inet_pton (AF_INET, "130.111.216.48", &(server.sin_addr)) ; inet_pton (AF_INET, "130.111.216.48", &(server.sin_addr)) ;
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{ {
perror("bind failed. Error"); //perror is a very helpful function for tracking down errors perror("bind failed. Error"); //perror is a very helpful function for tracking down errors
return 1; return 1;
} }
printf("bind completed \n"); printf("bind completed \n");
c = sizeof(struct sockaddr_in); c = sizeof(struct sockaddr_in);
listen(socket_desc , 1); listen(socket_desc , 1);
printf("Waiting for incoming connections...\n"); printf("Waiting for incoming connections...\n");
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
//if successful, client is filled in with address of connecting socket //if successful, client is filled in with address of connecting socket
if (client_sock < 0) if (client_sock < 0)
{ {
perror("accept failed"); perror("accept failed");
return 1; return 1;
} }
printf("Connection accepted\n"); printf("Connection accepted\n");
recv_size = recv(client_sock , client_message , 32 , 0) ; recv_size = recv(client_sock , client_message , 32 , 0) ;
printf("Received %d bytes. Msg is\n %s \n", printf("Received %d bytes. Msg is\n %s \n",
recv_size, client_message ) ; recv_size, client_message ) ;
sprintf(client_message, "HOW NOW BROWN COW?\n") ; //Other messages could be used as well. sprintf(client_message, "HOW NOW BROWN COW?\n") ; //Other messages could be used as well.
send_size = send(client_sock , client_message , 32,0); send_size = send(client_sock , client_message , 32,0);
printf("sent %d bytes\n", send_size) ; printf("sent %d bytes\n", send_size) ;
fflush(stdout) ; fflush(stdout) ;
close(socket_desc) ; close(socket_desc) ;
close(client_sock) ; close(client_sock) ;
return 0; return 0;
} }