Linux/Unix下Socket编程

演示linux的客户端和服务器端的通信过程。要同时启动两个terminal,一个运行Server,一个运行Client。通过Client向Server发送主机名和要检索的字符串。Server端接到客户端的请求后,便根据客户端发过来的原串去文件server.conf中检索其对应的值,找到后就把值返回给Client。否则返回"not find"
server.conf文件格式为:
name=aaa
mail=adfaf
:
编译源文件:gcc -o Server Server.c
          gcc -o Client Client.c
先启动Server,在命令提示符下输入:Server
再启动Client,在命令提示符下输入:Client redhat4 name #我的主机名是redhat4
 
服务器端源码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 35235    
#define BACKLOG 10    
main()
{
     int sockfd,client_fd;    
     struct sockaddr_in my_addr;
     struct sockaddr_in remote_addr;
        
    /* The fllow variables using to file */
    char lineStr[30];
    char recStr[20];
    char headStr[15],tailStr[15];
    char *delim="=";
    FILE *file;
    int flag=0;
        
     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)    
     {
            perror("socket error");    
     exit(1);
     }
     //设置本地socket实例的协议族,连接端口等
     my_addr.sin_family=AF_INET;
     my_addr.sin_port=htons(SERVPORT);
     my_addr.sin_addr.s_addr = INADDR_ANY;
     bzero(&(my_addr.sin_zero),8);
     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)    
     {
         perror("bind error!");
    exit(1);
     }
    if (listen(sockfd, BACKLOG) == -1)    
    {
         perror("listen error!");
         exit(1);
    }
    //监听并处理客户的连接请求
    while(1)    
    {
         int sin_size = sizeof(struct sockaddr_in);
         if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)    
    {
                perror("accept error!");
                continue;
         }
printf("received a connection from %s\n", inet_nto(remote_addr.sin_addr));    
    
//创建子进程,检索目标串并把结果返回个客户端
if (!fork())    
{
             int len;
        if((len=recv(client_fd,recStr,30,0))==-1)
        {
             printf("Receive error!\n");
        exit(1);
        }
        recStr[len]='\0';
        file=fopen("server.conf","r");
        printf("Client send: %s\n",recStr);
        while(fgets(lineStr,30,file))
        {
         //以“=”为标记,分别去等号的左边和右边,分别存入headStr和tailStr
         strcpy(headStr,strtok(lineStr,delim));
         strcpy(tailStr,strtok(NULL,delim));
     if(strcmp(headStr,recStr)==0)
     {
         flag=1;
         if(send(client_fd,tailStr,20,0)==-1)
         {
                            perror("send error!");
                exit(1);
         }
        /* break; */
        }
    }
    if(flag==0)
        send(client_fd,"Not find",9,0);
        
                close(client_fd);
                exit(0);
        }
        close(client_fd);
     }
}
客户端源码:
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 35235
#define MAXDATASIZE 100    
main(int argc, char *argv[])
{
     int sockfd, recvbytes;
     char buf[MAXDATASIZE];
     struct hostent *host;
     struct sockaddr_in serv_addr;
     if (argc < 3)    
     {
         fprintf(stderr,"Please enter the server's hostname and string!\n");
    exit(1);
     }
     if((host=gethostbyname(argv[1]))==NULL)    
     {
             herror("gethostbyname�eerror!");
             exit(1);
     }
     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
     {
             perror("socket?error!");
             exit(1);
     }
     //设置连接服务器的协议族,端口号,服务器端的地址等
     serv_addr.sin_family=AF_INET;
     serv_addr.sin_port=htons(SERVPORT);
     serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
     bzero(&(serv_addr.sin_zero),8);
     if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1)    
     {
            perror("connect?error!");
            exit(1);
     }
     if(send(sockfd,argv[2],20,0)==-1)
     {
             printf("send to server error!\n");
        exit(1);
}
     if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)    
     {
                perror("recv?error");
                exit(1);
        }
buf[recvbytes] = '\0';
printf("Received: %s\n",buf);
close(sockfd);
}
 
 

你可能感兴趣的:(linux,unix,socket,职场,休闲)