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

@@ -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);
}