码迷,mamicode.com
首页 > 系统相关 > 详细

linux_c_socket_example

时间:2019-10-21 16:27:19      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:sock   ddr   sage   技术   ons   NPU   oid   stderr   strlen   

server

技术图片
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <arpa/inet.h>
 6 #include <sys/socket.h>
 7 
 8 
 9 #define BUF_SIZE 1024
10 
11 void err_handler(char *msg);
12 
13 int main(int argc, char **argv) {
14     int srv_sock, cli_sock;
15     char msg[BUF_SIZE];
16     int str_len;
17 
18     struct sockaddr_in srv_addr, cli_addr;
19     socklen_t cli_adr_sz;
20 
21     if (argc != 2) {
22         printf("Usage : %s <port>", argv[0]);
23         exit(1);
24     }
25 
26     srv_sock = socket(PF_INET, SOCK_STREAM, 0);
27     if (srv_sock == -1) {
28         err_handler("socket() error");
29     }
30 
31     memset(&srv_addr, 0, sizeof(srv_addr));
32     srv_addr.sin_family = AF_INET;
33     srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
34     srv_addr.sin_port = htons(atoi(argv[1]));
35 
36     if (bind(srv_sock, (struct sockaddr *) &srv_addr, sizeof(srv_addr)) == -1) {
37         err_handler("bind() error");
38     }
39 
40     if (listen(srv_sock, 5) == -1) {
41         err_handler("listen() error");
42     }
43 
44     cli_adr_sz = sizeof(cli_addr);
45 
46     for (int i = 0; i < 5; ++i) {
47         cli_sock = accept(srv_sock, (struct sockaddr *) &cli_addr, &cli_adr_sz);
48         if (cli_sock == -1) {
49             err_handler("accept() error");
50         } else {
51             printf("connected client %d\n", i + i);
52         }
53 
54         while ((str_len = read(cli_sock, msg, BUF_SIZE)) != 0) {
55             write(cli_sock, msg, str_len);
56         }
57         close(cli_sock);
58     }
59 
60     close(srv_sock);
61     return 0;
62 }
63 
64 void err_handler(char *msg) {
65     fputs(msg, stderr);
66     fputc(\n, stderr);
67     exit(1);
68 }
View Code

client

技术图片
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <arpa/inet.h>
 6 #include <sys/socket.h>
 7 
 8 
 9 #define BUF_SIZE 1024
10 
11 void err_handler(char *msg);
12 
13 int main(int argc, char **argv) {
14     int sock;
15     char msg[BUF_SIZE];
16     int str_len;
17     struct sockaddr_in srv_addr;
18 
19     if (argc != 3) {
20         printf("Usage : %s <port>", argv[0]);
21         exit(1);
22     }
23 
24     sock = socket(PF_INET, SOCK_STREAM, 0);
25     if (sock == -1) {
26         err_handler("socket() error");
27     }
28 
29     memset(&srv_addr, 0, sizeof(srv_addr));
30     srv_addr.sin_family = AF_INET;
31     srv_addr.sin_addr.s_addr = inet_addr(argv[1]);
32     srv_addr.sin_port = htons(atoi(argv[2]));
33 
34     if (connect(sock, (struct sockaddr *) &srv_addr, sizeof(srv_addr)) == -1) {
35         err_handler("connect() error");
36     } else {
37         printf("connected .........");
38     }
39 
40     while (1) {
41         fputs("Input msg(Q to quit):", stdout);
42         fgets(msg, BUF_SIZE, stdin);
43         if (!strcmp(msg, "q\n") || !strcmp(msg, "Q\n")) {
44             break;
45         }
46         write(sock, msg, strlen(msg));
47         str_len = read(sock, msg, BUF_SIZE - 1);
48         msg[str_len] = 0;
49         printf("Msg form srv : %s", msg);
50     }
51 
52     close(sock);
53     return 0;
54 }
55 
56 void err_handler(char *msg) {
57     fputs(msg, stderr);
58     fputc(\n, stderr);
59     exit(1);
60 }
View Code

Makefile

all a_b:
    gcc -o srv main.c -g -Wall
    gcc -o cli client.c -g -Wall

 

linux_c_socket_example

标签:sock   ddr   sage   技术   ons   NPU   oid   stderr   strlen   

原文地址:https://www.cnblogs.com/--just-lbk/p/11713827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!