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. HTTP 错误 403.14 - Forbidden的解决办法

    错误: HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容.   原因: 出现这个错误,是因为默认文档中没有增加index.aspx导致的. 解决方法: 打开 ...

  2. 汇总前端最最常用的JS代码片段-你值得收藏

    原始出处,可拷贝:http://www.w3cfuns.com/notes/25068/1d0d350a974d879e63f1115cf80a3288.html 摘自:http://www.love ...

  3. iOS10 CAAnimationDelegate的适配

    最近在xcode8打开之前的动画代码,看到如下警告

  4. git add -f

    git add -f 添加已被 .gitignore 忽略的文件/文件夹

  5. 链表中LinkList L与LinkList *L 借鉴

    链表中LinkList L与LinkList *L的区别以及(*L).elem,L.elem L->next,(*L)->next的区别typedef struct Node{int el ...

  6. Chapter 19_0 位操作库

    位操作库是Lua5.2版本里添加的库,所有函数放在bit32 table里.(bit32只能针对32位整数运算) 在Lua5.3版本里,bit32库被废弃掉.不过可以使用一个外部兼容库,但是最好直接用 ...

  7. android自定义控件,其三个父类构造方法有什么区别

    android自定义控件时,通常需要重写父类构造函数.这三个够找函数具体啥时调用? public View (Context context) 是在java代码创建视图的时候被调用,如果是从xml填充 ...

  8. 。net定时关闭excel进程

    public void Application_Start() { // 在应用程序启动时运行的代码 System.Timers.Timer timer = new System.Timers.Tim ...

  9. Java并发编程:深入剖析ThreadLocal(转载)

    Java并发编程:深入剖析ThreadLocal(转载) 原文链接:Java并发编程:深入剖析ThreadLocal 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadL ...

  10. ionic 进入多级目录以后隐藏底部导航栏(tabs)(完美解决方案)

    公司开始使用ionic开发项目,在此记录下把遇到的问题,网上有大牛已经把解决方法整出来了,不过记录在自己这里方便查阅. 这篇记录在有tabs的项目里,进入子层级时,底部导航还一直存在,本人是要让他只在 ...