diff --git a/chat/Makefile b/chat/Makefile new file mode 100644 index 0000000..59f99de --- /dev/null +++ b/chat/Makefile @@ -0,0 +1,14 @@ +all: server client + +sockethelper.o: + gcc -c socketHelper.c -o sockethelper.o + +server: sockethelper.o + gcc sockethelper.o server.c -o server + +client: sockethelper.o + gcc sockethelper.o client.c -o client + + +clean: + rm -rf sockethelper.o client server diff --git a/chat/hofi1/Makefile b/chat/hofi1/Makefile new file mode 100644 index 0000000..59f99de --- /dev/null +++ b/chat/hofi1/Makefile @@ -0,0 +1,14 @@ +all: server client + +sockethelper.o: + gcc -c socketHelper.c -o sockethelper.o + +server: sockethelper.o + gcc sockethelper.o server.c -o server + +client: sockethelper.o + gcc sockethelper.o client.c -o client + + +clean: + rm -rf sockethelper.o client server diff --git a/chat/hofi1/client.c b/chat/hofi1/client.c new file mode 100644 index 0000000..319b9e0 --- /dev/null +++ b/chat/hofi1/client.c @@ -0,0 +1,66 @@ +// +// Created by joachim on 10/6/24. +// + +#include +#include +#include +#include +#include "socketHelper.h" + +int main() { + int socketFd; + struct sockaddr_in *address; + + // Get a socket descriptor + socketFd = getSocketFd(); + if (socketFd == -1) { + perror("Failed to get socket descriptor"); + return 1; + } + + // Create IPv4 address + address = createIPv4Address("127.0.0.1", 2000); + if (address == NULL) { + perror("Failed to create address"); + close(socketFd); // Close the socket on error + return 1; + } + + // Connect to the server + int result = connect(socketFd, (struct sockaddr*) address, sizeof(*address)); + if (result == -1) { + perror("Failed to connect"); + close(socketFd); + return 1; + } + + char *message = "GET \\ HTTP/1.1\r\nHost:google.com\r\n\r\n"; + + // Send the message + if (send(socketFd, message, strlen(message), 0) == -1) { + perror("Failed to send message"); + close(socketFd); + return 1; + } + + // Receive response + char buffer[4096]; // Larger buffer to handle potentially large responses + int bytesReceived = recv(socketFd, buffer, sizeof(buffer) - 1, 0); + if (bytesReceived == -1) { + perror("Failed to receive data"); + close(socketFd); + return 1; + } + + // Ensure null termination (in case the response isn't already) + buffer[bytesReceived] = '\0'; + + // Print the response + printf("Response was:\n%s", buffer); + + // Close the socket + close(socketFd); + + return 0; +} \ No newline at end of file diff --git a/chat/hofi1/server.c b/chat/hofi1/server.c new file mode 100644 index 0000000..4748515 --- /dev/null +++ b/chat/hofi1/server.c @@ -0,0 +1,46 @@ +// +// Created by joachim on 10/6/24. +// + +#include +#include +#include +#include +#include +#include "./socketHelper.h" + + #define handle_error(msg) \ + do { perror(msg); exit(EXIT_FAILURE); } while (0) + +int main() { + int serverFD = getSocketFd(); + struct sockaddr_in *srvAddress = createIPv4Address("127.0.0.1", 7585); + + // printf("Port: %d\n", srvAddress->sin_port); + + if (bind(serverFD, (struct sockaddr*)&srvAddress, sizeof(srvAddress))) { + handle_error("bind"); + } + /* if (result == 0) + printf("Socket was bound successfully\n"); + */ + + int listenResult = listen(serverFD, 10); + if (listenResult == 0) + printf("Listening was bound successfully\n"); + + struct sockaddr_in clientAddress; + uint clientAddressSize = sizeof clientAddress; + int clientSocketFD = accept(serverFD, (struct sockaddr *) &clientAddress, &clientAddressSize); + + // Receive response + char buffer[4096]; // Larger buffer to handle potentially large responses + long bytesReceived = recv(clientSocketFD, buffer, sizeof(buffer) - 1, 0); + if (bytesReceived == -1) { + perror("Failed to receive data"); + close(clientSocketFD); + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/chat/hofi1/socketHelper.c b/chat/hofi1/socketHelper.c new file mode 100644 index 0000000..7d11d3d --- /dev/null +++ b/chat/hofi1/socketHelper.c @@ -0,0 +1,26 @@ +#include "socketHelper.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +struct sockaddr_in* createIPv4Address (char *ip, int port) { + printf("Passed port: %d\n", port); + struct sockaddr_in *address = malloc(sizeof (struct sockaddr_in)); + address->sin_family = AF_INET; + address->sin_port = htons(port); + if (strlen(ip) == 0) + address->sin_addr.s_addr = INADDR_ANY; + else + address->sin_addr.s_addr = htonl(INADDR_ANY); + //inet_pton(AF_INET, ip, &address->sin_addr.s_addr); + printf("Final port: %d\n", address->sin_port); + return address; +} + +int getSocketFd() { return socket(AF_INET, SOCK_STREAM, 0); } \ No newline at end of file diff --git a/chat/hofi1/socketHelper.h b/chat/hofi1/socketHelper.h new file mode 100644 index 0000000..c85b7fc --- /dev/null +++ b/chat/hofi1/socketHelper.h @@ -0,0 +1,7 @@ +#ifndef CHAT_SOCKETHELPER_H +#define CHAT_SOCKETHELPER_H + +struct sockaddr_in* createIPv4Address (char *ip, int port); +int getSocketFd(); + +#endif //CHAT_SOCKETHELPER_H \ No newline at end of file diff --git a/chat/hofi2/Makefile b/chat/hofi2/Makefile new file mode 100644 index 0000000..59f99de --- /dev/null +++ b/chat/hofi2/Makefile @@ -0,0 +1,14 @@ +all: server client + +sockethelper.o: + gcc -c socketHelper.c -o sockethelper.o + +server: sockethelper.o + gcc sockethelper.o server.c -o server + +client: sockethelper.o + gcc sockethelper.o client.c -o client + + +clean: + rm -rf sockethelper.o client server diff --git a/chat/hofi2/client.c b/chat/hofi2/client.c new file mode 100644 index 0000000..69621e6 --- /dev/null +++ b/chat/hofi2/client.c @@ -0,0 +1,66 @@ +// +// Created by joachim on 10/6/24. +// + +#include +#include +#include +#include +#include "socketHelper.h" + +int main() { + int socketFd; + struct sockaddr_in *address; + + // Get a socket descriptor + socketFd = getSocketFd(); + if (socketFd == -1) { + perror("Failed to get socket descriptor"); + return 1; + } + + // Create IPv4 address + address = createIPv4Address("127.0.0.1", 7585); + if (address == NULL) { + perror("Failed to create address"); + close(socketFd); // Close the socket on error + return 1; + } + + // Connect to the server + int result = connect(socketFd, (struct sockaddr*) address, sizeof(*address)); + if (result == -1) { + perror("Failed to connect"); + close(socketFd); + return 1; + } + + char *message = "GET \\ HTTP/1.1\r\nHost:google.com\r\n\r\n"; + + // Send the message + if (send(socketFd, message, strlen(message), 0) == -1) { + perror("Failed to send message"); + close(socketFd); + return 1; + } + + // Receive response + char buffer[4096]; // Larger buffer to handle potentially large responses + int bytesReceived = recv(socketFd, buffer, sizeof(buffer) - 1, 0); + if (bytesReceived == -1) { + perror("Failed to receive data"); + close(socketFd); + return 1; + } + + // Ensure null termination (in case the response isn't already) + buffer[bytesReceived] = '\0'; + + // Print the response + printf("Response was:\n%s", buffer); + + // Close the socket + close(socketFd); + + return 0; +} \ No newline at end of file diff --git a/chat/hofi2/server.c b/chat/hofi2/server.c new file mode 100644 index 0000000..7f42cba --- /dev/null +++ b/chat/hofi2/server.c @@ -0,0 +1,59 @@ +// +// Created by joachim on 10/6/24. +// + +#include +#include +#include +#include +#include +#include "./socketHelper.h" +#include +#include +#include +#include +#include + + + +#define handle_error(msg) \ + do { perror(msg); exit(EXIT_FAILURE); } while (0) + +int main() { + int serverFD = socket(AF_INET, SOCK_STREAM, 0); + //struct sockaddr_in *srvAddress = createIPv4Address("127.0.0.1", 7585); + struct sockaddr_in srvAddress; + //memset(&srvAddress, '0', sizeof(srvAddress)); + + srvAddress.sin_family = AF_INET; + srvAddress.sin_addr.s_addr = htonl(INADDR_ANY); + srvAddress.sin_port = htons(7585); + + // printf("Port: %d\n", srvAddress->sin_port); + + if (bind(serverFD, (struct sockaddr*)&srvAddress, sizeof(srvAddress))) { + handle_error("bind"); + } + /* if (result == 0) + printf("Socket was bound successfully\n"); + */ + + int listenResult = listen(serverFD, 10); + if (listenResult == 0) + printf("Listening was bound successfully\n"); + + struct sockaddr_in clientAddress; + uint clientAddressSize = sizeof clientAddress; + int clientSocketFD = accept(serverFD, (struct sockaddr *) &clientAddress, &clientAddressSize); + + // Receive response + char buffer[4096]; // Larger buffer to handle potentially large responses + long bytesReceived = recv(clientSocketFD, buffer, sizeof(buffer) - 1, 0); + if (bytesReceived == -1) { + perror("Failed to receive data"); + close(clientSocketFD); + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/chat/hofi2/socketHelper.c b/chat/hofi2/socketHelper.c new file mode 100644 index 0000000..464a399 --- /dev/null +++ b/chat/hofi2/socketHelper.c @@ -0,0 +1,22 @@ +#include "socketHelper.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +struct sockaddr_in* createIPv4Address (char *ip, int port) { + printf("Passed port: %d\n", port); + struct sockaddr_in *address = malloc(sizeof (struct sockaddr_in)); + address->sin_family = AF_INET; + address->sin_port = htons(port); + address->sin_addr.s_addr = htonl(INADDR_ANY); + printf("Final port: %d\n", address->sin_port); + return address; +} + +int getSocketFd() { return socket(AF_INET, SOCK_STREAM, 0); } \ No newline at end of file diff --git a/chat/hofi2/socketHelper.h b/chat/hofi2/socketHelper.h new file mode 100644 index 0000000..c85b7fc --- /dev/null +++ b/chat/hofi2/socketHelper.h @@ -0,0 +1,7 @@ +#ifndef CHAT_SOCKETHELPER_H +#define CHAT_SOCKETHELPER_H + +struct sockaddr_in* createIPv4Address (char *ip, int port); +int getSocketFd(); + +#endif //CHAT_SOCKETHELPER_H \ No newline at end of file diff --git a/chat/mapeexample/Makefile b/chat/mapeexample/Makefile new file mode 100644 index 0000000..1ae08cf --- /dev/null +++ b/chat/mapeexample/Makefile @@ -0,0 +1,10 @@ +all: client server + +client: + gcc client.c -o client + +server: + gcc server.c -o server + +clean: + rm -f client server \ No newline at end of file diff --git a/chat/mapeexample/client.c b/chat/mapeexample/client.c new file mode 100644 index 0000000..5b7aa5e --- /dev/null +++ b/chat/mapeexample/client.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int sockfd = 0, n = 0; + char recvBuff[1024]; + struct sockaddr_in serv_addr; + + if(argc != 2) + { + printf("\n Usage: %s \n",argv[0]); + return 1; + } + + memset(recvBuff, '0',sizeof(recvBuff)); + + /* a socket is created through call to socket() function */ + if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("\n Error : Could not create socket \n"); + return 1; + } + + memset(&serv_addr, '0', sizeof(serv_addr)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(5000); + + if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) + { + printf("\n inet_pton error occured\n"); + return 1; + } + + /* Information like IP address of the remote host and its port is + * bundled up in a structure and a call to function connect() is made + * which tries to connect this socket with the socket (IP address and port) + * of the remote host + */ + if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + printf("\n Error : Connect Failed \n"); + return 1; + } + + /* Once the sockets are connected, the server sends the data (date+time) + * on clients socket through clients socket descriptor and client can read it + * through normal read call on the its socket descriptor. + */ + while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) + { + recvBuff[n] = 0; + if(fputs(recvBuff, stdout) == EOF) + { + printf("\n Error : Fputs error\n"); + } + } + + if(n < 0) + { + printf("\n Read error \n"); + } + + return 0; + close(sockfd); +} \ No newline at end of file diff --git a/chat/mapeexample/server.c b/chat/mapeexample/server.c new file mode 100644 index 0000000..071ccee --- /dev/null +++ b/chat/mapeexample/server.c @@ -0,0 +1,64 @@ +/* --- server.c --- */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int listenfd = 0, connfd = 0; + struct sockaddr_in serv_addr; + + char sendBuff[1025]; + time_t ticks; + + /* creates an UN-named socket inside the kernel and returns + * an integer known as socket descriptor + * This function takes domain/family as its first argument. + * For Internet family of IPv4 addresses we use AF_INET + */ + listenfd = socket(AF_INET, SOCK_STREAM, 0); + memset(&serv_addr, '0', sizeof(serv_addr)); + memset(sendBuff, '0', sizeof(sendBuff)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + serv_addr.sin_port = htons(5000); + + /* The call to the function "bind()" assigns the details specified + * in the structure 『serv_addr' to the socket created in the step above + */ + bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); + + /* The call to the function "listen()" with second argument as 10 specifies + * maximum number of client connections that server will queue for this listening + * socket. + */ + listen(listenfd, 10); + + while(1) + { + /* In the call to accept(), the server is put to sleep and when for an incoming + * client request, the three way TCP handshake* is complete, the function accept() + * wakes up and returns the socket descriptor representing the client socket. + */ + connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); + + /* As soon as server gets a request from client, it prepares the date and time and + * writes on the client socket through the descriptor returned by accept() + */ + ticks = time(NULL); + snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks)); + write(connfd, sendBuff, strlen(sendBuff)); + + close(connfd); + sleep(1); + } + close(listenfd); +}