Added udpclient #2
This commit is contained in:
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user