Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 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;
struct sockaddr_in mojAddr, doKogoAddr;
char wysylkoweIp2[20] = "127.0.0.1";
char mojeIp2[20] = "127.0.0.1";

int main(){
	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;
	}	
	
	doKogoAddr.sin_family = AF_INET;
	doKogoAddr.sin_port = htons(33701);	
	doKogoAddr.sin_addr.s_addr = inet_addr(wysylkoweIp2);
	
	mojAddr.sin_family = AF_INET;
	mojAddr.sin_port = htons(33700); //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;	

	puts("podaj tekst do wyslania - q wychodzi");	
	while(1){
		scanf("%s",bufor);		
	     	numbytes = sendto(socketFd, bufor, 50-1, 0, (struct sockaddr*)&doKogoAddr, sizeof(struct sockaddr));
		if (bufor[0] == 'q') break;	
	}
		
	
	close(socketFd);
     	
	return 0;
}