-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgreen_ctrl.c
More file actions
49 lines (46 loc) · 1.32 KB
/
green_ctrl.c
File metadata and controls
49 lines (46 loc) · 1.32 KB
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
//=============================================================
// 文件名称:tcpcli.c
// 功能描述:Transfer a TCP package and receive a response
//=============================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // bzero
#include <unistd.h> // getopt
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> // inet_ntop
#include <sys/select.h>
int send_and_recv_via_udp(const char *ip, unsigned short port, const char *sendString, char *recvString)
{
int sockfd; // 套接字
struct sockaddr_in servAddr; // 服务器地址结构体
const char *destIP = ip;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// 创建UDP套接字
if(sockfd < 0)
{
perror("socket");
return -1;
}
bzero(&servAddr, sizeof(servAddr)); // 初始化服务器地址
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(port);
servAddr.sin_addr.s_addr = inet_addr(destIP);
sendto(sockfd, sendString, strlen(sendString), 0, (struct sockaddr *)&servAddr, sizeof(servAddr));
int ret = -1;
do
{
struct timeval to;
to.tv_sec = 0;
to.tv_usec = 1000;
fd_set rset;
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
if(select(sockfd + 1, &rset, NULL, NULL, &to) <= 0)
break;
int len = read(sockfd, recvString, 1024);
if(len > 0)
ret = 0;
} while(0);
return ret;
}