Created
April 28, 2019 08:20
-
-
Save pelly-ryu/6613edb11352e47c588ba1ad8af5b593 to your computer and use it in GitHub Desktop.
대신 만들어 드렸습니다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <netdb.h> | |
#include <netinet/in.h> | |
#include <string.h> | |
int main( int argc, char *argv[] ) { | |
int sockfd, newsockfd, portno, clilen; | |
char buffer[256]; | |
struct sockaddr_in serv_addr, cli_addr; | |
int n; | |
/* First call to socket() function */ | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockfd < 0) { | |
perror("ERROR opening socket"); | |
exit(1); | |
} | |
/* Initialize socket structure */ | |
bzero((char *) &serv_addr, sizeof(serv_addr)); | |
portno = 5000; | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_addr.s_addr = INADDR_ANY; | |
serv_addr.sin_port = htons(portno); | |
/* Now bind the host address using bind() call.*/ | |
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { | |
perror("ERROR on binding"); | |
exit(1); | |
} | |
/* Now start listening for the clients, here process will | |
* go in sleep mode and will wait for the incoming connection | |
*/ | |
listen(sockfd,5); | |
clilen = sizeof(cli_addr); | |
/* Accept actual connection from the client */ | |
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); | |
if (newsockfd < 0) { | |
perror("ERROR on accept"); | |
exit(1); | |
} | |
printf("Client Connected!\n"); | |
/* If connection is established then start communicating */ | |
while (1) { | |
bzero(buffer,256); | |
n = read( newsockfd,buffer,255 ); | |
if (n < 0) { | |
perror("ERROR reading from socket"); | |
exit(1); | |
} | |
printf("client said : %s\n", buffer); | |
/* Write a response to the client */ | |
n = write(newsockfd, buffer, strlen(buffer)); | |
if (n < 0) { | |
perror("ERROR writing to socket"); | |
exit(1); | |
} | |
if (!bcmp(buffer, "quit", 4)) break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
감사합니다!!