Untitled
unknown
plain_text
2 years ago
1.6 kB
6
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> #include <fcntl.h> int main() { int socketFd, socketFdAccept; struct sockaddr_in A1, A3, A2; char IP_priv[9] = "127.0.0.1"; char IP_wyslania[9] = "127.0.0.1"; socklen_t _rozmiar; char bufor[70]; int i, K; socketFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(socketFd == -1){ puts("Nie da sie stworzyc gniazda"); perror("socket: "); return -1; } fcntl(socketFd, F_SETFL,O_NONBLOCK|FASYNC); A2.sin_family = AF_INET; A2.sin_port = htons(33700); A2.sin_addr.s_addr = inet_addr(IP_wyslania); A1.sin_family = AF_INET; A1.sin_port = htons(33701); A1.sin_addr.s_addr = inet_addr(IP_priv); if ( (bind(socketFd, (struct sockaddr*)&A1, sizeof(struct sockaddr))) == -1){ printf("Blad konfiguracji\n"); close(socketFd); perror("bind: "); return -1; } _rozmiar = sizeof(struct sockaddr); while(1) { for(i=10; i>0; i--) { if (recvfrom(socketFd, bufor, 50-1, 0, (struct sockaddr *)&A3, &_rozmiar) <0) { K = 0; sleep(1); } else { K = 1; break; } } sendto(socketFd, &K, sizeof(K), 0, (struct sockaddr*)&A2, sizeof(struct sockaddr)); if (bufor[0] == 'q') break; if(K == 1) printf("%s\n", bufor); } close(socketFd); close(socketFdAccept); return 0; }