Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz program, ktory bedzie realizowal polecenie zawarte w zadaniu pierwszy, ale w oparciu o protokol UDP/IP
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <string.h>
- #define SERVER_PORT 1919
- #define SERVER_IP "127.0.0.1"
- void error_handler(const char *msg)
- {
- perror(msg);
- exit(EXIT_FAILURE);
- }
- int main(void)
- {
- int socketfd;
- struct sockaddr_in sockaddr;
- char buffer[BUFSIZ];
- if(!inet_aton(SERVER_IP, &sockaddr.sin_addr))
- error_handler("inet_aton");
- sockaddr.sin_family = AF_INET;
- sockaddr.sin_port = htons(SERVER_PORT);
- memset(sockaddr.sin_zero, '\0', 8);
- if((socketfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- error_handler("socket");
- memset(buffer, '\0', BUFSIZ);
- printf("Podaj wiadomosc do wyslania: ");
- fgets(buffer, BUFSIZ, stdin);
- if(sendto(socketfd, buffer, BUFSIZ, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
- error_handler("sendto");
- if(close(socketfd) == -1)
- error_handler("close");
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment