#include #include #include #include #include int main() { /* Create a UDP server socket on port 1337 */ int server_fd = socket(PF_INET, SOCK_DGRAM, 0); struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; //inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr); inet_pton(AF_INET, "0.0.0.0", &server_addr.sin_addr); server_addr.sin_port = htons(1337); bind(server_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)); /* Connect the server socket */ struct sockaddr_in connect_addr; connect_addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &connect_addr.sin_addr); connect_addr.sin_port = htons(80); connect(server_fd, (struct sockaddr*)&connect_addr, sizeof(struct sockaddr_in)); /* Disconnect the server socket */ struct sockaddr disconnect_addr; memset(&disconnect_addr, 0, sizeof(struct sockaddr)); disconnect_addr.sa_family = AF_UNSPEC; connect(server_fd, &disconnect_addr, sizeof(struct sockaddr)); /* Create a UDP client socket, connect it to the server */ int client_fd = socket(PF_INET, SOCK_DGRAM, 0); struct sockaddr_in client_addr; client_addr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &client_addr.sin_addr); client_addr.sin_port = htons(1337); connect(client_fd, (struct sockaddr*)&client_addr, sizeof(struct sockaddr_in)); /* Send a message from the client to the server */ char buf[8]; send(client_fd, "hello", sizeof("hello"), 0); recv(server_fd, buf, sizeof(buf), 0); printf("%s\n", buf); return 0; }