博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一些服务器客户端的c例子
阅读量:7210 次
发布时间:2019-06-29

本文共 9313 字,大约阅读时间需要 31 分钟。

今天早上6点起床之后练习的一些c的网络编程的基础例子

client1

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
/*  Make the necessary includes and set up the variables.  */
 
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
 
int 
main()
{
    
int 
sockfd;
    
int 
len;
    
struct 
sockaddr_un address;
    
int 
result;
    
char 
ch = 
'A'
;
 
/*  Create a socket for the client.  */
 
    
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
 
/*  Name the socket, as agreed with the server.  */
 
    
address.sun_family = AF_UNIX;
    
strcpy
(address.sun_path, 
"server_socket"
);
    
len = 
sizeof
(address);
 
/*  Now connect our socket to the server's socket.  */
 
    
result = connect(sockfd, (
struct 
sockaddr *)&address, len);
 
    
if
(result == -1) {
        
perror
(
"oops: client1"
);
        
exit
(1);
    
}
 
/*  We can now read/write via sockfd.  */
 
    
write(sockfd, &ch, 1);
    
read(sockfd, &ch, 1);
    
printf
(
"char from server = %c\n"
, ch);
    
close(sockfd);
    
exit
(0);
}

  server1.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
/*  Make the necessary includes and set up the variables.  */
 
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
 
int 
main()
{
    
int 
server_sockfd, client_sockfd;
    
int 
server_len, client_len;
    
struct 
sockaddr_un server_address;
    
struct 
sockaddr_un client_address;
 
/*  Remove any old socket and create an unnamed socket for the server.  */
 
    
unlink(
"server_socket"
);
    
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
 
/*  Name the socket.  */
 
    
server_address.sun_family = AF_UNIX;
    
strcpy
(server_address.sun_path, 
"server_socket"
);
    
server_len = 
sizeof
(server_address);
    
bind(server_sockfd, (
struct 
sockaddr *)&server_address, server_len);
 
/*  Create a connection queue and wait for clients.  */
 
    
listen(server_sockfd, 5);
    
while
(1) {
        
char 
ch;
 
        
printf
(
"server waiting\n"
);
 
/*  Accept a connection.  */
 
        
client_len = 
sizeof
(client_address);
        
client_sockfd = accept(server_sockfd,
            
(
struct 
sockaddr *)&client_address, &client_len);
 
/*  We can now read/write to client on client_sockfd.  */
 
        
read(client_sockfd, &ch, 1);
        
ch++;
        
write(client_sockfd, &ch, 1);
        
close(client_sockfd);
    
}
}

  第二个服务器客户端的例子:

client

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
/*  Make the necessary includes and set up the variables.  */
 
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
 
int 
main()
{
    
int 
sockfd;
    
int 
len;
    
struct 
sockaddr_in address;
    
int 
result;
    
char 
ch = 
'A'
;
 
/*  Create a socket for the client.  */
 
    
sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
/*  Name the socket, as agreed with the server.  */
 
    
address.sin_family = AF_INET;
    
address.sin_addr.s_addr = inet_addr(
"127.0.0.1"
);
    
address.sin_port = htons(9734);
    
len = 
sizeof
(address);
 
/*  Now connect our socket to the server's socket.  */
 
    
result = connect(sockfd, (
struct 
sockaddr *)&address, len);
 
    
if
(result == -1) {
        
perror
(
"oops: client3"
);
        
exit
(1);
    
}
 
/*  We can now read/write via sockfd.  */
 
    
write(sockfd, &ch, 1);
    
read(sockfd, &ch, 1);
    
printf
(
"char from server = %c\n"
, ch);
    
close(sockfd);
    
exit
(0);
}

  server

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
/*  Make the necessary includes and set up the variables.  */
 
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
 
int 
main()
{
    
int 
server_sockfd, client_sockfd;
    
int 
server_len, client_len;
    
struct 
sockaddr_in server_address;
    
struct 
sockaddr_in client_address;
 
/*  Remove any old socket and create an unnamed socket for the server.  */
 
    
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
/*  Name the socket.  */
 
    
server_address.sin_family = AF_INET;
    
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    
server_address.sin_port = htons(9734);
    
server_len = 
sizeof
(server_address);
    
bind(server_sockfd, (
struct 
sockaddr *)&server_address, server_len);
 
/*  Create a connection queue and wait for clients.  */
 
    
listen(server_sockfd, 5);
    
while
(1) {
        
char 
ch;
 
        
printf
(
"server waiting\n"
);
 
/*  Accept a connection.  */
 
        
client_len = 
sizeof
(client_address);
        
client_sockfd = accept(server_sockfd,
            
(
struct 
sockaddr *)&client_address, &client_len);
 
/*  We can now read/write to client on client_sockfd.  */
 
        
read(client_sockfd, &ch, 1);
        
ch++;
        
write(client_sockfd, &ch, 1);
        
close(client_sockfd);
    
}
}

  

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
/*  As usual, make the appropriate includes and declare the variables.  */
 
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
 
int 
main(
int 
argc, 
char 
*argv[])
{
    
char 
*host, **names, **addrs;
    
struct 
hostent *hostinfo;
 
/*  Set the host in question to the argument supplied with the getname call,
    
or default to the user's machine.  */
 
    
if
(argc == 1) {
        
char 
myname[256];
        
gethostname(myname, 255);
        
host = myname;
    
}
    
else
        
host = argv[1];
 
/*  Make the call to gethostbyname and report an error if no information is found.  */
 
    
hostinfo = gethostbyname(host);
    
if
(!hostinfo) {
        
fprintf
(stderr, 
"cannot get info for host: %s\n"
, host);
        
exit
(1);
    
}
 
/*  Display the hostname and any aliases it may have.  */
 
    
printf
(
"results for host %s:\n"
, host);
    
printf
(
"Name: %s\n"
, hostinfo -> h_name);
    
printf
(
"Aliases:"
);
    
names = hostinfo -> h_aliases;
    
while
(*names) {
        
printf
(
" %s"
, *names);
        
names++;
    
}
    
printf
(
"\n"
);
 
/*  Warn and exit if the host in question isn't an IP host.  */
 
    
if
(hostinfo -> h_addrtype != AF_INET) {
        
fprintf
(stderr, 
"not an IP host!\n"
);
        
exit
(1);
    
}
 
/*  Otherwise, display the IP address(es).  */
 
    
addrs = hostinfo -> h_addr_list;
    
while
(*addrs) {
        
printf
(
" %s"
, inet_ntoa(*(
struct 
in_addr *)*addrs));
        
addrs++;
    
}
    
printf
(
"\n"
);
    
exit
(0);
}

  

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
/*  Start with the usual includes and declarations.  */
 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
int 
main(
int 
argc, 
char 
*argv[])
{
    
char 
*host;
    
int 
sockfd;
    
int 
len, result;
    
struct 
sockaddr_in address;
    
struct 
hostent *hostinfo;
    
struct 
servent *servinfo;
    
char 
buffer[128];
 
    
if
(argc == 1)
        
host = 
"localhost"
;
    
else
        
host = argv[1];
 
/*  Find the host address and report an error if none is found.  */
 
    
hostinfo = gethostbyname(host);
    
if
(!hostinfo) {
        
fprintf
(stderr, 
"no host: %s\n"
, host);
        
exit
(1);
    
}
 
/*  Check that the daytime service exists on the host.  */
 
    
servinfo = getservbyname(
"daytime"
"tcp"
);
    
if
(!servinfo) {
        
fprintf
(stderr,
"no daytime service\n"
);
        
exit
(1);
    
}
    
printf
(
"daytime port is %d\n"
, ntohs(servinfo -> s_port));
 
/*  Create a socket.  */
 
    
sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
/*  Construct the address for use with connect...  */
 
    
address.sin_family = AF_INET;
    
address.sin_port = servinfo -> s_port;
    
address.sin_addr = *(
struct 
in_addr *)*hostinfo -> h_addr_list;
    
len = 
sizeof
(address);
 
/*  ...then connect and get the information.  */
 
    
result = connect(sockfd, (
struct 
sockaddr *)&address, len);
    
if
(result == -1) {
        
perror
(
"oops: getdate"
);
        
exit
(1);
    
}
 
    
result = read(sockfd, buffer, 
sizeof
(buffer));
    
buffer[result] = 
'\0'
;
    
printf
(
"read %d bytes: %s"
, result, buffer);
 
    
close(sockfd);
    
exit
(0);
}

  getdate-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
/*  Start with the usual includes and declarations.  */
 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
int 
main(
int 
argc, 
char 
*argv[])
{
    
char 
*host;
    
int 
sockfd;
    
int 
len, result;
    
struct 
sockaddr_in address;
    
struct 
hostent *hostinfo;
    
struct 
servent *servinfo;
    
char 
buffer[128];
 
    
if
(argc == 1)
        
host = 
"localhost"
;
    
else
        
host = argv[1];
 
/*  Find the host address and report an error if none is found.  */
 
    
hostinfo = gethostbyname(host);
    
if
(!hostinfo) {
        
fprintf
(stderr, 
"no host: %s\n"
, host);
        
exit
(1);
    
}
 
/*  Check that the daytime service exists on the host.  */
 
    
servinfo = getservbyname(
"daytime"
"udp"
);
    
if
(!servinfo) {
        
fprintf
(stderr,
"no daytime service\n"
);
        
exit
(1);
    
}
    
printf
(
"daytime port is %d\n"
, ntohs(servinfo -> s_port));
 
/*  Create a UDP socket.  */
 
    
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 
/*  Construct the address for use with sendto/recvfrom...  */
 
    
address.sin_family = AF_INET;
    
address.sin_port = servinfo -> s_port;
    
address.sin_addr = *(
struct 
in_addr *)*hostinfo -> h_addr_list;
    
len = 
sizeof
(address);
 
    
result = sendto(sockfd, buffer, 1, 0, (
struct 
sockaddr *)&address, len);
    
result = recvfrom(sockfd, buffer, 
sizeof
(buffer), 0, (
struct 
sockaddr *)&address, &len);
    
buffer[result] = 
'\0'
;
    
printf
(
"read %d bytes: %s"
, result, buffer);
 
    
close(sockfd);
    
exit
(0);
}

转载地址:http://rzwym.baihongyu.com/

你可能感兴趣的文章
C++(实验六)
查看>>
EBS后台取消死锁检查代码和取消死锁会话步骤---经验
查看>>
[置顶] Gridview中弹出层前台取值避免了刷新,easyui+Jquery
查看>>
Oracle BIEE11G --- ADF_IFRAME
查看>>
Java 连接数据库
查看>>
部分 TCP 参数简介
查看>>
[转]java annotation 手册
查看>>
不安装oracle客户端也可以使用pl/sql developer
查看>>
4、在Shell程序中的使用变量
查看>>
AndroidのListView之滑动列表项(点击事件和滑动事件共存)
查看>>
pygtk手记(1)
查看>>
YOUYOU深入学习Ganglia之三(gmetad的软件架构)
查看>>
poj1483 It's not a Bug, It's a Feature!
查看>>
ESET Smart Security 6 – 免费60天(SG)
查看>>
Coursera Machine Leaning 课程总结
查看>>
js 控制div 显示隐藏的问题
查看>>
execute、executeQuery和executeUpdate之间的区别
查看>>
类继承已经过时了
查看>>
Objective-C在windows开发环境的搭建
查看>>
用正则表达式提取字符串中的字符(包含数字)
查看>>