一些服务器客户端的c例子

简介:

今天早上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);
}
目录
相关文章
|
13天前
|
网络协议 Python
pythonTCP客户端编程连接服务器
【4月更文挑战第6天】本教程介绍了TCP客户端如何连接服务器,包括指定服务器IP和端口、发送连接请求、处理异常、进行数据传输及关闭连接。在Python中,使用`socket`模块创建Socket对象,然后通过`connect()`方法尝试连接服务器 `(server_ip, server_port)`。成功连接后,利用`send()`和`recv()`进行数据交互,记得在通信完成后调用`close()`关闭连接,确保资源释放和程序稳定性。
|
3月前
|
SQL 测试技术 API
服务器端测试
服务器端测试
服务器端测试
|
7月前
|
存储 Java
WebSocket区分不同客户端方法
WebSocket区分不同客户端方法
286 0
|
SQL 安全 前端开发
服务端测试
大家好,我是阿萨。前几天我们讲了你了解客户端测试吗?和客户端对应的就是服务器端了。那么服务器端如何测试呢?服务器端相当于一个黑盒子,我们能接触到的只有前台向后端发送的API请求。所以服务器端测试也可以理解为API测试。
201 0
通过nc命令模拟客户端或服务器端程序
通过nc命令模拟客户端或服务器端程序
253 0
|
安全 Java C#
【知识积累】服务器端获取客户端的IP地址(当客户端调用由Axis开发的WebService)
由于项目中一个小的模块需要获取客户端的IP地址以保证安全调用webservice接口,项目中客户端使用C#编写,服务器端使用Java编写,服务器端与客户端采用Axis开发的WebService进行通信。服务器端维护IP白名单列表,只有IP地址在白名单中的客户端才可以成功调用到接口,获得服务。
225 0
|
测试技术 C# Python
|
Web App开发
一个用OWL-S组装Web服务的例子
OWL-S可以用来描述Web服务,这个帖子将介绍一个非常简单的例子,也许对理解Web服务的组装有些作用。这个服务是对已有Web服务进行组装和执行,所以你并不需要发布自己的Web服务。你需要安装Protege和OWL-S Editor插件,我用的版本前者是3.1 beta build 191,后者是build 15,它们在一起运行得还不错。
1162 0