C++ socket programming in Linux
Server.c
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define szSTR 256
#define SERVERPORT 21429 /*please modify the port with your id*/ int do_listen(const int port, const int bTcp)
{
int s = , r = , o = ;
struct sockaddr_in h;
memset(&h, , 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, );
if (s < ) { perror("socket(listen)"); return ;}
r = setsockopt(s, SOL_SOCKET,SO_REUSEADDR, (char *)&o, sizeof(int));
if (r == -) { perror("setsockopt(listen)"); return ;}
r = bind(s, (struct sockaddr *)&h, sizeof(h));
if (r == -) { perror("bind(listen)"); return ;}
if (bTcp) {
r = listen(s, SOMAXCONN);
if (r == -) { perror("listen()"); return ;}
}/*end if*/
/*signal(SIGPIPE, SIG_IGN);*/
return s;
}/*end do_listen*/ void response(int sck, struct sockaddr_in * host)
{
FILE * f = ; char cmd[szSTR]; time_t now;
struct tm * t = ;
f = fdopen(sck, "w+");
while(!feof(f)) {
memset(cmd, , szSTR);
fgets(cmd, szSTR -, 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 + , t->tm_mday, t->tm_year + );
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 = ;int sck = , i = , listener = ;
struct sockaddr_in client; pid_t proc = ;
system("ifconfig");
listener = do_listen(SERVERPORT, );
if(listener < ) { perror("listen()"); return ; }
for(i=;i<;i++) {
memset(&client, , sizeof(client));
sklen = sizeof(client);
sck = accept(listener, (struct sockaddr *)&client, &sklen);
if(sck < ) break;
proc = fork();
if (proc == -) { perror("fork()"); break ; }
if(proc) continue;
response(sck, &client);
break;
}/*next*/
close(listener);
return ;
}/*end main*/
Client.c
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#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, , 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, );
if (s < ) { perror("socket(tcp)"); return ;}
int r = connect(s, (struct sockaddr *)&h, sizeof(h));
if (r == ) return s;
perror("connect()");
return ;
}//end cnn int main(void)
{
int sck = ; FILE * f = ; char ip[szSTR]="127.0.0.1";
fprintf(stderr, "Please input the calendar server ip:");
fgets(ip, szSTR - , stdin);
sck = cnn(ip, SERVERPORT);
if(sck < ) return ;
f = fdopen(sck, "w+");
fprintf(f, "DATE\r\n");
fgets(ip, szSTR - , f);
fprintf(stderr, "%s\n", ip);
fprintf(f, "TIME\r\n");
fgets(ip, szSTR - , f);
fprintf(stderr, "%s\n", ip);
fprintf(f, "EXIT\r\n");
fclose(f);
close(sck);
return ;
}/*end main*/
C++ socket programming in Linux的更多相关文章
- C Socket Programming for Linux with a Server and Client Example Code
Typically two processes communicate with each other on a single system through one of the following ...
- Socket programming in C on Linux | tutorial
TCP/IP socket programming This is a quick guide/tutorial to learning socket programming in C languag ...
- linux c socket programming
原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http:/ ...
- [PHP-Socket] Socket Programming in PHP
Simple Client-Server socket program in PHP Introduction Sockets are used for interprocess communicat ...
- Socket Programming in C#--Conclusion
Conclusion And that's all there is to it! Here is how our client looks like Here is how our server l ...
- Socket Programming in C#--Getting Started
Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...
- Socket Programming in C#--Introduction
This is the second part of the previous article about the socket programming. In the earlier article ...
- How To: Perl TCP / UDP Socket Programming using IO::Socket::INET
http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/ In this article, let us discuss ...
- Python Socket Programming
本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...
随机推荐
- 使用$_SERVER['HTTP_HOST']时需注意的
在php中,我们一般通过$_SERVER['HTTP_HOST']来活得URL中网站的域名或者ip地址. $_SERVER['HTTP_HOST']在客户的环境里,取得的值总是程序所在的服务器在其局域 ...
- Linux lsof命令详解和使用示例【转】
所以如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符,无论这个文件的本质如何,该文件描述符为应用程序与基础操作系统之间的交互提供了通用接 ...
- [帖子收集]通用Windows平台(UWP)
通用Windows平台,universal windows platform,UWP 什么是通用 Windows 平台 (UWP) 应用?(微软MSDN) 如何在通用 Windows 平台应用中使用现 ...
- 最新Node.js 资源汇总
Node.js 资源汇总 文档 Node.js 官方文档:http://nodejs.org/api/ Node.js 中文文档:http://nodejs.jsbin.cn/api/ Express ...
- C# 常用接口学习 ICollection<T>
C# 常用接口学习 ICollection<T> 作者:乌龙哈里 时间:2015-11-01 平台:Window7 64bit,Visual Studio Community 2015 参 ...
- php 常用的调试方法
file_put_contents("c:\1.log","输出字符串",FILE_APPEND);第三个参数是防止前面的内容被覆盖 error_log(pri ...
- 单词缩写(abbr.cpp)每日一题
题目描述:YXY 发现好多计算机中的单词都是缩写,如 GDB,它是全称 Gnu DeBug 的缩写.但是,有时缩写对应的全称并不固定,如缩写 LINUX,可以理解为:(1)LINus's UniX ( ...
- ES 集群调整、升级 最佳实践
日常应用中我们会经常对es 集群做一些参数调整或者升级版本,但是每次关闭节点再打开 其中的数据同步的痛苦估计有很多人领悟过(有可能出现IO或者网络拥堵导致恶性循环)官网有套方案可以尝试一下: 1.关掉 ...
- L3-002. 堆栈
L3-002. 堆栈 题目链接:https://www.patest.cn/contests/gplt/L3-002 线段树 线段树的数据修改和查询都是O(lgn)的,此题只需维护各个区间内的数的个数 ...
- PostgreSQL学习手册
事实上之前有很长一段时间都在纠结是否有必要好好学习它,但是始终都没有一个很好的理由说服自己.甚至是直到这个项目最终决定选用PostgreSQL 时,我都没有真正意识到学习它的价值,当时只是想反正和其它 ...