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 = 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 <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, 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 cnn

int 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*/
时间: 2024-08-01 10:31:28

C++ socket programming in Linux的相关文章

Linux Socket Programming by Example-第十八章 实战

第18章主要介绍了一个软件实践项目. 架构: C/S 功能:实现一个股票信息广播发布平台. 这个代码本身对2年工作以上的人来说,毫无参考价值. 主要的是软件本身引发的一些发散思考. 最近智能家电 物联网 大数据 数字医疗等都很火热. 这些设备均涉及到2个网络, 一个是基于WIFI.以太网的局域网 一个是基于ISP服务的互联网. 具体构架: 家里的智能设备     -> WiFi  ->  智能路由器 -> ISP ->  互联网 (远程服务器) 如果将智能路由器扩展下,变成带数据库

linux c socket programming

原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http://www.slideshare.net/Arbow/asynchronous-io-programming verygood C: Linux Socket Programming, TCP, a simple HTTP client http://coding.debuntu.org/c-linu

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 how to write Perl socket programming using the inbuilt socket modules in Perl. Perl socket modules provides an object interface that makes it easier

Socket Programming

| There are two ways to store this value. Little Endian.(低位优先) Big Endian.(高位优先) | The complete Client and Server interaction. | The simplest way to write a concurrent server under Unix is to fork a child process to handle each client separately. | S

linux:socket 系统调用在linux内核中的实现流程图

socket 系统调用在linux内核中的实现:

将socket程序从linux移植到windows上

今天突然想试下纯socket编程在两个系统上代码重合量有多大,只要不使用VC自定义的宏(比如SOCKET.SOCKADDR等等)感觉代码重合量挺大的. 比如最简单的TCP客户端和服务端对话,在VC中用int取代SOCKET宏,用struct sockaddr_in取代SOCKADDR_IN宏. 然后区别就仅仅是头文件和windows额外加载/关闭套接字库的代码了. // Unix/Linux#include <sys/socket.h> #include <netinet/in.h>

Serial Port Programming on Linux(转载)

This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little interfaces on the PC which helps you to interface your embedded system projects using a minimum number of wires.In this tutorial we will write a smal

socket programming Max size of tcp/ip socket Buffer?

TCP data is buffered at both sender and receiver. The size of the receiver's socket receive buffer determines how much data can be in flight without acknowledgement, and the size of the sender's send buffer determines how much data can be sent before

programming in linux... with third_party open sources... methods

Actually I do not have experiences in programming with open sources/third party libs.. in linux.. I think this took me almost 3 days to figure out how to learn programming with open source tools.. First you need to build libs/shared libs in the syste