电子说
所需要头文件 :
#include
#include
函数格式 :
int connect (int sockfd, struct sockaddr *serv_addr, int addrlen);
函数功能 :
用来请求连接远程服务器,将参数sockfd的socket连至参数serv_addr所指定的服务器IP和端口号上去;
sockfd:客户端的socket套接字
serv_addr:一个struct sockaddr类型的结构体指针变量,存储着远程服务器的IP与端口号信息
addrlen:结构体变量的长度
返回值 :
若成功,返回0,否则返回-1,错误信息存在errno中;
d. 发送
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
函数格式 :
int send(int sockfd, const void *buf, int len, int flags);
函数功能 :
用来发送数据给指定的远端主机;
sockfd:客户端的socket套接字
buf:指明一个缓冲区
len:指明缓冲区的长度
flags:通常为0
返回值 :
若成功,返回发送的字节数,否则返回-1,错误信息存在errno中
d. 关闭socket
1 close(sock_fd);
2 exit(0);
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}
先在Ubuntu主机上交叉编译服务器端代码,再在Ubuntu主机上编译客户端代码。
在开发板上运行服务器端代码,在Ubuntu主机先启动tmux分屏,再分别运行客户端代码。
全部0条评论
快来发表一下你的评论吧 !