Added udpclient #2

This commit is contained in:
2017-12-15 12:36:47 +01:00
parent c298de57a8
commit 4651ee674a
4 changed files with 53 additions and 11 deletions

View File

@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) {
break; break;
case 'i': case 'i':
//connect to a csoundbox server //connect to a csoundbox server
inputNetwork(optarg);
break; break;
case 'l': case 'l':
default: default:
@@ -70,6 +70,24 @@ void inputLocal(void) {
endwin(); 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) { void printCursesWelcome(void) {
int y, x; int y, x;
getmaxyx(stdscr, y, x); getmaxyx(stdscr, y, x);

View File

@@ -1,3 +1,4 @@
void inputLocal(void); void inputLocal(void);
void printCursesWelcome(void); void printCursesWelcome(void);
void inputNetwork(char *server);

View File

@@ -50,10 +50,7 @@ void udpserver(void)
} }
//keep listening for data //keep listening for data
while(1) while(1) {
{
//printf("Waiting for data...");
//fflush(stdout);
memset(buf,'\0', BUFLEN); memset(buf,'\0', BUFLEN);
//try to receive some data, this is a blocking call //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) 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)) { if (!processUDPInput(buf)) {
break; 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); close(s);
@@ -92,3 +83,34 @@ int processUDPInput(char *input) {
return 1; 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);
}

View File

@@ -1,3 +1,4 @@
void die(char *s); //For debugging void die(char *s); //For debugging
void udpserver(void); //start the server by calling this function void udpserver(void); //start the server by calling this function
int processUDPInput(char *input); int processUDPInput(char *input);
void sendKeyUDP(char *server, char *message);