-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketserver.c
More file actions
69 lines (53 loc) · 2.01 KB
/
socketserver.c
File metadata and controls
69 lines (53 loc) · 2.01 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*************************************************************************
> File Name: socketserver.c
> Author: Li Wen
> Mail: loneavon1@gmail.com
> Created Time: 2014年02月24日 星期一 00时52分51秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
const int maxline = 4096;
const char * retmeg="It's server ackmeggage!\n";
int main(int argc, char** argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
int n;
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6666);
if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1 ){
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
if( listen(listenfd, 10) == -1 ){
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
printf("======waiting for client's request======\n");
while(1){
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1 ){
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
n = recv(connfd, buff, maxline, 0);
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
if ( send(connfd,retmeg,strlen(retmeg),0) < 0 ){
printf("send msg error: %s (errno: %d)\n",strerror(errno),errno);
}
close(connfd);
}
close(listenfd);
}