博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ socket programming in Linux
阅读量:5145 次
发布时间:2019-06-13

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

Server.c

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define szSTR 256#define SERVERPORT 21429 /*please modify the port with your id*/int do_listen(const int port, const int bTcp){ int s = 0, r = 0, o = 1; struct sockaddr_in h; memset(&h, 0, sizeof(h)); h.sin_family = AF_INET; h.sin_port = htons(port); h.sin_addr.s_addr = INADDR_ANY; s = socket(AF_INET, bTcp?SOCK_STREAM:SOCK_DGRAM, 0); if (s < 1) { perror("socket(listen)"); return 0;} r = setsockopt(s, SOL_SOCKET,SO_REUSEADDR, (char *)&o, sizeof(int)); if (r == -1) { perror("setsockopt(listen)"); return 0;} r = bind(s, (struct sockaddr *)&h, sizeof(h)); if (r == -1) { perror("bind(listen)"); return 0;} if (bTcp) { r = listen(s, SOMAXCONN); if (r == -1) { perror("listen()"); return 0;} }/*end if*/ /*signal(SIGPIPE, SIG_IGN);*/ return s;}/*end do_listen*/void response(int sck, struct sockaddr_in * host){ FILE * f = 0; char cmd[szSTR]; time_t now; struct tm * t = 0; f = fdopen(sck, "w+"); while(!feof(f)) { memset(cmd, 0, szSTR); fgets(cmd, szSTR -1, f); time(&now); t = localtime(&now); if(strstr(cmd, "DATE")) { fprintf(stderr, "Query Type: Date\n"); fprintf(f, "Date: %d %d, %d\n", t->tm_mon + 1, t->tm_mday, t->tm_year + 1900); continue; }/*end if*/ if(strstr(cmd, "TIME")) { fprintf(stderr, "Query Type: Time\n"); fprintf(f, "Time: %02d::%02d::%02d\n", t->tm_hour, t->tm_min, t->tm_sec); continue; }/*end if*/ if(strstr(cmd, "EXIT")) break; fprintf(f, "commands: DATE, TIME, EXIT.\n"); }/*end while*/ shutdown(sck, SHUT_RDWR); close(sck); fclose(f); return ;}/*end response*/int main(void){ socklen_t sklen = 0;int sck = 0, i = 0, listener = 0; struct sockaddr_in client; pid_t proc = 0; system("ifconfig"); listener = do_listen(SERVERPORT, 1); if(listener < 1) { perror("listen()"); return 0; } for(i=0;i<2;i++) { memset(&client, 0, sizeof(client)); sklen = sizeof(client); sck = accept(listener, (struct sockaddr *)&client, &sklen); if(sck < 1) break; proc = fork(); if (proc == -1) { perror("fork()"); break ; } if(proc) continue; response(sck, &client); break; }/*next*/ close(listener); return 0;}/*end main*/

Client.c

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define szSTR 256#define SERVERPORT 21429 /*please modify the port with your id*/int cnn(const char * ip, const int port){ struct sockaddr_in h; memset(&h, 0, sizeof(h)); h.sin_family = AF_INET; h.sin_port = htons(port); h.sin_addr.s_addr = inet_addr(ip); int s = socket(AF_INET, SOCK_STREAM, 0); if (s < 1) { perror("socket(tcp)"); return 0;} int r = connect(s, (struct sockaddr *)&h, sizeof(h)); if (r == 0) return s; perror("connect()"); return 0;}//end cnnint main(void){ int sck = 0; FILE * f = 0; char ip[szSTR]="127.0.0.1"; fprintf(stderr, "Please input the calendar server ip:"); fgets(ip, szSTR - 1, stdin); sck = cnn(ip, SERVERPORT); if(sck < 1) return 0; f = fdopen(sck, "w+"); fprintf(f, "DATE\r\n"); fgets(ip, szSTR -1 , f); fprintf(stderr, "%s\n", ip); fprintf(f, "TIME\r\n"); fgets(ip, szSTR -1 , f); fprintf(stderr, "%s\n", ip); fprintf(f, "EXIT\r\n"); fclose(f); close(sck); return 0;}/*end main*/

 

转载于:https://www.cnblogs.com/miaoz/p/4438612.html

你可能感兴趣的文章
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>
sublime3
查看>>
Exception Type: IntegrityError 数据完整性错误
查看>>
Nuget:Newtonsoft.Json
查看>>
Hdu - 1002 - A + B Problem II
查看>>
每天CookBook之Python-003
查看>>
每天CookBook之Python-004
查看>>
Android设置Gmail邮箱
查看>>
js编写时间选择框
查看>>
Java数据结构和算法(四)--链表
查看>>
JIRA
查看>>
小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
查看>>
深浅拷贝(十四)
查看>>
HDU 6370(并查集)
查看>>
BZOJ 1207(dp)
查看>>
PE知识复习之PE的导入表
查看>>