Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <netdb.h>
- #include <memory.h>
- // #define BUF_SIZE 64
- // int main()
- // {
- // int sock;
- // struct sockaddr_in addr;
- // char buf;
- // int bytes_read;
- // sock = socket(AF_INET, SOCK_DGRAM, 0);
- // addr.sin_family = AF_INET;
- // addr.sin_port = htons(3495);
- // addr.sin_addr.s_addr = htonl(INADDR_ANY);
- // bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0;
- // printf("%s\n", "la");
- // while(1)
- // {
- // printf("%s\n", "la");
- // bytes_read = recv(sock, &buf, 1, 0);
- // printf("%s\n", "la");
- // printf("%c", buf);
- // printf("%s\n", "la");
- // if (bytes_read<=0) break;
- // }
- // close(sock);
- // return 0;
- // }
- main()
- {
- int s, namelen, client_address_size;
- struct sockaddr_in client, server;
- char buf[32];
- if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
- {
- exit(1);
- }
- server.sin_family = AF_INET; /* Server is in Internet Domain */
- server.sin_port = 0; /* Use any available port */
- server.sin_addr.s_addr = INADDR_ANY;/* Server's Internet Address */
- if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
- {
- exit(2);
- }
- /* Find out what port was really assigned and print it */
- namelen = sizeof(server);
- if (getsockname(s, (struct sockaddr *) &server, &namelen) < 0)
- {
- exit(3);
- }
- printf("Port assigned is %d\n", ntohs(server.sin_port));
- client_address_size = sizeof(client);
- if(recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &client,
- &client_address_size) <0)
- {
- exit(4);
- }
- printf("Received message %s from domain %s port %d internet\
- address %s\n",
- buf,
- (client.sin_family == AF_INET?"AF_INET":"UNKNOWN"),
- ntohs(client.sin_port),
- inet_ntoa(client.sin_addr));
- close(s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement