@@ -0,0 +1,66 @@ | |||
#include <stdio.h> | |||
#include <string.h> | |||
#include <stdlib.h> | |||
#include <time.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
#include <netdb.h> | |||
#include <arpa/inet.h> | |||
#include <unistd.h> | |||
#define SERVER_IP_ADDR "127.0.0.1" | |||
#define SERVER_PORT "2000" | |||
#define SECRET_NUMBER 42 | |||
int main() { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
struct sockaddr_storage client_addr; | |||
socklen_t addrlen = sizeof client_addr; | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_DGRAM; | |||
if ((status = getaddrinfo(SERVER_IP_ADDR, SERVER_PORT, &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); | |||
int l = 1, r = SECRET_NUMBER+1; | |||
while (l < r) { | |||
int m = l + (r - l) / 2; | |||
char str[32]; | |||
sprintf(str, "%d", m); | |||
sendto(s, str, 32, 0, servinfo->ai_addr, servinfo->ai_addrlen); | |||
char res[32]; | |||
memset(res, 0, 32); | |||
if (recvfrom(s, res, 32, 0, | |||
(struct sockaddr *)&client_addr, &addrlen) < 0) { | |||
printf("Failed to receive a message from client\n"); | |||
} | |||
printf("res: %s\n", res); | |||
if (strcmp(res, "LESS\0") == 0) | |||
l = m; | |||
else if (strcmp(res, "MORE\0") == 0) | |||
r = m; | |||
else if (strcmp(res, "WIN\0") == 0) { | |||
close(s); | |||
freeaddrinfo(servinfo); | |||
printf("Secret Number: %s\n", str); | |||
exit(0); | |||
} else { | |||
fprintf(stderr, "unexpected error: %s\n", res); | |||
exit(1); | |||
} | |||
} | |||
close(s); | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1,107 @@ | |||
#include <stdio.h> | |||
#include <string.h> | |||
#include <stdlib.h> | |||
#include <time.h> | |||
#include <sys/socket.h> | |||
#include <arpa/inet.h> | |||
#include <unistd.h> | |||
#define SERVER_IP_ADDR "127.0.0.1" | |||
#define SERVER_PORT 2000 | |||
#define SECRET_NUMBER 42 | |||
struct sockaddr_in server_addr, client_addr; | |||
int socket_desc, client_struct_length = sizeof(client_addr); | |||
char client_message[2000]; | |||
// Function to send a message to the client | |||
int send_message(char *message) | |||
{ | |||
if (sendto(socket_desc, message, strlen(message), 0, | |||
(struct sockaddr *)&client_addr, client_struct_length) < 0) | |||
{ | |||
printf("Failed to send a message to client\n"); | |||
return -1; | |||
} | |||
return 0; | |||
} | |||
int main(void) | |||
{ | |||
// Create UDP socket | |||
socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |||
if (socket_desc < 0) | |||
{ | |||
printf("Failed to create a socket\n"); | |||
return -1; | |||
} | |||
// Set IP and Port | |||
server_addr.sin_family = AF_INET; | |||
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); | |||
server_addr.sin_port = htons(SERVER_PORT); | |||
// Bind the socket to IP:Port | |||
if (bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) | |||
{ | |||
printf("Failed to bind the socket\n"); | |||
return -1; | |||
} | |||
printf("Server is listening on %s:%d\n", SERVER_IP_ADDR, SERVER_PORT); | |||
// Generate a random number between 1 and 42 | |||
srand(time(NULL)); | |||
int target = rand() % SECRET_NUMBER + 1; | |||
for (int i = 0; i < SECRET_NUMBER; i++) | |||
{ | |||
// Receive a message from client | |||
memset(client_message, '\0', sizeof(client_message)); | |||
if (recvfrom(socket_desc, client_message, sizeof(client_message), 0, | |||
(struct sockaddr *)&client_addr, &client_struct_length) < 0) | |||
{ | |||
printf("Failed to receive a message from client\n"); | |||
return -1; | |||
} | |||
// Print the received message | |||
printf("%s:%i: %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_message); | |||
// If message is not a number b/w 1 and SECRET_NUMBER send an error and continue | |||
int n = atoi(client_message); | |||
if (n <= 0 || n > SECRET_NUMBER) | |||
{ | |||
if (send_message("ERROR\0")) | |||
return -1; | |||
continue; | |||
} | |||
// If a correct number was guessed. Send WIN and terminate. | |||
if (n == target) | |||
{ | |||
if (send_message("WIN\0")) | |||
return -1; | |||
close(socket_desc); | |||
return 0; | |||
} | |||
// If a wrong number was guessed, send a hint | |||
if (n < target) { | |||
printf("LESS %d %d\n", n, target); | |||
} else if (n > target){ | |||
printf("MORE %d %d\n", n, target); | |||
} else { | |||
printf("EQUAL %d %d\n", n, target); | |||
} | |||
if (send_message(n < target ? "LESS\0" : "MORE\0")) | |||
return -1; | |||
} | |||
if (send_message("LOSE\0")) | |||
return -1; | |||
close(socket_desc); | |||
return 0; | |||
} |
@@ -0,0 +1,35 @@ | |||
#include <iostream> | |||
void printIp(const std::string &name, uint32_t ip) { | |||
std::cout << name << ": " << (ip >> 24) << '.' | |||
<< ((ip << 8) >> 24) << '.' | |||
<< ((ip << 16) >> 24) << '.' | |||
<< ((ip << 24) >> 24) << '\n'; | |||
} | |||
int main() { | |||
uint32_t a, b, c, d, cidr; | |||
char temp; | |||
std::cin >> a >> temp >> b >> temp >> c >> temp >> d >> temp >> cidr; | |||
uint32_t ip, mask = 0; | |||
ip = (a << 24) + (b << 16) + (c << 8) + d; | |||
for (uint32_t i = 0; i < cidr; ++i) | |||
mask += (1u << (31u-i)); | |||
uint32_t neta = ip & mask, broadcast = neta; | |||
for (uint32_t i = 0; i < 32u-cidr ; ++i) { | |||
broadcast += (1 << i); | |||
} | |||
uint32_t minhost= neta+1, maxhost = broadcast-1; | |||
printIp("IP ADDRESS", ip); | |||
printIp("NETWORK ADDRESS", neta); | |||
printIp("BROADCAST ADDRESS", broadcast); | |||
printIp("SUBNET MASK", mask); | |||
printIp("MIN HOST ADDRESS", minhost); | |||
printIp("MAX HOST ADDRESS", maxhost); | |||
std::cout << "NUMBER OF HOSTS: " << (maxhost-minhost+1) << '\n'; | |||
} |
@@ -0,0 +1 @@ | |||
*.out |
@@ -0,0 +1,11 @@ | |||
CC = gcc | |||
CFLAGS = -O0 -lssl -lcrypto | |||
SRC = *.c | |||
all: $(patsubst %.c, %.out, $(wildcard *.c)) | |||
%.out: %.c Makefile | |||
$(CC) $(CFLAGS) $< -o $@ | |||
clean: | |||
rm *.out |
@@ -0,0 +1,58 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
// TODO: fix over buf overflow | |||
void fillHttpReq(char *buf, char *host, size_t size) { | |||
char *req = "GET / HTTP/1.1\r\nHost: \0"; | |||
memcpy(buf, req, strlen(req)); | |||
memcpy(buf + strlen(buf), host, strlen(host)); | |||
memcpy(buf + strlen(buf), "\r\n\r\n", strlen("\r\n\r\n")); | |||
} | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
if (argc < 2) { | |||
fprintf(stderr, "Usage: %s <hostname> <port (optional)>", argv[0]); | |||
exit(1); | |||
} | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
if ((status = getaddrinfo(argv[1], (argc > 2 ? argv[2] : "80"), &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s = socket(servinfo->ai_family, servinfo->ai_socktype, | |||
servinfo->ai_protocol); | |||
printf("%d\n", s); | |||
int connection; | |||
if ((connection = connect(s, servinfo->ai_addr, servinfo->ai_addrlen)) == | |||
-1) { | |||
fprintf(stderr, "connection error: %d\n", connection); | |||
exit(1); | |||
} | |||
char buf[10000]; | |||
fillHttpReq(buf, argv[1], 10000); | |||
printf("%s\n", buf); | |||
// TODO: send throw a loop | |||
send(s, buf, 10000, 0); | |||
memset(buf, 0, 10000); | |||
recv(s, buf, 10000, 0); | |||
printf("%s\n", buf); | |||
close(s); | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1,73 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <openssl/ssl.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
#include <unistd.h> | |||
// TODO: fix over buf overflow | |||
void fillHttpReq(char *buf, char *host, size_t size) { | |||
char *req = "GET / HTTP/1.1\r\nHost: \0"; | |||
memcpy(buf, req, strlen(req)); | |||
memcpy(buf + strlen(buf), host, strlen(host)); | |||
memcpy(buf + strlen(buf), "\r\n\r\n", strlen("\r\n\r\n")); | |||
} | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
if (argc != 2) { | |||
fprintf(stderr, "Usage: %s <hostname>", argv[0]); | |||
exit(1); | |||
} | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
if ((status = getaddrinfo(argv[1], "443", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s = socket(servinfo->ai_family, servinfo->ai_socktype, | |||
servinfo->ai_protocol); | |||
printf("%d\n", s); | |||
int connection; | |||
if ((connection = connect(s, servinfo->ai_addr, servinfo->ai_addrlen)) == | |||
-1) { | |||
fprintf(stderr, "connection error: %d\n", connection); | |||
exit(1); | |||
} | |||
SSL_load_error_strings(); | |||
SSL_library_init(); | |||
SSL_CTX *ssl_ctx = SSL_CTX_new(SSLv23_client_method()); | |||
SSL *conn = SSL_new(ssl_ctx); | |||
SSL_set_fd(conn, s); | |||
int err; | |||
if ((err = SSL_connect(conn)) != 1) { | |||
fprintf(stderr, "SSL_connect error: %d\n", err); | |||
exit(1); | |||
} | |||
char buf[10000]; | |||
fillHttpReq(buf, argv[1], 10000); | |||
printf("%s\n", buf); | |||
// TODO: send throw a loop | |||
SSL_write(conn, buf, 10000); | |||
memset(buf, 0, 10000); | |||
SSL_read(conn, buf, 10000); | |||
printf("%s\n", buf); | |||
close(s); | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1,60 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
#define BACKLOG 10 | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
struct sockaddr_storage their_addr; | |||
socklen_t addr_size; | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
hints.ai_flags = AI_PASSIVE; | |||
if ((status = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s; | |||
if ((s = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) { | |||
fprintf(stderr, "socket error: %d\n", status); | |||
exit(1); | |||
} | |||
printf("%d\n", s); | |||
int yes=1; | |||
/* when port is already bound to this program */ | |||
if ((status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)) == -1) { | |||
fprintf(stderr, "setsockopt error: %d\n", status); | |||
exit(1); | |||
} | |||
if((status = bind(s, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) { | |||
fprintf(stderr, "bind error: %d\n", status); | |||
exit(1); | |||
} | |||
freeaddrinfo(servinfo); | |||
listen(s, BACKLOG); | |||
addr_size = sizeof their_addr; | |||
int new_fd = accept(s, (struct sockaddr *)&their_addr, &addr_size); | |||
printf("%d\n", new_fd); | |||
char * buf = malloc (100000000); | |||
if () | |||
int bytes = recv(new_fd, buf, 100000000, 0); | |||
printf("bytes received %d\n", bytes); | |||
} |
@@ -0,0 +1,53 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
int main(int argc, char **argv) { | |||
if (argc != 2) { | |||
fprintf(stderr, "Usage: %s <hostname>", argv[0]); | |||
exit(1); | |||
} | |||
int status; | |||
struct addrinfo hints; | |||
memset(&hints, 0, sizeof hints); | |||
struct addrinfo *servinfo; | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
if ((status = getaddrinfo(argv[1], "80", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
struct addrinfo *cur = servinfo; | |||
while (cur) { | |||
void *addr; | |||
char *ipver; | |||
char ipstr[INET6_ADDRSTRLEN]; | |||
if (cur->ai_family == AF_INET) { | |||
struct sockaddr_in *temp = (struct sockaddr_in *)cur->ai_addr; | |||
addr = &(temp->sin_addr); | |||
ipver = "IPv4"; | |||
} else if (cur->ai_family == AF_INET6) { | |||
struct sockaddr_in6 *temp = (struct sockaddr_in6 *)cur->ai_addr; | |||
addr = &(temp->sin6_addr); | |||
ipver = "IPv6"; | |||
} | |||
inet_ntop(cur->ai_family, addr, ipstr, sizeof ipstr); | |||
printf("%s: %s\n", ipver, ipstr); | |||
cur = cur->ai_next; | |||
} | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1,33 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
if (argc < 2) { | |||
fprintf(stderr, "Usage: %s <hostname> <port (optional)>", argv[0]); | |||
exit(1); | |||
} | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_DGRAM; | |||
if ((status = getaddrinfo(argv[1], (argc > 2 ? argv[2] : "80"), &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); | |||
char str[10] = "Hi Mom!"; | |||
sendto(s, str, 10, 0, servinfo->ai_addr, servinfo->ai_addrlen); | |||
} |
@@ -0,0 +1,59 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
int func() { | |||
return 0; | |||
} | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
if ((status = getaddrinfo("127.0.0.1", "2001", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s = socket(servinfo->ai_family, servinfo->ai_socktype, | |||
servinfo->ai_protocol); | |||
printf("%d\n", s); | |||
int connection; | |||
if ((connection = connect(s, servinfo->ai_addr, servinfo->ai_addrlen)) == | |||
-1) { | |||
fprintf(stderr, "connection error: %d\n", connection); | |||
exit(1); | |||
} | |||
printf ("%d\n", connection); | |||
char buf[1000], svg_start[10000], svg_text[10000], svg_end[10000]; | |||
memset(buf, 0, 1000); | |||
sprintf(buf, "Amirlan"); | |||
int bytes = send(s, buf, 1000, 0); | |||
printf("bytes sent %d\n", bytes); | |||
memset(buf, 0, 1000); | |||
memset(svg_start, 0, 10000); | |||
memset(svg_text, 0, 10000); | |||
memset(svg_end, 0, 10000); | |||
recv(s, svg_start, 10000, 0); | |||
recv(s, svg_text, 10000, 0); | |||
recv(s, svg_end, 10000, 0); | |||
FILE * of = fopen("Image.svg", "w"); | |||
fwrite(svg_start, 1, strlen(svg_start), of); | |||
fwrite(svg_text, 1, strlen(svg_text), of); | |||
fwrite(svg_end, 1, strlen(svg_end), of); | |||
close(s); | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1 @@ | |||
<svg xmlns="http://www.w3.org/2000/svg" width="1000" height="1000" style="shape-rendering:geometricPrecision;text-rendering:geometricPrecision;image-rendering:optimizeQuality;fill-rule:evenodd;clip-rule:evenodd"><path fill="#7fbc00" d="M803 335c7-1 13 2 18 9l92 164c7 10 6 19-2 28L795 639l-2 103c-2 7-7 11-14 12H213c-9-2-14-7-16-15l-1-37-21-29-3-11 23-191c3-10 10-15 20-15l179 13 409-134Zm-11 43c3 0 5 1 7 4l75 134-9 10a7347 7347 0 0 0-108 99l-1 86-2 4-259 2-257-1-2-1-3-26a468 468 0 0 0-22-36l18-149c0-6 2-9 7-9l165 11 391-128Z" style="opacity:.974"/><path fill="#7fbc00" d="M675 453c11-3 19 2 23 13v201c-3 7-9 12-18 13-9-1-15-6-18-13V465c3-6 7-10 13-12Z" style="opacity:.994"/><path fill="#7fbc00" d="M770 453c11-3 19 2 23 13v77c-7 12-16 16-29 9-4-2-6-6-7-9v-78c3-6 7-10 13-12Z" style="opacity:.981"/><path fill="#7fbc00" d="M487 505c10-1 17 3 21 13v162c-3 8-9 12-18 13-9-1-15-5-18-13V518c3-8 7-12 15-13Z" style="opacity:.991"/><path fill="#7fbc00" d="M581 516c10-2 18 3 22 13v108c-3 7-9 12-18 13-9-1-15-6-18-13V529c2-7 7-11 14-13Z" style="opacity:.988"/><path fill="#7fbc00" d="M299 528c11-1 18 4 21 16v129c-3 12-11 17-24 15-7-2-11-7-13-13V542c3-8 8-13 16-14Z" style="opacity:.963"/><path fill="#7fbc00" d="M390 562c12-3 20 2 24 14v84c-4 11-12 16-24 13-8-3-12-8-13-15v-80c1-8 5-14 13-16Z" style="opacity:.968"/><text x="350" y="840" style="font-size:72px;fill:#000">Hello, Amirlan!</text></svg> |
@@ -0,0 +1,96 @@ | |||
#include <stdio.h> | |||
#include <string.h> | |||
#include <stdlib.h> | |||
#include <time.h> | |||
#include <sys/socket.h> | |||
#include <arpa/inet.h> | |||
#include <unistd.h> | |||
#define SERVER_IP_ADDR "127.0.0.1" | |||
#define SERVER_PORT 2001 | |||
char *svg_begin = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1000\" height=\"1000\" style=\"shape-rendering:geometricPrecision;text-rendering:geometricPrecision;image-rendering:optimizeQuality;fill-rule:evenodd;clip-rule:evenodd\"><path fill=\"#7fbc00\" d=\"M803 335c7-1 13 2 18 9l92 164c7 10 6 19-2 28L795 639l-2 103c-2 7-7 11-14 12H213c-9-2-14-7-16-15l-1-37-21-29-3-11 23-191c3-10 10-15 20-15l179 13 409-134Zm-11 43c3 0 5 1 7 4l75 134-9 10a7347 7347 0 0 0-108 99l-1 86-2 4-259 2-257-1-2-1-3-26a468 468 0 0 0-22-36l18-149c0-6 2-9 7-9l165 11 391-128Z\" style=\"opacity:.974\"/><path fill=\"#7fbc00\" d=\"M675 453c11-3 19 2 23 13v201c-3 7-9 12-18 13-9-1-15-6-18-13V465c3-6 7-10 13-12Z\" style=\"opacity:.994\"/><path fill=\"#7fbc00\" d=\"M770 453c11-3 19 2 23 13v77c-7 12-16 16-29 9-4-2-6-6-7-9v-78c3-6 7-10 13-12Z\" style=\"opacity:.981\"/><path fill=\"#7fbc00\" d=\"M487 505c10-1 17 3 21 13v162c-3 8-9 12-18 13-9-1-15-5-18-13V518c3-8 7-12 15-13Z\" style=\"opacity:.991\"/><path fill=\"#7fbc00\" d=\"M581 516c10-2 18 3 22 13v108c-3 7-9 12-18 13-9-1-15-6-18-13V529c2-7 7-11 14-13Z\" style=\"opacity:.988\"/><path fill=\"#7fbc00\" d=\"M299 528c11-1 18 4 21 16v129c-3 12-11 17-24 15-7-2-11-7-13-13V542c3-8 8-13 16-14Z\" style=\"opacity:.963\"/><path fill=\"#7fbc00\" d=\"M390 562c12-3 20 2 24 14v84c-4 11-12 16-24 13-8-3-12-8-13-15v-80c1-8 5-14 13-16Z\" style=\"opacity:.968\"/>"; | |||
char *svg_text = "<text x=\"350\" y=\"840\" style=\"font-size:72px;fill:#000\">Hello, %s!</text>"; | |||
char *svg_end = "</svg>"; | |||
struct sockaddr_in server_addr, client_addr; | |||
int welcoming_sock, connection_sock, client_struct_length = sizeof(client_addr); | |||
char client_message[2000], server_message[2000]; | |||
// Sends a message to the accepted client through the connection socket. | |||
int send_message(char *message) | |||
{ | |||
if (send(connection_sock, message, strlen(message), 0) < 0) | |||
{ | |||
printf("Failed to send a message to client\n"); | |||
return -1; | |||
} | |||
return 0; | |||
} | |||
int main(void) | |||
{ | |||
// Create TCP socket | |||
welcoming_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |||
if (welcoming_sock < 0) | |||
{ | |||
printf("Failed to create a socket\n"); | |||
return -1; | |||
} | |||
// Set IP and Port | |||
server_addr.sin_family = AF_INET; | |||
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); | |||
server_addr.sin_port = htons(SERVER_PORT); | |||
int yes=1, status; | |||
/* when port is already bound to this program */ | |||
if ((status = setsockopt(welcoming_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)) == -1) { | |||
fprintf(stderr, "setsockopt error: %d\n", status); | |||
exit(1); | |||
} | |||
// Bind the socket to IP:Port | |||
if (bind(welcoming_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) | |||
{ | |||
printf("Failed to bind the socket\n"); | |||
return -1; | |||
} | |||
// Listen for one incoming TCP connection | |||
if (listen(welcoming_sock, 1) < 0) | |||
{ | |||
printf("Failed to listen to incoming connections\n"); | |||
return -1; | |||
} | |||
printf("Server is listening on %s:%d\n", SERVER_IP_ADDR, SERVER_PORT); | |||
// Accept an incoming client connection | |||
connection_sock = accept(welcoming_sock, (struct sockaddr *)&client_addr, &client_struct_length); | |||
if (connection_sock < 0) | |||
{ | |||
printf("Failed to accept the client connection\n"); | |||
return -1; | |||
} | |||
// Receive the client's username | |||
if (recv(connection_sock, client_message, sizeof(client_message), 0) < 0) | |||
{ | |||
printf("Failed to receive a message from client\n"); | |||
return -1; | |||
} | |||
// Send a vector image to the client | |||
send_message(svg_begin); | |||
sprintf(server_message, svg_text, client_message); | |||
send_message(server_message); | |||
send_message(svg_end); | |||
// Close the sockets | |||
close(connection_sock); | |||
close(welcoming_sock); | |||
return 0; | |||
} |