I wrote a server which gets messages from clients and responds to them with words it has in its dictionary (dictionary I have in my file). For example:
Client wrote: cat
Server got: cat, Server wrote: miauuu! (and client can see the massege
from server in his window)
My dict.txt file looks like this:
cat
miauuu!
dog
raw,raw!
etc...
I have some problem with my code, cause my server gets messages from clients but still have problems to send a message to client... (in my client window i cant see a message from server). As a client I use telnet. Any ideas?
I mean, I run my server: ./server 9999 and telnet: telnet 127.0.0.1 9999 and can send a messages to server (and server can see them) but on telnets side I cant see any messages from server (also server seems to send an empty string (or nothing, I dont really know))
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
map <string, string> dict;
void* clients_service(void * arg)
{
int client_fd = *((int*)&arg), buffsize = 1024, bytes_num = 0;
char buffer[1024];
char word[1024];
while(1)
{
if ((bytes_num = read(client_fd, buffer, buffsize)) == -1)
{
perror("read error");
exit(1);
}
else if (bytes_num == 0)
{
fprintf(stdout, "connection closed.\n");
break;
}
buffer[bytes_num] = '\0';
fprintf(stdout, "Got: %s", buffer);
string from_client(buffer);
string to_client = dict[from_client];
char buff[1024];
strncpy(buff, to_client.c_str(), strlen(buff));
buff[strlen(buff) - 1] = '\0';
if ((write(client_fd, buff, strlen(buff))) == -1)
{
fprintf(stderr, "write error\n");
close(client_fd);
break;
}
fprintf(stdout, "I wrote: %s", buff);
}
close(client_fd);
}
void loadDictionary(map <string, string> &dict)
{
dict.clear();
ifstream file("dict.txt");
string s1, s2;
if(file.is_open())
{
while(file.good())
{
getline(file, s1);
getline(file, s2);
dict.insert(pair<string, string>(s1, s2));
}
}
file.close();
}
int main(int argc, char **argv)
{
int server_soc_fd, port, buffsize = 1024;
char buffer[buffsize];
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
loadDictionary(dict);
if (argc < 2)
{
fprintf(stderr, "Use : ./server {port number}\n");
exit(1);
}
port = atoi(argv[1]);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
server_soc_fd = socket(AF_INET, SOCK_STREAM, 0);
if(server_soc_fd < 0)
{
perror("socket");
exit(1);
}
if(bind(server_soc_fd,(struct sockaddr *)&addr, addrlen) < 0)
{
perror("bind");
exit(1);
}
if(listen(server_soc_fd, 10) < 0)
{
perror("listen");
exit(1);
}
fprintf(stdout, "\nWorking...\n\n");
while(true)
{
int client_soc_fd = accept(server_soc_fd, NULL, NULL);
pthread_t thread;
pthread_create(&thread, NULL, clients_service, (void*)client_soc_fd);
}
return 0;
}
This:
char buff[1024];
strncpy(buff, to_client.c_str(), strlen(buff));
buff[strlen(buff) - 1] = '\0';
will not fill buff
.
char *strncpy(char *dest, const char *src, size_t n);
copies n
bytes from src
to dest
.
So you better do:
const char * cstr = to_client.c_str();
size_t len = strlen(cstr);
strncpy(buff, cstr, max(len, sizeof(buff)-1));
buff[len] = 0;
or even better do:
const char * cstr = to_client.c_str();
size_t len = strlen(cstr);
if (!len)
continue;
if ((write(client_fd, cstr, len) == -1)
{
...
Also you should not rely on write()
writing all data at once.
So you might extend it, like so:
...
const char * cstr = to_client.c_str();
size_t len = strlen(cstr);
size_t lenWrittenTotal = 0;
while (lenWrittenTotal < len);
{
ssize_t lenWritten = write(client_fd, cstr + lenWrittenTotal , len - lenWrittenTotal);
if (lenWritten < 0)
{
perror("write");
break;
}
else if (lenWritten > 0)
{
lenWrittenTotal += lenWritten;
}
else
{
...
break;
}
};
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.