SHOW:
|
|
- or go back to the newest paste.
| 1 | - | #include <sys/socket.h> |
| 1 | + | #include <sys/socket.h> |
| 2 | - | #include <sys/types.h> |
| 2 | + | #include <sys/types.h> |
| 3 | - | #include <netinet/in.h> |
| 3 | + | #include <netinet/in.h> |
| 4 | - | #include <stdio.h> |
| 4 | + | #include <stdio.h> |
| 5 | - | #define SERV_PORT 45000 |
| 5 | + | #define SERV_PORT 45000 |
| 6 | - | #define BUFOR 100 |
| 6 | + | #define BUFOR 100 |
| 7 | - | int main() |
| 7 | + | int main() |
| 8 | {
| |
| 9 | - | int listenfd; |
| 9 | + | int listenfd; |
| 10 | - | int connfd; |
| 10 | + | int connfd; |
| 11 | - | pid_t child; |
| 11 | + | pid_t child; |
| 12 | - | socklen_t klient_len; |
| 12 | + | socklen_t klient_len; |
| 13 | - | struct sockaddr_in klient_addres, serwer_addres; |
| 13 | + | struct sockaddr_in klient_addres, serwer_addres; |
| 14 | - | char Addr[100]; |
| 14 | + | char Addr[100]; |
| 15 | - | int ChId; |
| 15 | + | int ChId; |
| 16 | - | listenfd = socket(AF_INET, SOCK_STREAM, 0); |
| 16 | + | listenfd = socket(AF_INET, SOCK_STREAM, 0); |
| 17 | - | bzero(&serwer_addres, sizeof(serwer_addres)); |
| 17 | + | bzero(&serwer_addres, sizeof(serwer_addres)); |
| 18 | - | serwer_addres.sin_family = AF_INET; |
| 18 | + | serwer_addres.sin_family = AF_INET; |
| 19 | - | serwer_addres.sin_addr.s_addr = htonl(INADDR_ANY); |
| 19 | + | serwer_addres.sin_addr.s_addr = htonl(INADDR_ANY); |
| 20 | - | serwer_addres.sin_port = htons(SERV_PORT); |
| 20 | + | serwer_addres.sin_port = htons(SERV_PORT); |
| 21 | - | bind(listenfd, &serwer_addres, sizeof(serwer_addres)); |
| 21 | + | bind(listenfd, &serwer_addres, sizeof(serwer_addres)); |
| 22 | - | listen(listenfd, BUFOR); |
| 22 | + | listen(listenfd, BUFOR); |
| 23 | - | inet_ntop(AF_INET, &serwer_addres.sin_addr, Addr, 100); |
| 23 | + | inet_ntop(AF_INET, &serwer_addres.sin_addr, Addr, 100); |
| 24 | - | printf("\nSERWER: Serwer zostal uruchomiony IP=%s,%d", Addr, SERV_PORT);
|
| 24 | + | printf("\nSERWER: Serwer zostal uruchomiony IP=%s,%d", Addr, SERV_PORT);
|
| 25 | - | for (;;) |
| 25 | + | for (;;) |
| 26 | {
| |
| 27 | - | klient_len = sizeof(klient_addres); |
| 27 | + | klient_len = sizeof(klient_addres); |
| 28 | - | connfd = accept(listenfd, &klient_addres, &klient_len); |
| 28 | + | connfd = accept(listenfd, &klient_addres, &klient_len); |
| 29 | - | inet_ntop(AF_INET, &klient_addres.sin_addr,Addr,100); |
| 29 | + | if (fork() == 0) |
| 30 | - | printf("\nSERWER: Polaczenie klienta z komputerem o adresie IP=%s",Addr);
|
| 30 | + | |
| 31 | - | Echo(connfd); |
| 31 | + | ChId = getpid(); |
| 32 | - | printf("\nSERWER: Obsluga klienta o adresie IP=%s zostala zakonczona", Addr);
|
| 32 | + | printf("%d", ChId);
|
| 33 | close(listenfd); | |
| 34 | inet_ntop(AF_INET, &klient_addres.sin_addr, Addr, 100); | |
| 35 | printf("\nSERWER(%ld): Polaczenie klienta z komputerem o adresie IP=%s", ChId, Addr);
| |
| 36 | - | void Echo(int socketfd) |
| 36 | + | Echo(connfd); |
| 37 | printf("\nSERWER(%ld): Obsluga klienta o adresie IP=%s zostala zakonczona", ChId, Addr);
| |
| 38 | - | int n; |
| 38 | + | |
| 39 | - | char bufor[100]; |
| 39 | + | exit(0); |
| 40 | - | char line[100]; |
| 40 | + | |
| 41 | - | for (;;) |
| 41 | + | close(connfd); |
| 42 | } | |
| 43 | - | n = read(socketfd, line, 100); // n=strlen (1ine); |
| 43 | + | } |
| 44 | - | // n=Czytaj( socket, line, 100); |
| 44 | + | void Echo(int socketfd) |
| 45 | - | if (n <= 1) |
| 45 | + | {
|
| 46 | - | {
|
| 46 | + | int n; |
| 47 | - | printf("\nSERWER: POLACZENIE ZAKONCZONO ");
|
| 47 | + | char bufor[100]; |
| 48 | - | return; |
| 48 | + | char line[100]; |
| 49 | - | } |
| 49 | + | for (;;) |
| 50 | - | |
| 50 | + | |
| 51 | - | line[n] = '\0'; |
| 51 | + | n = read(socketfd, line, 100); // n=strlen (1ine); |
| 52 | - | printf("\nSERWER: ODEBRANO %d znakow--> %s ", n, line);
|
| 52 | + | // n=Czytaj( socket, line, 100); |
| 53 | - | write(socketfd, line, n); |
| 53 | + | if (n <= 1) |
| 54 | - | printf("\nSERWER: WYSLANO --> %s", line);
|
| 54 | + | |
| 55 | - | } // for |
| 55 | + | printf("\nSERWER: POLACZENIE ZAKONCZONO ");
|
| 56 | - | } // Echo |
| 56 | + | return; |
| 57 | } | |
| 58 | ||
| 59 | line[n] = '\0'; | |
| 60 | printf("\nSERWER: ODEBRANO %d znakow--> %s ", n, line);
| |
| 61 | write(socketfd, line, n); | |
| 62 | printf("\nSERWER: WYSLANO --> %s", line);
| |
| 63 | } // for | |
| 64 | } |