1、服务器程序
复制代码
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#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> #include <signal.h> /* * socket * bind * listen * accept * send/recv */ #define SERVER_PORT 8888 #define BACKLOG 10 /* ./server ip */ int main(int argc, char **argv) { int iRet; int iRecvLen; int iSendLen; int iSocketServer; int iSocketClient; int iAddrLen; unsigned char ucRecvByte[1000]; unsigned char ucSendByte[1000]; struct sockaddr_in tSocketServerAddr; struct sockaddr_in tSocketClientAddr; int iClientNum = -1; signal(SIGCHLD,SIG_IGN); iSocketServer = socket(AF_INET, SOCK_STREAM, 0); tSocketServerAddr.sin_family = AF_INET; tSocketServerAddr.sin_port = htons(SERVER_PORT); tSocketServerAddr.sin_addr.s_addr = INADDR_ANY; memset(tSocketServerAddr.sin_zero, 0, 8); iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if(iRet == -1) { printf("server bind errorn"); return -1; } iRet = listen(iSocketServer, BACKLOG); if (-1 == iRet) { printf("listen error!n"); return -1; } while(1) { iAddrLen = sizeof(struct sockaddr); iSocketClient = accept(iSocketServer, (struct sockaddr *)&tSocketClientAddr, &iAddrLen); if(iSocketClient != -1) { iClientNum++; printf("Get connect from client %d : %sn", iClientNum, inet_ntoa(tSocketClientAddr.sin_addr)); if(fork() == 0) { while(1) { iRecvLen = recv(iSocketClient, ucRecvByte, 999, 0); if(iRecvLen <= 0) { close(iSocketClient); return -1; } else { ucRecvByte[iRecvLen] = ''; printf("Get Msg From Client %d: %sn", iClientNum, ucRecvByte); strcpy(ucSendByte, "have recv"); iSendLen = send(iSocketClient, ucSendByte, strlen(ucSendByte), 0); } } } } } close(iSocketServer); return 0; }
2、客服端程序
复制代码
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#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> #define SERVER_PORT 8888 /* * socket * connect * recv */ /* ./client ip */ int main(int argc, char **argv) { int iRet; int iSocketClient; int iSendLen; int iRecvLen; unsigned char ucByte[1000]; unsigned char ucRecvByte[1000]; struct sockaddr_in tSocketServerAddr; if(argc != 2) { printf("%s <server_ip>n", argv[0]); return -1; } iSocketClient = socket(AF_INET, SOCK_STREAM, 0); tSocketServerAddr.sin_family = AF_INET; tSocketServerAddr.sin_port = htons(SERVER_PORT); if(0 == inet_aton(argv[1], &tSocketServerAddr.sin_addr)) { printf("invalid ip addrn"); return -1; } memset(tSocketServerAddr.sin_zero, 0, 8); iRet = connect(iSocketClient, (struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); if(-1 == iRet) { printf("client connect errorn"); return -1; } while(1) { if(fgets(ucByte, 999, stdin)) { iSendLen = send(iSocketClient, ucByte, strlen(ucByte), 0); if(iSendLen <= 0) { close(iSocketClient); return -1; } iRecvLen = recv(iSocketClient, ucRecvByte, 999, 0); ucRecvByte[iRecvLen] = ''; printf("client recv %sn", ucRecvByte); } } return 0; }
最后
以上就是无限小笼包最近收集整理的关于TCP/IP协议实现客服端与服务器的双向发送的全部内容,更多相关TCP/IP协议实现客服端与服务器内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复