티스토리 뷰
code from Computer_Networks_Larry_Peterson_5ed
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 47500
#define MAX_PENDING 5
#define MAX_LINE 256
int main() {
struct sockaddr_in sin;
char buf[MAX_LINE];
int len;
int s, new_s;
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s, MAX_PENDING);
/* wait for connection, then receive and print text */
while(1) {
if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
while (len = recv(new_s, buf, sizeof(buf), 0))
fputs(buf, stdout);
close(new_s);
}
}
bind()
https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-bind
listen()
https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-listen
accept()
https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-accept
recv()
https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-recv
'C, C++' 카테고리의 다른 글
C) Socket Connect : Client~Server (0) | 2019.09.12 |
---|---|
C) basic Socket code (Client) (0) | 2019.09.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- socket
- bfs
- dfs
- 개발자
- git
- sort
- reversing
- JPA
- Android Studio
- brute-force
- webhacking.kr
- Java
- Algorithm
- 리버싱
- 프로그래머스
- 웹해킹
- Data Structure
- C
- 회고
- Stack
- javascript
- mysql
- Android
- FRAGMENT
- Vo
- 우아한 테크코스
- 해외여행
- graph
- OneToMany
- queue
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함