1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "unicast_socket_sendto.h"
void *send_uni(void *arg)
{
char *dest_ip = "127.0.0.1";
char *message = "hello";
int port = 7;
while (1)
{
printf("Push a letter S or Q:\r\n ");
char c = getchar();
if ((c=='s')||(c=='S'))
{
printf("Message sent: %s\n", message);
unicast_socket_sendto(port, dest_ip, message);
system("BREAK");
return EXIT_SUCCESS;
}
else if ((c=='q')||(c=='Q'))
{
printf("QUIT");
break;
}
else
{
printf("Invalid key, tape 'S' to send a message, 'Q' to exit.");
}
}
return NULL;
}
int main(void)
{
/* this variable is our reference to the second thread */
pthread_t thread;
/* create a second thread which executes send_uni(void *arg) */
if(pthread_create(&thread, NULL, send_uni, NULL)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
/* wait for the second thread to finish */
if(pthread_join(thread, NULL)) {
fprintf(stderr, "Error joining thread\n");
return 2;
}
return 0;
} |
Partager