有进程控制的服务器与客户机的通信

头文件hea.h

#ifndef _HEAD_H_
#define _HEAD_H_


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <dirent.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <pthread.h>
#include <semaphore.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define error do\
  {\
   printf("[%d]:%s\n",__LINE__,strerror(errno));\
   exit(-1);\
  }while(0)

 

#endif

 

 

服务器部分:

fork_server.c

#include "head.h"

#define SERVER_PORT 8899
#define SERVER_IP "127.0.0.1"
#define BACKLOG 12

int write_fully(int fd, void *buf,int len)
{
 int total=0;
 int count=0;
 while(total<len)
 {
  count=write(fd,buf+total,len-total);
  if(count<0)return -1;
  total+=count;
 } 
 return 0;
}

int main(int argc,char *argv[])
{
 int sockfd;
 sockfd=socket(AF_INET,SOCK_STREAM,0);
 if(sockfd<0)error;
 
 struct sockaddr_in l_addr;
 l_addr.sin_family=AF_INET;
 l_addr.sin_port=htons(SERVER_PORT );
 l_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
 //inet_aton(SERVER_IP,&l_addr.sin_addr);
 //inet_pton(AF_INET,SERVER_IP,&l_addr.sin_addr);
 
 if(bind(sockfd,(struct sockaddr*)&l_addr,sizeof(l_addr))<0)error;
  
 if(listen(sockfd,BACKLOG)<0)error;

 int new_sockfd; 
 struct sockaddr_in c_addr;
 socklen_t c_len=sizeof(c_addr);
 
 while(1)
 {
  new_sockfd=accept(sockfd,(struct sockaddr*)&c_addr,&c_len);
  
  int port=ntohs(c_addr.sin_port);
  char *cip=inet_ntoa(c_addr.sin_addr);
  printf("<%s>:<%d>\n",cip,port);
  
  pid_t pid;
  if((pid=fork())<0)error;
  if(pid==0)
  {
   while(1)
   {
    char buf[256];
    int r;
    r=read(new_sockfd,buf,sizeof(buf));
    buf[r]=0;
    printf("%s\n",buf);
    
    char send[256]="hello client,i am server!";
    if(write_fully(new_sockfd,send,sizeof(send))<0)error;
   }
   close(new_sockfd);
  }
 }
 
 close(sockfd);
 
 return 0;
}

 

客户机部分:

fork_client.c:

#include "head.h"

#define LOCALE_PORT 7788

#define SERVER_PORT 8899
#define SERVER_IP "127.0.0.1"

int write_fully(int fd, void *buf,int len)
{
 int total=0;
 int count=0;
 while(total<len)
 {
  count=write(fd,buf+total,len-total);
  if(count<0)return -1;
  total+=count;
 } 
 return 0;
}

int main(int argc,char *argv[])
{
 int sockfd;
 sockfd=socket(AF_INET,SOCK_STREAM,0);
 if(sockfd<0)error;
  
 //指定要连接的服务器地址结构
 struct sockaddr_in s_addr;
 s_addr.sin_family=AF_INET;
 s_addr.sin_port=htons(SERVER_PORT);
 s_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
 
 if(connect(sockfd,(struct sockaddr*)&s_addr,sizeof(s_addr))<0)error;
 
 while(1)
 { 
  char send[256]="hello server,i am client!";
  write_fully(sockfd,send,sizeof(send));
  
  sleep(1);
  
  char buf[256];
  int r;
  r=read(sockfd,buf,sizeof(buf));
  buf[r]=0;
  printf("%s\n",buf);
 }
 
 close(sockfd);
  
 return 0;
}

posted on 2013-05-27 09:14  老zhang  阅读(133)  评论(0)    收藏  举报

导航