- C/S
1 |
|
- server
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int main(int argc,char **argv)
{
struct sockaddr_in sin;
struct sockaddr_in cin;
int l_fd;
int c_fd;
socklen_t len;
char buf[MAX_LINE];
char addr_p[INET_ADDR_STR_LEN];
int port = 8000;
int n;
bzero(&sin , sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
//参数设置
int bReuseaddr=1;
struct timeval nNetTimeout={0,10000};//10秒
if((l_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("fail to create socket");
exit(1);
}
if(setsockopt(l_fd,SOL_SOCKET ,SO_REUSEADDR,&bReuseaddr,sizeof(int)) == -1)
{
perror("fail to set opt reuseaddr");
exit(1);
}
if(setsockopt(l_fd,SOL_SOCKET ,SO_RCVTIMEO,&nNetTimeout,sizeof(nNetTimeout)) == -1)
{
perror("fail to set opt rcvtimeout");
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)
if((c_fd = accept(l_fd,NULL, 0)) == -1)
{
// perror("fail to accept");
// exit(1);
continue;
}
n = recv(c_fd , buf, MAX_LINE, 0);
if(n == -1)
{
perror("fail to recv");
exit(1);
}
else if(n == 0)
{
printf("the connect has been closed\n");
close(c_fd);
continue;
}
//inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p));
printf("content is : %s\n",buf);
n = strlen(buf);
sprintf(buf,"%d",n);
n = send(c_fd , buf, sizeof(buf) + 1 , 0);
if( n == -1)
{
perror("fail to send");
exit(1);
}
if(close(c_fd) == -1)
{
perror("fail to close");
exit(1);
}
}
if(close(l_fd) == -1)
{
perror("fail to close");
exit(1);
}
return 0;
}
- linux下基于简单socket编程实现C/S
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
69
70
void my_fun(char *p)
{
if(p == NULL)
{
return;
}
for( ; *p != '\0' ; p++)
{
if((*p >= 'a') && (*p <= 'z'))
{
*p = *p - 32;
}
}
return;
}
int main(int argc,char **argv)
{
struct sockaddr_in sin; //服务器通信地址结构
struct sockaddr_in cin; //保存客户端通信地址结构
int l_fd;
int c_fd;
socklen_t len;
char buf[MAX_LINE]; //存储传送内容的缓冲区
char addr_p[INET_ADDR_STR]; //存储客户端地址的缓冲区
int port = 8000;
int n;
bzero((void *)&sin,sizeof(sin));
sin.sin_family = AF_INET; //使用IPV4通信域
sin.sin_addr.s_addr = INADDR_ANY; //服务器可以接受任意地址
sin.sin_port = htons(port); //端口转换为网络字节序
l_fd = socket(AF_INET,SOCK_STREAM,0); //创建套接子,使用TCP协议
bind(l_fd,(struct sockaddr *)&sin,sizeof(sin));
listen(l_fd,10); //开始监听连接
printf("waiting ....\n");
while(1)
{
c_fd = accept(l_fd,(struct sockaddr *)&cin,&len);
n = read(c_fd,buf,MAX_LINE); //读取客户端发送来的信息
inet_ntop(AF_INET,&cin.sin_addr,addr_p,INET_ADDR_STR); //将客户端传来地址转化为字符串
printf("client IP is %s,port is %d\n",addr_p,ntohs(cin.sin_port));
printf("content is : %s\n", buf); //打印客户端发送过来的数据
my_fun(buf);
write(c_fd,buf,n); //转换后发给客户端
close(c_fd);
}
printf("buf = %s\n",buf);
if((close(l_fd)) == -1)
{
perror("fail to close\n");
exit(1);
}
return 0;
}
1 |
|