Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- #include <errno.h>
- #include <signal.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- //server tcp dzialajacy interacyjnie pod adres lokalhost port > 1024
- //klient program rc serwer wysyla w formie tekstowej znaczniki czasu takie jakie istnieja
- #define PORT 8080
- #define SA const struct sockaddr
- void errorExit(const char* str)
- {
- perror(str);
- exit(EXIT_FAILURE);
- }
- int sockfd, len, n;
- struct sockaddr_in servaddr, cliaddr;
- void func()
- {
- int SIZE = 256;
- char buff[SIZE];
- len = sizeof(cliaddr);
- while(1)
- {
- memset(&buff,'\0',sizeof(buff));
- n = recvfrom(sockfd, (char*)buff, SIZE, MSG_WAITALL, (struct sockaddr*) &cliaddr, (socklen_t*)&len);
- buff[n] = '\0';
- printf("%s\n", buff);
- sendto(sockfd, "siema", 5, MSG_CONFIRM, (const struct sockaddr*)&cliaddr, len);
- }
- }
- int main( int argc, char* argv[] )
- {
- if((sockfd=socket(AF_INET, SOCK_DGRAM, 0))==-1)
- errorExit("socket");
- memset(&servaddr,'\0', sizeof(servaddr));
- memset(&cliaddr,'\0', sizeof(cliaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- servaddr.sin_port=htons(PORT);
- if(bind(sockfd, (SA*)&servaddr, sizeof(servaddr))!=0)
- errorExit("bind");
- func();
- if(close(sockfd)==-1)
- errorExit("close");
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement