Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
3
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";

int main(){
	char bufor[50];
	int numbytes;

	socketFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(socketFd == -1){
		puts("nie mozna stworzyc gniazda");
		perror("socket: ");
		return -1;
	}	
	
	doKogoAddr.sin_family = AF_INET;
	doKogoAddr.sin_port = htons(33601);	
	doKogoAddr.sin_addr.s_addr = inet_addr(wysylkoweIp2);

	if ( connect(socketFd, (struct sockaddr*)&doKogoAddr, sizeof(struct sockaddr)) == -1){
        	printf ("Nieudana proba polaczenia!\n");
        	perror("connect: ");
        	close(socketFd);
		return -1;
     	}
     	else printf("Polaczenie nawiazane poprawnie\n");
		
	puts("podaj tekst do wyslania - q wychodzi");	
	while(1){
		scanf("%s",bufor);		
	     	numbytes = send(socketFd, bufor, 49, 0);
		if (bufor[0] == 'q') break;	
	}
	close(socketFd);
     	
	return 0;
}