-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_client.c
More file actions
executable file
·41 lines (35 loc) · 1.1 KB
/
udp_client.c
File metadata and controls
executable file
·41 lines (35 loc) · 1.1 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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int main(int argc, char *argv[])
{
int client_sockfd;
int len;
struct sockaddr_in remote_addr; //服务器端网络地址结构体
int sin_size;
char buf[BUFSIZ]; //数据传送的缓冲区
memset(&remote_addr, 0, sizeof(remote_addr)); //数据初始化--清零
remote_addr.sin_family = AF_INET; //设置为IP通信
remote_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //服务器IP地址
remote_addr.sin_port = htons(8001); //服务器端口号
/* 创建客户端套接字--IPv4协议,面向无连接通信,UDP协议 */
if ( (client_sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket");
return 1;
}
strcpy(buf,"This is a test message");
printf("sending: '%s'\n",buf);
sin_size = sizeof(struct sockaddr_in);
/* 向服务器发送数据包 */
if ( (len = sendto(client_sockfd, buf, strlen(buf), 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr))) < 0 )
{
perror("recvfrom");
return 1;
}
close(client_sockfd);
return 0;
}