forked from s444341/Projekt_SIK
39 lines
1.1 KiB
C
39 lines
1.1 KiB
C
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <stdio.h>
|
|
#include <netinet/in.h>
|
|
#include <string.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
int main(void) {
|
|
char bufor[1024]; //wiadomosc
|
|
int gniazdo, gniazdo2;
|
|
struct sockaddr_in adr, nadawca;
|
|
socklen_t dl = sizeof(struct sockaddr_in);
|
|
|
|
gniazdo = socket(PF_INET, SOCK_STREAM, 0);
|
|
adr.sin_family = AF_INET;
|
|
adr.sin_port = htons(44444); //port
|
|
adr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port));
|
|
|
|
if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind
|
|
printf("Bind nie powiodl sie.\n");
|
|
return 1;
|
|
}
|
|
if (listen(gniazdo, 10) < 0) { //listen
|
|
printf("Listen nie powiodl sie.\n");
|
|
return 1;
|
|
}
|
|
printf("Czekam na polaczenie ...\n");
|
|
while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie
|
|
memset(bufor, 0, 1024);
|
|
recv(gniazdo2, bufor, 1024, 0);
|
|
printf("Wiadomosc od %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor);
|
|
close(gniazdo2);
|
|
}
|
|
close(gniazdo);
|
|
return 0;
|
|
} |