我正在尝试开发一个服务器应用程序,从客户端获取英语单词,而不是将其翻译为土耳其语单词。服务器应该在翻译过程中使用litesql。在此过程中,数据库应至少具有以下键值对:
键:值
一:bir
二:iki
三:uc
四:dort
五:bes
六:alti
七:yedi
八:sekiz
九:dokuz
十:
每当我尝试编译此代码时,我都会收到以下错误代码:
21701047@db:~$ gcc threadv1.c -o threadv1 -lpthread
/tmp/ccMnTKvk.o: In function `Child':
threadv1.c:(.text+0x54): undefined reference to `sqlite3_open'
threadv1.c:(.text+0x6c): undefined reference to `sqlite3_errmsg'
collect2: error: ld returned 1 exit status
代码
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <sqlite3.h>
/* Definations */
#define DEFAULT_BUFLEN 1024
#define PORT 1888
void PANIC(char* msg);
#define PANIC(msg) { perror(msg); exit(-1); }
/*--------------------------------------------------------------------*/
/*--- Child - echo server ---*/
/*--------------------------------------------------------------------*/
void* Child(void* arg)
{ char line[DEFAULT_BUFLEN];
int bytes_read;
int client = *(int *)arg;
char* f_name = "translator.db";
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
printf("[server] send %s to the client...", f_name);
rc = sqlite3_open("translator.db", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(1);
}
do
{
bytes_read = recv(client, line, sizeof(line), rc);
if (bytes_read > 0) {
if ( (bytes_read=send(client, line, bytes_read, 0)) < 0 ) {
printf("Send failed\n");
break;
}
} else if (bytes_read == 0 ) {
printf("Connection closed by client\n");
break;
} else {
printf("Connection has problem\n");
break;
}
} while (bytes_read > 0);
close(client);
return arg;
}
/*--------------------------------------------------------------------*/
/*--- main - setup server and await connections (no need to clean ---*/
/*--- up after terminated children. ---*/
/*--------------------------------------------------------------------*/
int main(int argc, char *argv[])
{ int sd,opt,optval;
struct sockaddr_in addr;
unsigned short port=0;
while ((opt = getopt(argc, argv, "p:")) != -1) {
switch (opt) {
case 'p':
port=atoi(optarg);
break;
}
}
if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
PANIC("Socket");
addr.sin_family = AF_INET;
if ( port > 0 )
addr.sin_port = htons(port);
else
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;
// set SO_REUSEADDR on a socket to true (1):
optval = 1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
PANIC("Bind");
if ( listen(sd, SOMAXCONN) != 0 )
PANIC("Listen");
printf("System ready on port %d\n",ntohs(addr.sin_port));
while (1)
{
int client, addr_size = sizeof(addr);
pthread_t child;
client = accept(sd, (struct sockaddr*)&addr, &addr_size);
printf("Connected: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
if ( pthread_create(&child, NULL, Child, &client) != 0 )
perror("Thread creation");
else
pthread_detach(child); /* disassociate from parent */
}
return 0;
}