Untitled
unknown
plain_text
2 years ago
1.9 kB
0
Indexable
Never
#include <stdio.h> #include <stdlib.h> #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 <netdb.h> #include <sys/types.h> #include <arpa/inet.h> int socketFd, socketFdAccept; struct sockaddr_in mojAddr, odKogoAddr; char mojeIp[20] = "95.49.132.180"; char mojeIp2[20] = "127.0.0.1"; char wysylkoweIp[20] = "95.49.132.180"; char wysylkoweIp2[20] = "127.0.0.1"; int main(){ socklen_t _size; char bufor[50]; int numbytes; socketFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(socketFd == -1){ puts("nie mozna stworzyc gniazda"); perror("socket: "); return -1; } mojAddr.sin_family = AF_INET; mojAddr.sin_port = htons(33701);//wlasny port //mojAddr.sin_port = 0; //automatyczny wybor wolnego //mojAddr.sin_addr.s_addr = htonl(INADDR_ANY); //sam wybiera nasz adres ip mojAddr.sin_addr.s_addr = inet_addr(mojeIp2); //wlasne wpisanie; if ( (bind(socketFd, (struct sockaddr*)&mojAddr, sizeof(struct sockaddr))) == -1){ printf("Nie mozna skonfigurowac gniazda\n"); close(socketFd); perror("bind: "); return -1; } _size = sizeof(struct sockaddr); while(1){ numbytes=recvfrom(socketFd, bufor, 50-1, 0, (struct sockaddr *)&odKogoAddr, &_size); if ( numbytes == -1){ printf("Nie mozna oebrac\n"); close(socketFd); perror("recvfrom: "); return -1; } if (bufor[0] == 'q') break; printf("%s\n", bufor); } printf("Adres IP i port nadawcy: %s:%d\n", inet_ntoa(odKogoAddr.sin_addr),htons(odKogoAddr.sin_port)); //printf("Odebrana wiadomosc: %s\n",bufor); close(socketFd); close(socketFdAccept); return 0; }