From 566e2f03b5e0e9414c6f503c285a1a6702866140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Sabol?= Date: Mon, 2 Dec 2019 12:53:44 +0000 Subject: [PATCH] =?UTF-8?q?Nahr=C3=A1t=20soubory=20do=20'.'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- program.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 program.c diff --git a/program.c b/program.c new file mode 100644 index 0000000..dc307ae --- /dev/null +++ b/program.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +void error(char *msg) { + //tato funkcia sa vyuziva ked systemove volanie zlyha + perror(msg); //vypise spravu o chybe a ukonci program server + exit(1); +} +int main(int argc, char *argv[]) { + int sockfd, newsockfd, portno, clilen, n; //pomocne premenne + char buffer[256]; //buffer pre ulozenie znakov zo socketu + struct sockaddr_in serv_addr; //obsahuje adresu servera + struct sockaddr_in cli_addr; //obsahuje adresu klienta + if (argc < 2) { //kontrola poctu argumentov + fprintf(stderr,"ERROR, no port provided\n"); + exit(1); + } //vytvorenie socketu + sockfd = socket(PF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + error("ERROR opening socket"); //naplnenie struktury sockaddr_in + bzero((char *) &serv_addr, sizeof(serv_addr)); + portno = atoi(argv[1]); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); //zviazanie socketu sluzbou bind() + //printf("%s", hostent->h_name); + if (bind(sockfd, (struct sockaddr *) &serv_addr, + sizeof(serv_addr)) < 0) + error("ERROR on binding"); + listen(sockfd,5); //vytvorime si front poziadaviek + clilen = sizeof(cli_addr); //velkost struktury adresy klienta //akceptovanie spojenia + newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); + if (newsockfd < 0) + error("ERROR on accept"); + bzero(buffer,256); //spracovanie dat od klientov + n = read(newsockfd,buffer,255); + if (n < 0) + error("ERROR reading from socket"); + printf("Here is the message: %s\n",buffer); + n = write(newsockfd,"I got your message",18); + if (n < 0) + error("ERROR writing to socket"); + return 0; +}