赞
踩
- "server"
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <ctype.h>
- #include <string.h>
-
-
- typedef struct sockaddr SA;
- typedef struct sockaddr_in SA4;
- int main (void)
- {
- SA4 serv, cli; // 定义服务器地址变量,客户端地址变量
- int s_fd, conn_fd;
- int ret = 0;
- char buf[1024] = {0}, buf1[1024] = {0};
- char ip[128] = {0}; // 存放ip
- int port; // 存放端口
- /* 1. 创建socket通讯端 */
- s_fd = socket (AF_INET, SOCK_STREAM, 0);
- if (-1 == s_fd) {
- perror ("socket");
- return 1;
- }
- /* 2. 初始化服务器信息 */
- serv.sin_family = AF_INET; // ipv4
- serv.sin_port = htons (7778); // port
- serv.sin_addr.s_addr = htonl (INADDR_ANY); // 32bit long
- /* 3. 将服务器ip地址和端口号与s_fd绑定 */
- ret = bind (s_fd, (SA *)&serv, sizeof (serv));
- if (-1 == ret) {
- perror ("bind");
- return 2;
- }
- /* 4. 监听s_fd */
- listen (s_fd, 10); // 5个设备同时
- /* 5. 阻塞等待客户端的连接 */
- while (1) {
- /* 将客户端的通讯对保存在了cli指向的地址里 */
- int cli_len = sizeof (cli);
- conn_fd = accept (s_fd, (SA*)&cli, &cli_len);
- /* 将二进制ip转换为字符串ip地址打印 */
- char *p = (char*)inet_ntop (AF_INET, &cli.sin_addr, ip, 128);
- printf ("客户端ip:%s\n", p);
- printf ("端口号:%d\n", ntohs (cli.sin_port));
- if (-1 == conn_fd) {
- perror ("accept");
- return 3;
- }
- /* 6. 读取客户端发送过来的信息 */
- int r = read (conn_fd, buf, sizeof (buf)-1);
- /* 7. 数据处理/显示 */
- write (1, buf, r);
- // write (1, "\n", 2);
- memset (buf, 0, sizeof (buf));
- /* 8. 给客户端响应 */
- fgets (buf1, sizeof (buf1), stdin);
- write (conn_fd, buf1, sizeof (buf1));
- memset (buf1, 0, sizeof (buf1));
- }
- /* 9. 连接结束,关闭连接 */
- close (conn_fd);
- return 0;
- }
- "client"
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <string.h>
-
-
- typedef struct sockaddr SA;
- typedef struct sockaddr_in SA4;
- int main (int argc, char *argv[])
- {
- SA4 server;
- int s_fd;
- int ret;
- char buf[1024] = {0}, buf1[1024] = {0};
- /* 1. 创建通讯端 */
- s_fd = socket (AF_INET, SOCK_STREAM, 0);
- if (-1 == s_fd) {
- perror ("socket");
- return 1;
- }
- /* 2. 初始化服务器端信息 */
- server.sin_family = AF_INET; // ipv4
- server.sin_port = htons (7778); // port
- inet_pton (AF_INET, argv[1], &server.sin_addr); // copy ip 到服务器
- /* 3. 将s_fd和目的地址连接 */
- ret = connect (s_fd, (SA*)&server, sizeof (server));
- if (-1 == ret) {
- perror ("connect");
- return 2;
- }
- while (1) {
- /* 4. 向服务器发送数据 */
- fgets (buf, sizeof (buf), stdin);
- write (s_fd, buf, sizeof (buf));
- memset (buf, 0, sizeof (buf));
- /* 5. 等待获取服务器响应信息 */
- int r = read (s_fd, buf1, sizeof (buf1));
- /* 6. 将服务器的响应信息输出到显示器 */
- write (1, buf1, r); // 1==STDOUT
- // write (1, "\n", 2);
- memset (buf1, 0, sizeof (buf1));
- }
- /* 7. 关闭连接描述符 */
- close (s_fd);
- return 0;
- }
- "server - 支持多用户并发"
- /** 代码如下 server.c **/
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <ctype.h>
- #include <string.h>
- typedef struct sockaddr SA;
- typedef struct sockaddr_in SA4;
- int main(void) { /** 新增变量cli **/
- SA4 serv, cli; //定义服务器的地址变量
- int s_fd, conn_fd;
- int ret;
- char buf[128] = {0};
- char ip[128] = {0}; /** 存放ip地址字符串 **/
- int port; /** 存放端口号 **/
- //<1> 创建socket通讯端
- s_fd = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == s_fd) {
- perror("socket");
- return 1;
- }
- //补充:初始化服务器的信息
- serv.sin_family = AF_INET;
- serv.sin_port = htons(7777); //端口5000以下留给系,可选5000以上
- serv.sin_addr.s_addr = htonl(INADDR_ANY);//长整型主机号(ip)
- //<2> 将服务器的IP地址和端口号与s_fd绑定
- ret = bind(s_fd, (SA *)&serv, sizeof(SA4)); //SA4或serv
- if(-1 == ret) {
- perror("bind");
- return 2;
- }
- //<3> 监听s_fd
- listen(s_fd, 5);
- //<4> 阻塞等待客户端的连接
- while(1) {
- /** 将客户端的socket pairs保存在了cli指向的地址里 **/
- int cli_len = sizeof(cli);
- conn_fd = accept(s_fd, (SA *)&cli, &cli_len);
- if(-1 == conn_fd) {
- perror("accept");
- return 3;
- }
- pid_t pid = fork();
- if(pid < 0) {
- perror("fork");
- return 4;
- }
- if(pid > 0) { /** 父进程:负责监听客户端的连接 **/
- close(conn_fd);
- continue;
- } else { /** 子进程:负责和客户端通信 **/
- close(s_fd); /** 取消关闭s_fd就不会报accept **/
- /** 将二进制ip转换为字符串ip地址打印 **/
- char *p = (char *)inet_ntop(AF_INET, &cli.sin_addr, ip, 128);
- printf("客户端ip:%s\n", p);
- printf("用户姓名:");
- if(!strcmp(p, "176.135.11.138")) {
- printf("唐发洪\n");
- }
- /** 从cli结构体中打印端口号 **/
- printf("端口号:%d\n", ntohs(cli.sin_port));
- //<5> 读取客户端发送过来的信息
- int r = read(conn_fd, buf, 127);
- //<6> 加工处理信息
- for(int i = 0; i < r; i++) {
- buf[i] = toupper(buf[i]);
- }
- //给客户端响应
- write(conn_fd, buf, r);
- printf("接收到信息:%s\n", buf);
- //<7> 和这个客户端业务结束,关闭连接
- sleep(10);
- exit(0); //关闭子进程
- close(conn_fd);
- }
- }
- return 0;
- }
- /** 举例验证:
- 编写程序,实现UDP的通讯,客户端发送字符串,服务器端将客户端的发送过来的字符串转换为大写,响应给客户端。客户端收到服务器端的响应,将响应信息输出到屏幕上。
- 服务器端 userver.c
- 客户端 uclient.c **/
- "userver.c"
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <ctype.h>
- typedef struct sockaddr SA;
- typedef struct sockaddr_in SA4;
- int main(void) {
- int s_fd;
- SA4 server, client;
- char buf[128] = {0};
- //<1> 创建socket
- s_fd = socket(AF_INET, SOCK_DGRAM, 0);
- if(-1 == s_fd) {
- perror("socket");
- return 1;
- }
- //初始化server
- server.sin_family = AF_INET;
- server.sin_port = htons(7779);
- server.sin_addr.s_addr = htonl(INADDR_ANY);
- //<2> 绑定s_fd和服务器的socket pair
- int b = bind(s_fd, (SA *)&server, sizeof(server));
- if(-1 == b) {
- perror("bind");
- return 2;
- }
- while(1) {
- //<3> 接收数据
- int cli_len = sizeof(client);
- int r = recvfrom(s_fd, buf, 128, 0, (SA *)&client, &cli_len);
- printf("接收到信息:%s\n", buf);
- //<4> 数据处理
- for(int i = 0; i < r; i++) {
- buf[i] = toupper(buf[i]);
- }
- //<5> 发送给客户端
- sendto(s_fd, buf, r, 0, (SA *)&client, sizeof(client));
- printf("发出的信息:%s\n", buf);
- }
- close(s_fd);
- return 0;
- }
- "uclient.c"
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- int main(void) {
- int s_fd;
- char buf[20] = "hello,world!";
- //<1> 创建socket通讯端
- s_fd = socket(AF_INET, SOCK_DGRAM, 0);
- if(-1 == s_fd) {
- perror("socket");
- return 1;
- }
- struct sockaddr_in server;
- //初始化server的信息
- server.sin_family = AF_INET;
- server.sin_port = htons(7779);
- inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);
- //<2> 发送信息给服务器
- sendto(s_fd, buf, strlen(buf)+1, 0, (struct sockaddr *)&server, sizeof(server));
- //<3> 从服务器端等待响应信息
- bzero(buf, 20);
- int r = recvfrom(s_fd, buf, 20, 0, NULL, NULL);
- //<4> 处理数据
- write(1, buf, r);
- write(1, "\n", 2);
- close(s_fd);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。