Hi all,
I'm very interested in using the state-threads library or actually the
server-example that's implemented with this library, but I'm experiencing
some troubles.
I have quite some experience in coding C, but little experience with either
threads or tcp/ip programming in C.
The problem is this:
I've tried to modifiy the server example to be able to return inputted
strings in uppercase. Herefor I changed handle_session(...) to look like this:
---
void handle_session(long srv_socket_index, st_netfd_t cli_nfd) {
char buf[512];
struct in_addr *from = st_netfd_getspecific(cli_nfd);
if (st_read(cli_nfd, buf, sizeof(buf), SEC2USEC(REQUEST_TIMEOUT)) < 0) {
err_sys_report(errfd, "WARN: can't read request from %s: st_read",
inet_ntoa(*from));
return;
}
if (st_write(cli_nfd, to_uppercase(buf), strlen(buf), -1) == -1) {
err_sys_report(errfd, "WARN: can't write response to %s: st_write",
inet_ntoa(*from));
return;
}
RQST_COUNT(srv_socket_index)++;
}
---
and added the function:
---
char* to_uppercase(char *string) {
int i;
for (i = 0; string[i] != '\0'; i++)
string[i] = toupper(string[i]);
return string;
}
---
This seemed to work at first, but today I discovered that the array of
characters `buf' sometimes later on contained text I had inputted earlier,
but in uppercase. I suspect that I'm either making a dumb mistake or are
totally missing some vital concept here.
Might using '\0' not be a smart way to determine the end of a string or am I
making some off-by-one error which causes this?
To further demonstrate the problem, the following examples:
An example of a working session is:
---
[matthijs@helena /tmp]$ telnet helena 8000
Trying 192.168.79.13...
Connected to helena.
Escape character is '^]'.
test
TEST
Connection closed by foreign host.
---
Sometimes something like this happens:
---
[matthijs@helena /tmp]$ telnet helena 8000
Trying 192.168.79.13...
Connected to helena.
Escape character is '^]'.
test
TEST
T IS EEN MEGA-LANGE-TEST. EENS ZIEN WAT HIER GAAT GEBEUREN
Connection closed by foreign host.
---
Here `T IS EEN MEGA-LANGE-TEST. EENS ZIEN WAT HIER GAAT GEBEUREN' is
something I typed in earlier in lowercase.
I don't understand why this would happen and I'm not even sure that this has
to do with (the threading of) the state-threads library. I sure hope someone
can shed some light at this problem.
TIA,
--
Matthijs
|