#include #include #include #include #include #define DEST_PORT 5842 // The port used is hardcoded, the IP is fed as an argument for the sake of testing. main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s \n\tDefault IP is 69.55.233.82. Assuming port 5842\n", argv[0]); return(1); } // Need a socket first int sockfd; // Socket descriptor sockfd = socket(PF_INET, SOCK_STREAM, 0); // Creates a struct of type sockaddr_in struct sockaddr_in dest_addr; dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(DEST_PORT); dest_addr.sin_addr.s_addr = inet_addr(argv[1]); memset(&(dest_addr.sin_zero), '\0', 8); // int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1) { printf("Error connecting\n"); return(1); } else { printf("Connected OK to %d @ %s\n", DEST_PORT, argv[1]); } // container for the package unsigned int message[4] = {0, 0, 0, 0}; unsigned int sum = 0; unsigned int tx = 0; unsigned int i = 0; // lets receive the unsigned ints (4 of them) printf("RX:\n"); for (i = 0; i < 4; i++) { tx += recv(sockfd, &message[i], sizeof(unsigned int), 0); printf(" %d 0x%x (%d)\n", i, message[i], message[i]); sum += message[i]; } printf("[%i bytes]\n\n", tx, sum); // answer the server printf("TX:\n 0 0x%x (%u)\n\n", sum, sum); char buf[255] = ""; send(sockfd, &sum, sizeof(sum), 0); // receive final reply recv(sockfd, buf, sizeof(buf), 0); printf("RX: \n %s", buf); // finished close(sockfd); return(0); }