Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #define SERV_PORT 45000
- #define BUFOR 100
- int main()
- {
- int listenfd;
- int connfd;
- pid_t child;
- socklen_t klient_len;
- struct sockaddr_in klient_addres, serwer_addres;
- char Addr[100];
- int ChId;
- listenfd = socket(AF_INET, SOCK_STREAM, 0);
- bzero(&serwer_addres, sizeof(serwer_addres));
- serwer_addres.sin_family = AF_INET;
- serwer_addres.sin_addr.s_addr = htonl(INADDR_ANY);
- serwer_addres.sin_port = htons(SERV_PORT);
- bind(listenfd, &serwer_addres, sizeof(serwer_addres));
- listen(listenfd, BUFOR);
- inet_ntop(AF_INET, &serwer_addres.sin_addr, Addr, 100);
- printf("\nSERWER: Serwer zostal uruchomiony IP=%s,%d", Addr, SERV_PORT);
- for (;;)
- {
- klient_len = sizeof(klient_addres);
- connfd = accept(listenfd, &klient_addres, &klient_len);
- if (fork() == 0)
- {
- ChId = getpid();
- printf("%d", ChId);
- close(listenfd);
- inet_ntop(AF_INET, &klient_addres.sin_addr, Addr, 100);
- printf("\nSERWER(%ld): Polaczenie klienta z komputerem o adresie IP=%s", ChId, Addr);
- Echo(connfd);
- printf("\nSERWER(%ld): Obsluga klienta o adresie IP=%s zostala zakonczona", ChId, Addr);
- close(connfd);
- exit(0);
- }
- close(connfd);
- }
- }
- void Echo(int socketfd)
- {
- int n;
- char bufor[100];
- char line[100];
- for (;;)
- {
- n = read(socketfd, line, 100); // n=strlen (1ine);
- // n=Czytaj( socket, line, 100);
- if (n <= 1)
- {
- printf("\nSERWER: POLACZENIE ZAKONCZONO ");
- return;
- }
- line[n] = '\0';
- printf("\nSERWER: ODEBRANO %d znakow--> %s ", n, line);
- write(socketfd, line, n);
- printf("\nSERWER: WYSLANO --> %s", line);
- } // for
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement