#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_LINE 4096
#define PORT 20120
int main(void)
{
struct sockaddr_in sin;
struct sockaddr_in cin;
int l_fd, c_fd;
socklen_t len;
char buf[MAX_LINE];
int n;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port=htons(PORT);
if((l_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("fail to create socket");
exit(1);
}
if(bind(l_fd, (struct sockaddr*)&sin, sizeof(sin)) == -1){
perror("fail to bind");
exit(1);
}
if( listen(l_fd, 10) == -1){
perror("fail to listen");
exit(1);
}
printf("waiting...\n");
while(1){
if((c_fd = accept(l_fd, (struct sockaddr*)&cin, &len)) == -1){
perror("fail to accept");
exit(1);
}
n = recv(c_fd, buf, MAX_LINE, 0);
if(n == -1){
perror("fail to receive");
exit(1);
}
printf("content is : %s\n", buf);
close(c_fd);
}
return 0;
}
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_LINE 4096
#define PORT 20120
int main(void)
{
struct sockaddr_in sin;
struct sockaddr_in cin;
int l_fd, c_fd;
socklen_t len;
char buf[MAX_LINE];
int n;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port=htons(PORT);
if((l_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("fail to create socket");
exit(1);
}
if(bind(l_fd, (struct sockaddr*)&sin, sizeof(sin)) == -1){
perror("fail to bind");
exit(1);
}
if( listen(l_fd, 10) == -1){
perror("fail to listen");
exit(1);
}
printf("waiting...\n");
while(1){
if((c_fd = accept(l_fd, (struct sockaddr*)&cin, &len)) == -1){
perror("fail to accept");
exit(1);
}
n = recv(c_fd, buf, MAX_LINE, 0);
if(n == -1){
perror("fail to receive");
exit(1);
}
printf("content is : %s\n", buf);
close(c_fd);
}
return 0;
}