Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <string.h>
- #include <errno.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <math.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <endian.h>
- // use ./client paris.kocachic.com 2280 23 43 43
- //Global Variables
- //Function prototypes
- //Functions
- //This function prints all the arguments in the command line
- void printArguments(int argc, char **argv){
- printf("Number of arguments including the filename %d\n", argc);
- // Print the elements of the array argv
- printf("The elements of the array are:\n");
- for (int i = 0; i < (int)(argc - 1) ; ++i) {
- printf("%s\n", argv[i + 1]); //Plus one so as not to print './'
- }
- }
- int main(int argc, char **argv){
- printArguments(argc, argv);
- printf("%s\n", argv[0]);
- //printf("The output of argv[argc-3]: %d", (int)*argv[argc -3] ); //This print system call returns 50 which the ascci code of 2
- long x1Long = strtol(argv[argc -3], NULL, 10);
- long x2Long = strtol(argv[argc -2], NULL, 10);
- long x3Long = strtol(argv[argc -1], NULL, 10);
- long thePortLong = strtol(argv[2], NULL, 10);
- uint16_t thePort = (uint16_t)thePortLong;
- int sock;
- if ( ( sock = socket(PF_INET, SOCK_STREAM, 0) ) < 0 ) {
- printf("\n Socket creation error \n");
- return -1;
- }
- struct sockaddr_in addr = {
- .sin_family = AF_INET,
- .sin_port = htobe16(thePort),
- };
- struct hostent *host = gethostbyname(argv[1]);
- // copy the address into the addr struct
- memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);
- if ( ( connect( sock, (struct sockaddr *)&addr, sizeof addr ) ) < 0)
- {
- printf("\nConnection Failed \n");
- return -1;
- }
- int amountOfNumbersToSend = 3;
- // number we want to send
- uint64_t xsend0 = htobe64(amountOfNumbersToSend);
- write(sock, &xsend0, sizeof xsend0);
- int x1 = (int)x1Long ;
- // number we want to send
- uint64_t xsend1 = htobe64(x1);
- write(sock, &xsend1, sizeof xsend1);
- int x2 = (int)x2Long;
- // number we want to send
- uint64_t xsend2 = htobe64(x2);
- write(sock, &xsend2, sizeof xsend2);
- int x3 = (int)x3Long ;
- // number we want to send
- uint64_t xsend3 = htobe64(x3);
- write(sock, &xsend3, sizeof xsend3);
- uint64_t x_recv1;
- read(sock, &x_recv1, sizeof x_recv1);
- int r1 = be64toh(x_recv1);
- uint64_t x_recv2;
- read(sock, &x_recv2, sizeof x_recv2);
- int r2 = be64toh(x_recv2);
- uint64_t x_recv3;
- read(sock, &x_recv3, sizeof x_recv3);
- int r3 = be64toh(x_recv3);
- printf("The output %d\n", r1);
- printf("The output %d\n", r2);
- printf("The output %d\n", r3);
- return 0;
- }
- /*
- Notes
- //These lines of codes are for the server
- struct sockaddr_in listening_addr = {
- .sin_family = AF_INET, // IPv4
- .sin_port = htobe16(argv[2]), // listen on port 3000
- .sin_addr = { // listen on any/every interface
- .s_addr = htobe32(INADDR_ANY),
- },
- };
- //bind(sock, (struct sockaddr *)&listening_addr, sizeof listening_addr);
- listen(sock, 2);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement