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的更多相关文章

  1. 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 ...

  2. Socket programming in C on Linux | tutorial

    TCP/IP socket programming This is a quick guide/tutorial to learning socket programming in C languag ...

  3. linux c socket programming

    原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http:/ ...

  4. [PHP-Socket] Socket Programming in PHP

    Simple Client-Server socket program in PHP Introduction Sockets are used for interprocess communicat ...

  5. 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 ...

  6. Socket Programming in C#--Getting Started

    Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...

  7. Socket Programming in C#--Introduction

    This is the second part of the previous article about the socket programming. In the earlier article ...

  8. 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 ...

  9. Python Socket Programming

    本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...

随机推荐

  1. VMware虚拟机服务的vmware-hostd自动启动和停止

    安装了虚拟机 任务管理器会出现vmware-hostd.exe  占用了80端口,导致xampp打不开,所以就想关闭vmware,解决方案如下: 开始——运行——services.msc,找到VM打头 ...

  2. CentOS 6.5的安装详解

    CentOS 6.5的安装详解 主流: 目前的Linux操作系统主要应用于生产环境, 主流企业级Linux系统仍旧是RedHat或者CentOS 免费: RedHat 和CentOS差别不大,Cent ...

  3. 【锋利的Jquery】读书笔记二

    一.jquery选择器 基本选择器 层次选择器 过滤选择器 基本过滤 内容过滤 可见性过滤 属性过滤 子元素过滤 first  : 获取单个元素       $("div:first&quo ...

  4. MSSQL2008 中文乱码问题 (引自ljg888的专栏)

    PHP向MSSQL2008中写入数据,中文乱码   首先:查看SQLserver编码格式的SQL语句为:     SELECT  COLLATIONPROPERTY('Chinese_PRC_Stro ...

  5. PQ分区魔术师v9.0 中文版

    软件名称: pqmagic 硬盘分区大师9.0中文绿色版 软件大小:5.80MB 软件语言:简体中文 软件类别:磁盘工具 软件授权:免费软件 更新时间:2013-10-082013-10-08 09: ...

  6. how to make a git repo un-git?

    If you have a git repo and now you want to make it a plain filesystem tree .. (removing the git trac ...

  7. .NET 简单的扩展方法使用。

    写代码时,我们经常会碰到dll中提供的方法,不够用或者不好用的情况.而且我们也不方便去更改dll本身的源码. 这时候我们可以使用.NET提供的"扩展方法"去解决这个问题. 下面我写 ...

  8. 学习node的REPL

    REPL: read eval print loop 支持 多行代码 编辑模式,即 识别{} () 为代码块. 在repl中快速查看 对象的属性 gl = global repl command .h ...

  9. c# 配置文件App.config操作类库

    public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </sum ...

  10. was性能调优前期准备

    http://www.ibm.com/developerworks/cn/websphere/library/techarticles/0707_wudan/#resources was应用服务器环境 ...