網路字典伺服器端

2021-06-06 08:43:02 字數 2768 閱讀 4255

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define  n  256

#define  r  1   //  user register

#define  l  2   //  user login

#define  q  3   //  query word

#define  h  4   //  history record

#define database "my.db"

typedef struct sockaddr sa;

typedef struct

msg;

void handler(int signo)

void do_register(int connfd, msg *pbuf, sqlite3 *db)

else

strcpy(pbuf->data, "ok");

send(connfd, pbuf, sizeof(msg), 0); }

void do_login(int connfd, msg *pbuf, sqlite3 *db)

bzero(pbuf->data, sizeof(pbuf->data));

if (nrow == 0)

strcpy(pbuf->data, "name or password is wrong!!!");

else

strcpy(pbuf->data, "ok");

send(connfd, pbuf, sizeof(msg), 0);

sqlite3_free_table(result); }

void get_date(char date)

int do_searchword(int connfd, msg *pbuf) ;

char *p;

int len, result;

/***************pbuf->data

傳入單詞*****************/

len = strlen(pbuf->data);//pbuf->data --- about

if ((fp = fopen("dict.txt", "r")) == null)

return -1;

printf("query word is %s\n", pbuf->data);

/***************fgets

函式讀到』\n

』結束 **********************/

while (fgets(line, 300, fp) != null)

fclose(fp);

return 0; }

void do_query(int connfd, msg *pbuf, sqlite3 *db)

, word[64] = ;

strcpy(word, pbuf->data);

found = do_searchword(connfd, pbuf);

if (found == -1)

strcpy(pbuf->data, "server error\n");

else if ( found )

} else

strcpy(pbuf->data, "not found\n");

send(connfd, pbuf, sizeof(msg), 0); }

int history_callback(void *arg, int f_num, char **f_value, char **f_name)

void do_history(int connfd, msg *pbuf, sqlite3 *db)

pbuf->data[0] = '\0';

send(connfd, pbuf, sizeof(msg), 0);

return; }

int main(int argc, char *argv)

if (sqlite3_open(database, &db) != sqlite_ok)

if ((listenfd = socket(pf_inet, sock_stream, 0)) < 0)

bzero(&myaddr, sizeof(myaddr));

myaddr.sin_family = pf_inet;

myaddr.sin_port = htons(atoi(argv[2]));

myaddr.sin_addr.s_addr = inet_addr(argv[1]);

if (bind(listenfd, (sa *)&myaddr, sizeof(myaddr)) < 0)

if (listen(listenfd, 5) < 0)

signal(sigchld, handler);

while ( 1 )

if ((pid = fork()) < 0)

else if (pid == 0)

} printf("client quit\n");

close(connfd);

exit(0); }

close(connfd); }

exit(0); }

socket伺服器端

伺服器 include winsock2.h include string.h include stdio.h include time.h include stdarg.h include stdlib.h pragma comment lib,ws2 32 void errexit const ...

kerberos伺服器端

1.安裝tcl wget tar zvxf tcl8.5.12 src.tar.gz cd tcl8.5.12 cd unix configure make make install 3.解壓 tar xvf krb5 1.10.3 signed.tar tar zvxf krb5 1.10.3.t...

C tcp伺服器端

伺服器端 include stdafx.h include winsock2.h pragma comment lib,ws2 32.lib include using namespace std int tmain int argc,char ar 建立套接字 sserver socket af ...