|
server.c
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
| #include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
static void usage(const char* proc)
{
printf("Usage: %s [ip] [port]\n", proc);
}
int main(int argc, char* argv[])
{
if( argc != 3 ){
usage(argv[0]);
return 1;
}
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if( sock < 0 ){
perror("socket");
return 2;
}
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(atoi(argv[2]));
local.sin_addr.s_addr = inet_addr(argv[1]);
if( bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0){
perror("bind");
return 3;
}
int done = 0;
struct sockaddr_in peer;
socklen_t len = sizeof(peer);
char buf[1024];
while( !done ){
memset(buf, '\0', sizeof(buf));
recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)& peer, &len);
printf("#############################################\n");
printf("get a client, socket: %s:%d\n", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port) );
printf("client: %s, echo client!\n", buf);
printf("#############################################\n");
sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&peer,len);
}
return 0;
}
|
client.c
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
| #include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
static void usage(const char* proc)
{
printf("Usage: %s [remote_ip] [remote_port]\n", proc);
}
int main(int argc, char* argv[])
{
if( argc != 3 ){
usage(argv[0]);
return 1;
}
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if( sock < 0 ){
perror("socket");
return 2;
}
struct sockaddr_in remote;
remote.sin_family = AF_INET;
remote.sin_port = htons(atoi(argv[2]));
remote.sin_addr.s_addr = inet_addr(argv[1]);
int done = 0;
char buf[1024];
struct sockaddr_in peer;
socklen_t len = sizeof(peer);
while( !done ){
printf("Please Enter: ");
fflush(stdout);
ssize_t _s = read(0, buf, sizeof(buf)-1);
if( _s > 0 ){
buf[_s-1] = '\0';
sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&remote, sizeof(remote));
memset(buf, '\0', sizeof(buf));
recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&peer, &len);
printf("server echo %s, socket: %s:%d\n", buf, inet_ntoa(peer.sin_addr), ntohs(peer.sin_port) );
}
}
return 0;
}
|
|
|