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

linux c++ 服务器编程,收藏一个测试例子

时间:2015-09-05 00:00:17      阅读:548      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
// server
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include"time.h"
 
 
#define  SERVER_PORT 10000
#define  MAX_BUF_SIZE  16
#define  SUCCESS 0
#define  FAILURE -1
 
int RecvFromServer(int iClientSockFd);
 
int main(int argc, char **argv)
{
    int iServerSockFd;
    int iClientSockFd;
    struct sockaddr_in stServerAddr;
    struct sockaddr_in stClientAddr;
    int iServerLen;
    int iClientLen;
    int iOpt =1;
    int iReturn;
    pid_t  pid;
     
    // time 
    time_t timer;
    struct tm  *tblock;
    timer = time(NULL);
    tblock= localtime(&timer);
    printf("servertime is %s",asctime(tblock));
 
    printf("This is the Server.\n");
    printf("I am waiting the client to rsync time \n");
 
    // create server socket
    iServerSockFd = socket(AF_INET, SOCK_STREAM, 0);
    if (iServerSockFd < 0)
    {
        printf("socket create failed in CommManageServerRecvThread.\n");
    return FAILURE;
    }
    stServerAddr.sin_family = AF_INET;
    stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    stServerAddr.sin_port = htons(SERVER_PORT);
    iServerLen = sizeof(stServerAddr);
 
    // bind socket
    iReturn = bind(iServerSockFd, (struct sockaddr *)&stServerAddr, iServerLen);
    if (iReturn < 0)
    {
    printf("socket bind failed in CommManageServerRecvThread.\n");
 
    }
 
    setsockopt(iServerSockFd,SOL_SOCKET,SO_REUSEADDR,&iOpt,sizeof(iOpt));
 
    // listen socket
    iReturn = listen(iServerSockFd, 10);
 
    // accept request from client
    while(1)
    {
    iClientSockFd = accept(iServerSockFd, (struct sockaddr*)&stClientAddr, (unsigned *)&iClientLen);
    if (iServerSockFd < 0)
    {
        printf("accept failed.\n");
         
    }
    pid = fork();    
    switch(pid)
    {
        case -1:
        {
            printf("fork failed.\n");
            break;
        }
        case 0:
        {
           write(iClientSockFd,asctime(tblock),30);
           printf("child service ok!!\n");
           close(iClientSockFd);
           break;
        }
        default:
        {
            close(iClientSockFd);
            break;
        }
    }
 
    }
 
    close(iClientSockFd);
 
    close(iServerSockFd);
 
    return SUCCESS;
}
 
int RecvFromServer(int iClientSockFd)
{
    char acBuf[MAX_BUF_SIZE];
    int iReadNum;
 
    memset(acBuf, 0, sizeof(acBuf));
 
    iReadNum = recv(iClientSockFd, acBuf, 0xFFFF, 0);
    if(iReadNum < 0)
    {
        printf("read failed.\n");
        return FAILURE;
    }
 
    printf("Receive form client:\n");
    printf("%s\n", acBuf);
 
    return SUCCESS;
}
 
 
 
 
 
// client
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
 
 
#define  SERVER_PORT 10000
#define  SERVER_IP   "192.168.0.112"
#define  WRITE_BUF_SIZE  1024
 
#define  SUCCESS 0
#define  FAILURE -1
char acBuf[WRITE_BUF_SIZE];
 
int my_read(int fd,void *buffer,int length) 
{        
         int bytes_left;
         int bytes_read;
         char *ptr;     
         bytes_left=length; 
          
         ptr=buffer;
              
         while(bytes_left>0)         
         {          
            bytes_read=read(fd,ptr,bytes_left);
            if(bytes_read<0) 
                 {        
                  if(errno==EINTR) 
                      bytes_read=0;
                  else                 
                      return -1;
                  }              
            else
               if(bytes_read==0)     
                     break;
              bytes_left -= bytes_read; 
              ptr+=bytes_read;    
          }     
          return(length-bytes_left);
}
 
int main(int argc, char **argv)
{
    int iSockFd;
    int iReadNum;
     
    struct sockaddr_in stServerAddr;
    struct sockaddr_in stClientAddr;
    int iServerLen;
    int iClientLen;
    int iReturn;
     
 
    printf("This is the client.\n");
 
 
    // create server socket
    iSockFd = socket(AF_INET, SOCK_STREAM, 0);
    if (iSockFd < 0)
    {
        printf("socket create failed in CommManageServerRecvThread.\n");
        return FAILURE;
    }
    stServerAddr.sin_family = AF_INET;
    inet_pton(AF_INET, SERVER_IP, &stServerAddr.sin_addr);
    stServerAddr.sin_port = htons(SERVER_PORT);
 
    iServerLen = sizeof(stServerAddr);
 
    while(1)
    {    
        //printf("try to connect to the server... ...\n");
        iReturn = connect(iSockFd, (const struct sockaddr *)&stServerAddr, iServerLen);
        if(iReturn >= 0)
        {
            break;
        }
 
        sleep(3);
    }
 
    printf("connect SUCCESS.\n");
     
    memset(acBuf, 0, sizeof(acBuf));
     
    my_read(iSockFd, acBuf, 30);
      
    printf("%s\n",acBuf);
 
    close(iSockFd);
 
    return SUCCESS;
}
View Code

 

linux c++ 服务器编程,收藏一个测试例子

标签:

原文地址:http://www.cnblogs.com/stone531/p/4782507.html

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