我使用 winsock2.h 库创建了简单的TCP服务器。它为客户将英语单词翻译成立陶宛语。我从客户那里接受了这样的询问:
int MAXRECV = 100;
char *buffer;
buffer = (char*) malloc((MAXRECV + 1) * sizeof(char));
valread = recv(new_socket, buffer, MAXRECV, 0);
buffer[valread] = '\0';
之后,我想打开一本字典并在文件中找到该单词翻译。这是函数调用和函数本身。
char *lithuanianWord;
lithuanianWord = (char*) malloc((MAXWORDSIZE + 1) * sizeof(char));
if (findTranslation(buffer, &lithuanianWord)) {
send(new_socket, lithuanianWord, strlen(lithuanianWord), 0);
} else {
lithuanianWord = "Translation not found";
send(new_socket, lithuanianWord, strlen(lithuanianWord), 0);
}
int findTranslation (char* word, char** lithuanianWord) {
FILE *fp;
int translationFound = 0;
const char englishWord[50];
char buffer[255];
fp = fopen("*\\dictionary.txt","r");
while((fgets(buffer,255,fp)) != NULL) {
sscanf(buffer,"%s%s", englishWord, lithuanianWord);
if(strcmp(englishWord, word)==0 ) {
translationFound = 1;
break;
}
}
fclose(fp);
return translationFound;
}
代码编译和运行完美。问题是txt文件没有打开,答案总是“翻译未找到”。独立时此功能正常。路径完整和相同,我只是缩短了它。 我收到错误: 没有此类文件或目录 。客户端和服务器都在同一硬件上运行。
#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#include<string.h>
int findTranslation (char* word, char** translation);
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET master , new_socket, client_socket[30], s;
struct sockaddr_in server , address;
int max_clients = 30, activity, addrlen, i, valread;
char *lithuanianWord;
int MAXWORDSIZE = 100;
lithuanianWord = (char*) malloc((MAXWORDSIZE + 1) * sizeof(char));
//size of our receive buffer, this is string length.
int MAXRECV = 100;
//set of socket descriptors
fd_set readfds;
//1 extra for null character, string termination
char *buffer, *message;
buffer = (char*) malloc((MAXRECV + 1) * sizeof(char));
for(i = 0 ; i < 30;i++)
{
client_socket[i] = 0;
}
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create a socket
if((master = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(master ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
}
puts("Bind done");
listen(master , 3);
puts("Waiting for incoming connections...");
addrlen = sizeof(struct sockaddr_in);
for(;;)
{
//clear the socket fd set
FD_ZERO(&readfds);
//add master socket to fd set
FD_SET(master, &readfds);
//add child sockets to fd set
for ( i = 0 ; i < max_clients ; i++)
{
s = client_socket[i];
if(s > 0)
{
FD_SET( s , &readfds);
}
}
//wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely
activity = select( 0 , &readfds , NULL , NULL , NULL);
if ( activity == SOCKET_ERROR )
{
printf("select call failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
//If something happened on the master socket , then its an incoming connection
if (FD_ISSET(master , &readfds))
{
if ((new_socket = accept(master , (struct sockaddr *)&address, (int *)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
//add new socket to array of sockets
for (i = 0; i < max_clients; i++)
{
if (client_socket[i] == 0)
{
client_socket[i] = new_socket;
printf("Adding to list of sockets at index %d \n" , i);
break;
}
}
valread = recv(new_socket, buffer, MAXRECV, 0);
buffer[valread] = '\0';
printf("%s:%d - %s \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port), buffer);
int i = findTranslation(buffer, &lithuanianWord);
if (i == 1) {
send(new_socket, lithuanianWord, strlen(lithuanianWord), 0);
} else {
lithuanianWord = "Vertimas nerastas";
send(new_socket, lithuanianWord, strlen(lithuanianWord), 0);
}
}
//else its some IO operation on some other socket
for (i = 0; i < max_clients; i++)
{
s = client_socket[i];
//if client present in read sockets
if (FD_ISSET( s , &readfds))
{
//get details of the client
getpeername(s , (struct sockaddr*)&address , (int*)&addrlen);
//Check if it was for closing , and also read the incoming message
//recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one).
valread = recv( s , buffer, MAXRECV, 0);
buffer[valread] = '\0';
if( valread == SOCKET_ERROR)
{
int error_code = WSAGetLastError();
if(error_code == WSAECONNRESET)
{
//Somebody disconnected , get his details and print
printf("Host disconnected unexpectedly , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
closesocket( s );
client_socket[i] = 0;
}
else
{
printf("recv failed with error code : %d" , error_code);
}
}
if ( valread == 0)
{
//Somebody disconnected , get his details and print
printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
closesocket( s );
client_socket[i] = 0;
}
//Echo back the message that came in
else
{
i = findTranslation(buffer, &lithuanianWord);
if (i == 1) {
send(s, lithuanianWord, strlen(lithuanianWord), 0);
} else {
lithuanianWord = "Vertimas nerastas";
send(s, lithuanianWord, strlen(lithuanianWord), 0);
}
}
}
}
}
closesocket(s);
WSACleanup();
return 0;
}
int findTranslation (char* word, char** lithuanianWord) {
FILE *fp;
int translationFound = 0;
const char englishWord[50];
char buffer[255];
fp = fopen("C:\\Users\\Martynas\\Documents\\Programų_sistemos\\IV semestras\\Tinklai\\async\\dictionary.txt","r");
if (fp == NULL) {
perror("Error");
}
while((fgets(buffer,255,fp)) != NULL) {
sscanf(buffer,"%s%s", englishWord, lithuanianWord);
if(strcmp(englishWord, word)==0 ) {
translationFound = 1;
break;
}
}
fclose(fp);
return translationFound;
}