所需要頭文件 :
#include
#include
函數(shù)格式 :
int connect (int sockfd, struct sockaddr *serv_addr, int addrlen);
函數(shù)功能 :
用來(lái)請(qǐng)求連接遠(yuǎn)程服務(wù)器,將參數(shù)sockfd的socket連至參數(shù)serv_addr所指定的服務(wù)器IP和端口號(hào)上去;
sockfd:客戶端的socket套接字
serv_addr:一個(gè)struct sockaddr類型的結(jié)構(gòu)體指針變量,存儲(chǔ)著遠(yuǎn)程服務(wù)器的IP與端口號(hào)信息
addrlen:結(jié)構(gòu)體變量的長(zhǎng)度
返回值 :
若成功,返回0,否則返回-1,錯(cuò)誤信息存在errno中;
d. 發(fā)送
1 send_buf = send(sock_fd, send_buf, strlen(send_buf), 0);
2 if (send_buf <= 0)
3 {
4 fprintf(stderr,"send error:%s\\n\\a", strerror(errno));
5 close(sock_fd);
6 exit(1);
7 }
所需要頭文件 :
#include
#include
函數(shù)格式 :
int send(int sockfd, const void *buf, int len, int flags);
函數(shù)功能 :
用來(lái)發(fā)送數(shù)據(jù)給指定的遠(yuǎn)端主機(jī);
sockfd:客戶端的socket套接字
buf:指明一個(gè)緩沖區(qū)
len:指明緩沖區(qū)的長(zhǎng)度
flags:通常為0
返回值 :
若成功,返回發(fā)送的字節(jié)數(shù),否則返回-1,錯(cuò)誤信息存在errno中
d. 關(guān)閉socket
1 close(sock_fd);
2 exit(0);
3.3 TCP完整代碼
1/*
2* tcp_server.c
3# Copyright (C) 2017 hceng,
9#include
10#include
11#include
12#include
13#include
14#include
15#include
16#include
17#include
18#include
19#include
20
21#define PORT_NUMBER 8888
22#define BACKLOG 10
23
24/* socket->bind->listen->accept->send/recv->close*/
25
26int main(int argc, char **argv)
27{
28 int sock_fd, new_fd;
29 struct sockaddr_in server_addr;
30 struct sockaddr_in client_addr;
31 int ret;
32 int addr_len;
33 int recv_len;
34 unsigned char recv_buf[1000];
35 int client_num = -1;
36
37 signal(SIGCHLD,SIG_IGN);
38
39 /* socket */
40 sock_fd = socket(AF_INET, SOCK_STREAM, 0);//AF_INET:IPV4;SOCK_STREAM:TCP
41 if (-1 == sock_fd)
42 {
43 fprintf(stderr,"socket error:%s\\n\\a", strerror(errno));
44 exit(1);
45 }
46
47 /* set server sockaddr_in */
48 memset(&server_addr, 0, sizeof(struct sockaddr_in));//clear
49 server_addr.sin_family = AF_INET;
50 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//INADDR_ANY:This machine all IP
51 server_addr.sin_port = htons(PORT_NUMBER);
52
53 /* bind */
54 ret = bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr));
55 if(-1 == ret)
56 {
57 fprintf(stderr,"bind error:%s\\n\\a", strerror(errno));
58 close(sock_fd);
59 exit(1);
60 }
61
62 /* listen */
63 ret = listen(sock_fd, BACKLOG);
64 if (-1 == ret)
65 {
66 fprintf(stderr,"listen error:%s\\n\\a", strerror(errno));
67 close(sock_fd);
68 exit(1);
69 }
70
71 /* accept */
72 while(1)
73 {
74 addr_len = sizeof(struct sockaddr);
75 new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &addr_len);
76 if (-1 == new_fd)
77 {
78 fprintf(stderr,"accept error:%s\\n\\a", strerror(errno));
79 close(sock_fd);
80 exit(1);
81 }
82
83 client_num++;
84 fprintf(stderr, "Server get connetion form client%d: %s\\n", client_num, inet_ntoa(client_addr.sin_addr));
85
86 if (!fork())
87 {
88 /* Child process */
89 while (1)
90 {
91 /* recv */
92 recv_len = recv(new_fd, recv_buf, 999, 0);
93 if (recv_len <= 0)
94 {
95 fprintf(stderr, "recv error:%s\\n\\a", strerror(errno));
96 close(new_fd);
97 exit(1);
98 }
99 else
100 {
101 recv_buf[recv_len] = '\\0';
102 printf("Get msg from client%d: %s\\n", client_num, recv_buf);
103 }
104 }
105 close(new_fd);
106 }
107 }
108
109 /* close */
110 close(sock_fd);
111 exit(0);
112}
1/*
2* tcp_client.c
3# Copyright (C) 2017 hceng,
9#include
10#include
11#include
12#include
13#include
14#include
15#include
16#include
17#include
18#include
19
20#define PORT_NUMBER 8888
21
22/* socket->connect->send->close*/
23int main(int argc, char *argv[])
24{
25 int sock_fd;
26 struct sockaddr_in server_addr;
27 int ret;
28 unsigned char send_buf[1000];
29 int send_len;
30
31 if(argc != 2)
32 {
33 fprintf(stderr, "Usage:%s hostname\\n\\a", argv[0]);
34 exit(1);
35 }
36
37 /* socket */
38 sock_fd = socket(AF_INET, SOCK_STREAM, 0);//AF_INET:IPV4;SOCK_STREAM:TCP
39 if (-1 == sock_fd)
40 {
41 fprintf(stderr,"socket error:%s\\n\\a", strerror(errno));
42 exit(1);
43 }
44
45 /* set sockaddr_in parameter*/
46 memset(&server_addr, 0, sizeof(struct sockaddr_in));//clear
47 server_addr.sin_family = AF_INET;
48 server_addr.sin_port = htons(PORT_NUMBER);
49 ret = inet_aton(argv[1], &server_addr.sin_addr);
50 if(0 == ret)
51 {
52 fprintf(stderr,"server_ip error.\\n");
53 close(sock_fd);
54 exit(1);
55 }
56
57 /* connect */
58 ret = connect(sock_fd, (const struct sockaddr *)&server_addr, sizeof(struct sockaddr));
59 if (-1 == ret)
60 {
61 fprintf(stderr,"connect error:%s\\n\\a", strerror(errno));
62 close(sock_fd);
63 exit(1);
64 }
65
66 while (1)
67 {
68 if (fgets(send_buf, 999, stdin))
69 {
70 /* send */
71 send_len = send(sock_fd, send_buf, strlen(send_buf), 0);
72 if (send_len <= 0)
73 {
74 fprintf(stderr,"send error:%s\\n\\a", strerror(errno));
75 close(sock_fd);
76 exit(1);
77 }
78 }
79 }
80
81 /* close */
82 close(sock_fd);
83 exit(0);
84}
3.4 測(cè)試結(jié)果
先在Ubuntu主機(jī)上交叉編譯服務(wù)器端代碼,再在Ubuntu主機(jī)上編譯客戶端代碼。
在開(kāi)發(fā)板上運(yùn)行服務(wù)器端代碼,在Ubuntu主機(jī)先啟動(dòng)tmux分屏,再分別運(yùn)行客戶端代碼。
- 服務(wù)器端
- 客戶機(jī)端
-
TCP
+關(guān)注
關(guān)注
8文章
1353瀏覽量
79055 -
UDP
+關(guān)注
關(guān)注
0文章
325瀏覽量
33931 -
網(wǎng)絡(luò)通信
+關(guān)注
關(guān)注
4文章
797瀏覽量
29795 -
網(wǎng)絡(luò)編程
+關(guān)注
關(guān)注
0文章
71瀏覽量
10074
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論