Added udpclient #2
This commit is contained in:
20
csoundbox.c
20
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);
|
||||
|
@@ -1,3 +1,4 @@
|
||||
|
||||
void inputLocal(void);
|
||||
void printCursesWelcome(void);
|
||||
void inputNetwork(char *server);
|
||||
|
42
udpserver.c
42
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);
|
||||
}
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user