Created
January 12, 2022 11:47
-
-
Save Park-Developer/d4fded3372e76854c370f202045a6dbe to your computer and use it in GitHub Desktop.
tcp_server using c
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 <string.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#define TCP_PORT 5100 | |
int main(int argc, char **argv) | |
{ | |
int ssock; | |
socklen_t clen; // 소켓 디스크립터 정의 | |
int n; | |
struct sockaddr_in servaddr, cliaddr; // 구조체 정의 | |
char mesg[BUFSIZ]; | |
// 소켓 생성 | |
if((ssock=socket(AF_INET,SOCK_STREAM,0))<0){ | |
perror("socket()"); | |
return -1; | |
} | |
// 주소 구조체에 주소 지정 | |
memset(&servaddr,0,sizeof(servaddr)); | |
servaddr.sin_family=AF_INET; | |
servaddr.sin_addr.s_addr=htonl(INADDR_ANY); | |
servaddr.sin_port=htons(TCP_PORT); // 사용할 포트 지정 | |
// bind 함수를 사용하여 서버 소켓의 주소 설정 | |
if(bind(ssock,(struct sockaddr *)&servaddr,sizeof(servaddr))<0){ | |
perror("bind()"); | |
return -1; | |
} | |
// 동시에 접속하는 클라이언트의 처리를 위한 대기 큐를 설정 | |
if(listen(ssock,8)<0){ // 최대 8개의 클라이언트가 동시 접속 대기할 수 있도록 설정 | |
perror("listen()"); | |
return -1; | |
} | |
clen=sizeof(cliaddr); | |
do{ | |
// 클라이언트가 접속하면 접속을 허용하고 클라이언트 소켓 생성 | |
int n, csock = accept(ssock,(struct sockaddr *)&cliaddr,&clen); | |
// 네트워크 주소를 문자열로 변경 | |
inet_ntop(AF_INET,&cliaddr.sin_addr,mesg,BUFSIZ); | |
printf("Client is conneted : %s\n",mesg); | |
if ((n=read(csock, mesg,BUFSIZ))<=0) | |
perror("read()"); | |
printf("Received Data : &s",mesg); | |
// 클라이언트로 buf에 있는 문자열 전송 | |
if(write(csock,mesg,n)<=0) | |
perror("write()"); | |
close(csock); | |
}while(strncmp(mesg,"q",1)); | |
close(ssock); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment