电子词典的查寻程序,发送和接收应答程序

整个电子词典是分块做的:包含的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\n",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] = '\0';

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

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

}

else if(buf[0] == (dictionary[0] + 1))
{
printf("input dictionary error\n");
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;
}

 

原文链接: https://www.cnblogs.com/cnlg/p/4366924.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    电子词典的查寻程序,发送和接收应答程序

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/171310

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 上午1:56
下一篇 2023年2月12日 上午2:15

相关推荐