zoukankan      html  css  js  c++  java
  • 电子词典的查寻程序,发送和接收应答程序

    整个电子词典是分块做的:包含的Dic_Server.c,Dic_Client.c,db.c,query.c,xprtcl.c,dict.h,xprtcl.h,dict.txt(单词文件)

    下面是query.c代码:主要用创建子进程实现服务器端的并发操作

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>

    #define N 300


    int query_dictionary(const char *dictionary ,const char *path,char *text)
    {
    int i=0;
    char *pdic;
    char buf[N];
    char temp[17];
    FILE *fps;
    const char *dest,*src;

    if((fps = fopen(path,"r")) == NULL)
    {
    fprintf(stderr,"fail to fopen %s ",strerror(errno));
    return -1;
    }

    while((fgets(buf, N, fps)) != NULL)
    {

    if(buf[0] == dictionary[0])
    {
    pdic = buf;
    i = 0;
    while(*pdic != ' ')
    {
    temp[i] = buf[i];
    i++;
    pdic++;
    }
    temp[i] = '';

    dest = (const char *)dictionary;
    src = (const char *)temp;
    if(strcmp(dest,src) == 0)
    {
    while(*pdic++ == ' ');
    pdic--;
    i = 0;
    while(*pdic != ' ')
    {
    text[i++] = *pdic++;
    }

    text[i] = '';
    return 0;
    }

    }

    else if(buf[0] == (dictionary[0] + 1))
    {
    printf("input dictionary error ");
    return -1;
    }
    }

    if(feof(fps))
    {
    printf("input dictionary error");
    return -1;
    }
    return 0;
    }

    下面代码是xprtcl.c程序:(移植汪老师的代码)

    * http://nj.hqyj.com/
    *
    * This program is distributed in the purpose for training only
    * and hope that it will be useful, but WITHOUT ANY WARRANTY;
    * without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    *
    * Revision history
    * ------------------------------------------------------
    * 20140928 unicornx initial archived
    */
    /* sample codes for on-line dictionary project */

    #include <stdio.h>
    #include <arpa/inet.h>
    #include <unistd.h>

    #include "xprtcl.h"

    /*
    * sample code demo how to write/send an application data
    * packet to the target
    */
    int xp_send(int sfd, xprotocol_t *packet)
    {
    packet->hdr.cmd_type = htons(packet->hdr.cmd_type);
    packet->hdr.ret_code = htons(packet->hdr.ret_code);

    if (write(sfd, packet, sizeof(xprotocol_t)) < 0) {
    perror("write error");
    return -1;
    }

    return 0;
    }

    /*
    * sample code demos how to receive/read an application
    * data packet from the target
    */
    ssize_t xp_recv(int sfd, xprotocol_t *packet)
    {
    ssize_t nr;
    if ((nr = read(sfd, packet, sizeof(xprotocol_t))) <= 0) {
    perror("read error");
    return nr;
    }

    packet->hdr.cmd_type = ntohs(packet->hdr.cmd_type);
    packet->hdr.ret_code = ntohs(packet->hdr.ret_code);

    return nr;
    }

  • 相关阅读:
    pat1041. Be Unique (20)
    Linux基础命令---service
    Linux基础命令---last
    Linux基础命令---date
    Linux基础命令---ckconfig
    Linux基础命令---cal
    Linux基础命令---bc
    linux基础命令---df
    linux基础命令---du
    Linux基础命令---hwclock
  • 原文地址:https://www.cnblogs.com/cnlg/p/4366924.html
Copyright © 2011-2022 走看看