반응형
///////CONCURRENT SERVER.c///////////
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define SIZE sizeof(struct sockaddr_in)
int main(void)
{
int sockfd_listen;
int sockfd_connect;
int client_len;
char buff[80];
//struct sockaddr_in server = {AF_INET, 5000, INADDR_ANY};
struct sockaddr_in server,client;
// server, client 소켓 정의
bzero(&server, sizeof(server));//server 에 자신의 크기만큼 0 으로 clear
server.sin_family = AF_INET; //IPv4
server.sin_port = 5000; // 포트 설정
server.sin_addr.s_addr = inet_addr("192.168.56.128");
printf("Server IP %s, Server Port : %u\n",inet_ntoa(server.sin_addr), server.sin_port);
///////////////SOCKET///////////////////
printf("skcket()\n");
sockfd_listen =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //sockfd_listen = 3
if(sockfd_listen < 0)
printf("socket error\n");
else
printf("client sock%d\n",sockfd_listen);
///////////////SOCKET///////////////////
///////////////BIND/////////////////////
//1. IPv4 address
printf("bind()\n");
if(bind(sockfd_listen, (struct sockaddr *)&server, SIZE))
printf("bind error \n");
///////////////BIND/////////////////////
///////////////LISTEN///////////////////
printf("listen()\n");
if(listen(sockfd_listen,5) == -1)
printf("listen error \n");
///////////////LISTEN///////////////////
while(1){
bzero(&client, sizeof(client));
client_len= sizeof(client);
///////////////ACCEPT////////////////////
printf("wating for client\n");
sockfd_connect = accept(sockfd_listen, (struct sockaddr *)&client, &client_len);
//sockfd_connect = 4
if(sockfd_connect < 0 )
printf("accept error\n");
else
printf("sockfd connect %d\n",sockfd_connect);
printf("accepted\n");
printf("SERVER : Client IP : %s, Client Port : %u \n",inet_ntoa(client.sin_addr), ntohs(client.sin_port));
///////////////ACCEPT////////////////////
///////////////ACCEPT////////////////////
//client 와 ACCEPT 하면 fork 하여 자식 프로세스와 통신
if(fork() == 0) { //child process
//close
close(sockfd_listen); // 3 close (parent)
while(1){
bzero(buff,sizeof(buff));
recv(sockfd_connect, buff, sizeof(buff), 0 );
printf("Server recv %s \n",buff);
//"quit" exit
if(strcmp(buff, "quit") == 0){//when BUFFER is a "quit"
printf("client exit \n");
close(sockfd_connect); // 4 child process close
exit(0); // child process exit
}
else {
//fgets(buff, sizeof(buff), stdin); // can't chat is child , buff= hello --> "hello:server"
strcat(buff, ":::SERVER"); // BUFFER 에 :SERVER 를 달아서 server 가 메시지를 받음을 확인
send(sockfd_connect, buff, strlen(buff),0);
continue; // goto 67 line (while)
}
}
close(sockfd_connect); // 4
exit(0); // child process exit
}//end child
//paraent process
printf("close() sockfd_connect = %d\n", sockfd_connect); //4s
close(sockfd_connect);
}//end while
//close(sockfd_listen);
}
Server 의 특징
- Client 코드를 직접 배포
///////////CILENT.c///////////
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#define SIZE sizeof(struct sockaddr_in)
int main(void)
{
int sockfd;
//char send_c, recv_c;
char message[80];
struct sockaddr_in server ;
bzero(&server, sizeof(server));//0 clear
server.sin_family = AF_INET; //IPv4
server.sin_port = 5000;
//server.sin_addr = INADDR_ANY;
server.sin_addr.s_addr = inet_addr("192.168.56.128");
printf("Client IP %s, Client Port : %u\n",inet_ntoa(server.sin_addr), server.sin_port);
/////////SOCKET//////////
printf("skcket()\n");
sockfd =socket(AF_INET, SOCK_STREAM, 0);
/////////SOCKET//////////
/////////CONNECT//////////
printf("connect()\n");
connect(sockfd, (struct sockaddr *)&server, SIZE);
/////////CONNECT//////////
/////////SEND_LOOP////////
while(1){//send -> rcv
printf("client message: ");
fgets(message, sizeof(message), stdin);
send(sockfd,message,strlen(message)-1,0);//fgets include 'enter' space so, -1
bzero(message,sizeof(message));
recv(sockfd, message, sizeof(message), 0);
printf("client recv: %s \n",message);
}
/////////SEND_LOOP////////
printf("close()\n");
close(sockfd);
}
//////////FORK_PRACTICE/////////
#include <stdio.h>
#include <sys/type.h>
#include <unistd.h>
int main(void){
pid_t pid;
//pid 선언
pid=fork();
// fork 하여 자식 프로세스 생성
if(pid>0){// 부모 프로세스 일때
printf("parent process pid : %d \n",getpid());
printf("parent process ppid : %d \n",getppid());
printf("parent process child id : %d \n",pid);
sleep(30);
else if(pid==0){//자식 프로세스 일때
printf("child process pid : %d \n",getpid());
printf("child process ppid : %d \n",getppid());
}
else{
prinf("fork error\n");
}
}
반응형
'Lecture > System Software' 카테고리의 다른 글
[시소프] 3.SIC, SIC/XE (0) | 2021.10.31 |
---|---|
[시소프] 2.Machine Level (0) | 2021.10.26 |
[시소프]1. Integer, Float (0) | 2021.10.25 |