-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcps.c
84 lines (76 loc) · 2.08 KB
/
tcps.c
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
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#define MYPORT 8888
#define QUEUE 20
#define BUFFER_SIZE 1024
int main()
{
///定义sockfd
int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);
printf("socket success, sockfd = %d\n", server_sockfd);
///定义sockaddr_in
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(MYPORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
///bind,成功返回0,出错返回-1
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
{
perror("bind");
exit(1);
}
puts("bind success");
///listen,成功返回0,出错返回-1
if(listen(server_sockfd,QUEUE) == -1)
{
perror("listen");
exit(1);
}
puts("listen success");
///客户端套接字
char buffer[BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
///成功返回非负描述字,出错返回-1
int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);
if(conn<0)
{
perror("connect");
exit(1);
}
puts("accept success");
char sendbuf[BUFFER_SIZE];//
int i;
while(1)
{
memset(buffer,0,sizeof(buffer));
int len = recv(conn, buffer, sizeof(buffer),0);
if( len <= 0 )
{
puts("disconnect");
break;
}
printf("\nrecv data length %d :", len);
if(strcmp(buffer,"exit\n")==0)
break;
fputs(buffer, stdout);
for( i=0; i<len; i++)
{
sendbuf[i] = buffer[i] <= 'z' && buffer[i] >= 'a' ? sendbuf[i] = buffer[i] - 'a' + 'A' : buffer[i];
}
//sendbuf[i] = '\0';
send(conn, sendbuf, len, 0);
memset(buffer,0,sizeof(sendbuf));
}
close(conn);
close(server_sockfd);
return 0;
}