Commit 4cd036fb authored by Heiko Liermann's avatar Heiko Liermann
Browse files

bug fix chargen_server_TCP.c

parent f74cabaf
......@@ -2,7 +2,8 @@ bin_PROGRAMS = client client_old chargen_server_TCP get_addresses echo_se
client_SOURCES = client.c Socket.c
client_old_SOURCES = client_old.c Socket.c
chargen_server_TCP_SOURCES = discard_server_TCP.c Socket.c
chargen_server_TCP_SOURCES = chargen_server_TCP.c Socket.c
chargen_server_TCP_LDFLAGS = -pthread
get_addresses_SOURCES = get_addresses.c Socket.c
echo_server_IPv6_SOURCES = echo_server_IPv6.c Socket.c
echo_server_IPv6_LDFLAGS = -pthread
......
......@@ -25,7 +25,7 @@ TCP Based Character Generator Service
It is fairly likely that users of this service will abruptly decide
that they have had enough and abort the TCP connection, instead of
carefully closing it. The service should be prepared for either the
carfull close or the rude abort.
carefully close or the rude abort.
The data flow over the connection is limited by the normal TCP flow
control mechanisms, so there is no concern about the service sending
......@@ -81,30 +81,44 @@ int main(int argc, char **argv) {
void *thread_handler(void *args) {
uint8_t off = 0;
fd_set write_set, read_set;
int client_fd = *(int*) args;
free(args);
pthread_detach(pthread_self());
// allocate memory init it with 0
uint8_t *buf = (uint8_t *) calloc(1, 74 * sizeof(uint8_t));
if (buf == NULL)
uint8_t *write_buffer = (uint8_t *) calloc(1, 74 * sizeof(uint8_t));
uint8_t *read_buf = (uint8_t *) calloc(1, 74 * sizeof(uint8_t));
if (write_buffer == NULL || read_buf == NULL)
perror("error on calloc");
FD_ZERO(&read_set);
FD_ZERO(&write_set);
while (1) {
FD_SET(client_fd, &read_set);
FD_SET(client_fd, &write_set);
select(client_fd+1, &read_set, &write_set, NULL, NULL);
//rfc specifies this kind of output
//rfc wants 72char in a line
for (uint8_t i = 0; i < 72; i++)
buf[i] = ((i + off) % (126 - 32)) + 32;
if (FD_ISSET(client_fd, &write_set)) {
for (uint8_t i = 0; i < 72; i++)
write_buffer[i] = ((i + off) % (126 - 32)) + 32;
buf[72] = 13;
buf[73] = 10;
off= (off+1) % 95;
write_buffer[72] = 13;
write_buffer[73] = 10;
off = (off + 1) % 95;
if (write(client_fd, buf, sizeof(buf)) == -1)
break;
usleep(10000);
if (write(client_fd, write_buffer, sizeof(write_buffer)) == -1)
break;
}
if (FD_ISSET(client_fd, &read_set)) {
if (recv_wrapper(client_fd, read_buf, sizeof(read_buf), 0) == 0)
break;
}
}
close_wrapper(client_fd);
free(buf);
free(write_buffer);
return NULL;
}
\ No newline at end of file
......@@ -15,18 +15,19 @@ int main (int argc, char **argv) {
pthread_t thread_id;
struct sockaddr_in6 server_addr;
//create an IPv6 socket
// create an IPv6 socket
memset(&server_addr, 0, sizeof(server_addr));
server_fd = socket_wrapper(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
server_addr.sin6_family = AF_INET6;
// set ipv6 wildcard
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(strtol(argv[1], NULL, 10));
#ifdef HAVE_SIN_LEN
server_addr.sin6_len = sizeof(struct sockaddr_in);
#endif
// allow local address reuse
setsockopt_wrap(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));
//accept Ipv4 and Ipv6
// enable ipv4 compatibility
setsockopt_wrap(server_fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int));
//bind the socket
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment