linux下udp服务器端源码示例:
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
87
88
89
90
91
92
93
94
|
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
int main( int argc, char **argv)
{
printf ( "server ip: %s/n" , argv[1]);
printf ( "server port: %s/n" , argv[2]);
printf ( "/n service starting.../n/n" );
int socketFd;
struct sockaddr_in svrAddr;
struct sockaddr_in localAddr;
socketFd = socket (AF_INET, SOCK_DGRAM, 0);
if ( -1 == socketFd )
{
perror ( "socket:" );
exit (-1);
}
// 设置地址可复用
int option = 1;
setsockopt( socketFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof (option) );
// 客户端IP
memset (&localAddr, 0, sizeof (localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = inet_addr( argv[1]);
localAddr.sin_port = htons ( atoi (argv[2]));
int bindResult = bind(socketFd, ( struct sockaddr *) &localAddr, sizeof (localAddr));
if ( -1 == bindResult )
{
perror ( "bind:" );
close(socketFd);
exit (-1);
}
// 服务器IP
memset (&svrAddr, 0, sizeof (svrAddr));
svrAddr.sin_family = AF_INET;
svrAddr.sin_addr.s_addr = inet_addr( argv[1]);
svrAddr.sin_port = htons ( atoi (argv[2]));
char tempBuff[2048] = {0};
for ( ; ; )
{
// 接收数据
struct sockaddr_in fromAddr;
memset (( char *)&fromAddr, 0, ( int ) sizeof (fromAddr));
socklen_t fromLen = sizeof (fromAddr);
ssize_t result = recvfrom( socketFd, tempBuff, sizeof (tempBuff), 0, ( struct sockaddr *)&fromAddr, &fromLen);
if ( -1 == result )
{
perror ( "recvfrom:" );
continue ;
}
else
{
printf ( "recv data %s successed. data len: %d/n" , inet_ntoa(fromAddr.sin_addr), result );
printf ( "data:/n" );
for ( int i = 0; i < result; i++ )
{
printf ( "%02x " , tempBuff[i] );
if ( (i+1)%16 == 0 )
{
printf ( "/n" );
}
}
printf ( "/n" );
}
// 发送数据
result = sendto( socketFd, tempBuff, result, 0, ( struct sockaddr*)&fromAddr, sizeof (fromAddr));
if ( -1 == result )
{
perror ( "sendto:" );
}
else
{
printf ( "send data success. data len:%d/n" , result );
}
sleep( 60 );
}
close(socketFd);
}
|
linux下udp客户端源码示例:
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
typedef struct _NSS_HEADER
{
unsigned short ProtocolVersion; /* 协议版本信息 */
unsigned short MsgType; /* 消息类型 */
unsigned short TransactionNo; /* 传输编号 */
unsigned short PacketLength; /* 数据包长度,包括Header */
}NSS_HEADER;
int main( int argc, char **argv)
{
printf ( "client ip: %s/n" , argv[1]);
printf ( "client port: %s/n" , argv[2]);
printf ( "server ip: %s/n" , argv[3]);
printf ( "server port: %s/n" , argv[4]);
printf ( "/n service starting.../n/n" );
int socketFd;
struct sockaddr_in svrAddr;
struct sockaddr_in localAddr;
socketFd = socket (AF_INET, SOCK_DGRAM, 0);
if ( -1 == socketFd )
{
perror ( "socket:" );
exit (-1);
}
// 设置地址可复用
int option = 1;
setsockopt( socketFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof (option) );
// 客户端IP
memset (&localAddr, 0, sizeof (localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = inet_addr( argv[1]);
localAddr.sin_port = htons ( atoi (argv[2]));
int bindResult = bind(socketFd, ( struct sockaddr *) &localAddr, sizeof (localAddr));
if ( -1 == bindResult )
{
perror ( "bind:" );
sleep(10);
close(socketFd);
exit (-1);
}
// 服务器IP
memset (&svrAddr, 0, sizeof (svrAddr));
svrAddr.sin_family = AF_INET;
svrAddr.sin_addr.s_addr = inet_addr( argv[3]);
svrAddr.sin_port = htons ( atoi (argv[4]));
char tempBuff[2048] = {0};
// 每分钟发送一次数据
NSS_HEADER hdr;
memset ( &hdr, 0, sizeof (NSS_HEADER));
for ( ; ; )
{
// 发送数据
hdr.TransactionNo++;
ssize_t result = sendto( socketFd, &hdr, sizeof (NSS_HEADER), 0, ( struct sockaddr*)&svrAddr, sizeof (svrAddr));
if ( -1 == result )
{
perror ( "sendto:" );
}
else
{
printf ( "send data success. data len:%d/n" , result );
}
// 接收数据
struct sockaddr_in fromAddr;
memset (( char *)&fromAddr, 0, ( int ) sizeof (fromAddr));
socklen_t fromLen = sizeof (fromAddr);
result = recvfrom( socketFd, tempBuff, sizeof (tempBuff), 0, ( struct sockaddr *)&fromAddr, &fromLen);
if ( -1 == result )
{
perror ( "recvfrom:" );
}
else
{
printf ( "recv data %s successed. data len: %d/n" , inet_ntoa(fromAddr.sin_addr), result );
printf ( "data:/n" );
for ( int i = 0; i < result; i++ )
{
printf ( "%02x " , tempBuff[i] );
if ( (i+1)%16 == 0 )
{
printf ( "/n" );
}
}
printf ( "/n" );
}
sleep( 60 );
}
close(socketFd);
}
|