Untitled
unknown
plain_text
2 years ago
1.3 kB
5
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 main() { int socketFd; socklen_t _rozmiar; struct sockaddr_in A1, A2, A3; char IP_wyslania[9] = "127.0.0.1"; char IP_priv[9] = "127.0.0.1"; int K = 1; char bufor[70]; socketFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(socketFd == -1){ puts("Nie da sie stworzyc gniazda"); perror("socket: "); return -1; } A2.sin_family = AF_INET; A2.sin_port = htons(33701); A2.sin_addr.s_addr = inet_addr(IP_wyslania); A1.sin_family = AF_INET; A1.sin_port = htons(33700); 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; } puts("podaj tekst do wyslania - q wychodzi"); while(1) { if(K == 1) scanf("%s",bufor); sendto(socketFd, bufor, 50-1, 0, (struct sockaddr*)&A2, sizeof(struct sockaddr)); recvfrom(socketFd, &K, sizeof(K), 0, (struct sockaddr *)&A3, &_rozmiar); if(bufor[0] == 'q' && K == 1) break; } close(socketFd); return 0; }