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