Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** talker.c -- a datagram "client" demo
- ** taken from beej
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include "common.h"
- #define SERVERPORT "4950" // the port users will be connecting to
- #define SERVER_IP_ADDRESS "192.168.1.12"
- int main()
- {
- int sockfd, sent_recv_bytes = 0;
- struct addrinfo hints, *servinfo, *p;
- int rv;
- int numbytes;
- test_struct_t client_data;
- result_struct_t result;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_DGRAM;
- if ((rv = getaddrinfo(SERVER_IP_ADDRESS, SERVERPORT, &hints, &servinfo)) != 0) {
- fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
- return 1;
- }
- // loop through all the results and make a socket
- for(p = servinfo; p != NULL; p = p->ai_next) {
- if ((sockfd = socket(p->ai_family, p->ai_socktype,
- p->ai_protocol)) == -1) {
- perror("talker: socket");
- continue;
- }
- break;
- }
- if (p == NULL) {
- fprintf(stderr, "talker: failed to create socket\n");
- return 2;
- }
- int addr_len = sizeof(struct sockaddr);
- while(1) {
- /*Prompt the user to enter data*/
- /*You will want to change the promt for the second task*/
- printf("Enter name : ?\n");
- scanf("%s", &client_data.name);
- printf("Enter age : ?\n");
- scanf("%s", &client_data.age);
- printf("Enter group : ?\n");
- scanf("%s", &client_data.group);
- if ((numbytes = sendto(sockfd, &client_data, sizeof(test_struct_t), 0, p->ai_addr, p->ai_addrlen)) == -1){
- perror("talker: sendto");
- exit(1);
- }
- printf("talker: sent %d bytes to %s\n", numbytes, SERVER_IP_ADDRESS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement