码迷,mamicode.com
首页 > 编程语言 > 详细

C++ socket programming in Linux

时间:2015-04-19 08:51:40      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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*/

 

C++ socket programming in Linux

标签:

原文地址:http://www.cnblogs.com/miaoz/p/4438612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!