Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define porta_locale 7777
- int main(){
- int sockfd;
- struct sockaddr_in my_addr, cli_addr;
- size_t l;
- char testo[512];
- char resp[] = "ACK";
- if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
- printf("ERRORE APERTURA SOCKET\n");
- exit(1);
- }
- memset((char *) &my_addr, 0, sizeof(my_addr));
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(porta_locale);
- my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- if(bind(sockfd, (struct sockaddr *) &my_addr, sizeof(my_addr)) <0){
- printf("ERRORE BINDING\n");
- exit(1);
- }
- for(;;){
- l=sizeof(my_addr);
- recvfrom(sockfd, testo, 511, 0, (struct sockaddr*) &cli_addr, (socklen_t *) &l);
- printf("%s\n", testo);
- sendto(sockfd, resp, strlen(resp)+1, 0, (struct sockaddr*) &cli_addr, sizeof(cli_addr));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement