Commit 3f3e0e0a authored by Heiko Liermann's avatar Heiko Liermann
Browse files

- bugfix

parent 173e403a
......@@ -17,7 +17,7 @@ int main(int argc, char **argv) {
memset(&server_addr, 0, sizeof(server_addr)); //socket address structure
server_addr.sin_family = AF_INET; // inte
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //specify the IP address allows the server to accept a client //connection on any interface,
server_addr.sin_port = htons(15001); //port 13 daytime server
server_addr.sin_port = htons(strtol(argv[2], NULL, 10));
bind_wrapper(server_fd, (const struct sockaddr *) &server_addr, (socklen_t) sizeof(server_addr)); //(1)The bind() function shall assign a local socket address address to a socket identified by descriptor socket
......@@ -66,7 +66,6 @@ int main(int argc, char **argv) {
if (FD_ISSET(client_fds[i], &read_set)) {
// write data into the buffer
recv_wrapper(client_fds[i], buf, BUFFER_SIZE, 0);
printf("Data discarded!");
// close the connection
close_wrapper(client_fds[i]);
FD_CLR(client_fds[i], &whole_set);
......
......@@ -3,27 +3,25 @@
//
#include "Socket.h"
void *handle_connection(void *arg);
int
main(void)
{
int main (int argc, char **argv) {
int server_fd;
int* thread_fd;
pthread_t client_thread;
struct sockaddr_in server_addr;
server_fd = socket_wrapper(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
server_fd = socket_wrapper(AF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
server_addr.sin_len = sizeof(struct sockaddr_in);
#endif
server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
server_addr.sin_port = htons(1337);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(strtol(argv[1], NULL, 10));
bind_wrapper(server_fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr));
bind_wrapper(server_fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr));
listen_wrapper(server_fd,1);
while(1) {
......@@ -40,6 +38,22 @@ main(void)
pthread_detach(client_thread);
}
close_wrapper(server_fd);
return (0);
}
void *handle_connection(void *arg) {
ssize_t read_len, send_len;
char buf[BUFFER_SIZE];
int client_fd = *(int*)arg;
free(arg);
do {
read_len = recv_wrapper(client_fd, (void *) buf, sizeof(buf), 0);
send_len = 0;
while (send_len < read_len) {
send_len += send_wrapper(client_fd, (void *) (buf+send_len), read_len - send_len, 0);
}
} while(read_len > 0);
close(client_fd);
return 0;
}
\ No newline at end of file
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