From 4651ee674a8fc068762182021d2dc66a0532a033 Mon Sep 17 00:00:00 2001 From: structix Date: Fri, 15 Dec 2017 12:36:47 +0100 Subject: [PATCH] Added udpclient #2 --- csoundbox.c | 20 +++++++++++++++++++- csoundbox.h | 1 + udpserver.c | 42 ++++++++++++++++++++++++++++++++---------- udpserver.h | 1 + 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/csoundbox.c b/csoundbox.c index 72dc725..0f4f4ad 100644 --- a/csoundbox.c +++ b/csoundbox.c @@ -37,7 +37,7 @@ int main(int argc, char *argv[]) { break; case 'i': //connect to a csoundbox server - + inputNetwork(optarg); break; case 'l': default: @@ -70,6 +70,24 @@ void inputLocal(void) { endwin(); } +void inputNetwork(char *server) { + initscr(); + nonl(); //no newline + noecho(); + keypad(stdscr, FALSE); //Disable the F1-12 keypad + curs_set(0); //Disable the cursor + + printCursesWelcome(); + + char input[2]; + while ((input[0] = getch()) != 13) { + input[1] = '\0'; + sendKeyUDP(server, input); + } + + endwin(); +} + void printCursesWelcome(void) { int y, x; getmaxyx(stdscr, y, x); diff --git a/csoundbox.h b/csoundbox.h index e697e05..fe557ca 100644 --- a/csoundbox.h +++ b/csoundbox.h @@ -1,3 +1,4 @@ void inputLocal(void); void printCursesWelcome(void); +void inputNetwork(char *server); diff --git a/udpserver.c b/udpserver.c index 824de20..57b691e 100644 --- a/udpserver.c +++ b/udpserver.c @@ -50,10 +50,7 @@ void udpserver(void) } //keep listening for data - while(1) - { - //printf("Waiting for data..."); - //fflush(stdout); + while(1) { memset(buf,'\0', BUFLEN); //try to receive some data, this is a blocking call if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, (unsigned int *)&slen)) == -1) @@ -68,12 +65,6 @@ void udpserver(void) if (!processUDPInput(buf)) { break; } - - //now reply the client with the same data - if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) - { - die("sendto()"); - } } close(s); @@ -92,3 +83,34 @@ int processUDPInput(char *input) { return 1; } + +//Client part +void sendKeyUDP(char *server, char *message) { + struct sockaddr_in si_other; + int s, slen=sizeof(si_other); + + int PORT = cfgreadudpport(); + + if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) + { + die("socket"); + } + + memset((char *) &si_other, 0, sizeof(si_other)); + si_other.sin_family = AF_INET; + si_other.sin_port = htons(PORT); + + if (inet_aton(server , &si_other.sin_addr) == 0) + { + fprintf(stderr, "inet_aton() failed\n"); + exit(1); + } + + //send the message + if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1) + { + die("sendto()"); + } + + close(s); +} diff --git a/udpserver.h b/udpserver.h index 2547aa5..270f706 100644 --- a/udpserver.h +++ b/udpserver.h @@ -1,3 +1,4 @@ void die(char *s); //For debugging void udpserver(void); //start the server by calling this function int processUDPInput(char *input); +void sendKeyUDP(char *server, char *message);