diff --git a/client.c b/client.c index 01eb0177cdd02f..a9fe4cf6c982b0 100644 --- a/client.c +++ b/client.c @@ -19,10 +19,83 @@ static void bcopy(void *dest, void *source, int length) static void sleep(int seconds) { -return; Sleep(seconds * 1000); } +#if WIN32 +int win_start_process(const char* exename, int exestdin, int exestdout, int exestderr) +{ + int result; + STARTUPINFO si; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + si.dwFlags = STARTF_USESTDHANDLES; + { + struct { + int fd; + HANDLE fall_back_hdl; + HANDLE *si_hdl; + } ioe[] = { + { + exestdin, + GetStdHandle(STD_INPUT_HANDLE), + &si.hStdInput + }, + { + exestdout, + GetStdHandle(STD_OUTPUT_HANDLE), + &si.hStdOutput + }, + { + exestderr, + GetStdHandle(STD_ERROR_HANDLE), + &si.hStdError + }, + + }; + int i; + for (i = 0; i < sizeof(ioe) / sizeof(ioe[0]); i++) { + if (ioe[i].fd < 0) { + *ioe[i].si_hdl = ioe[i].fall_back_hdl; + } + else { + HANDLE hdl; + hdl = (HANDLE)_get_osfhandle(ioe[i].fd); + if (hdl != INVALID_HANDLE_VALUE) { + *ioe[i].si_hdl = hdl; + } + else { + *ioe[i].si_hdl = ioe[i].fall_back_hdl; + } + } + } + } + + { + PROCESS_INFORMATION pi; + BOOL state; + state = CreateProcess(exename, NULL, NULL, NULL, TRUE, 0, + NULL, + NULL, + &si, + &pi); + + result = state ? 0 : -1; + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + } + + return result; +} + +int read_all_from_socket_with_process(int sockfd) +{ + int result; + result = win_start_process("svr_reader.exe", sockfd, -1, -1); + return result; +} + +#endif #include static int mingw_socket(int domain, int type, int protocol) { @@ -190,17 +263,49 @@ void error(const char *msg) } +int read_all_from_socket_in_process(int sockfd) +{ +#define BUFFER_SIZE 256 + char buffer[BUFFER_SIZE]; + int n; + int result; + result = 0; + do { + bzero(buffer, sizeof(buffer)); + n = read(sockfd,buffer,sizeof(buffer) - 1); + if (n == 0) { + break; + } + else if (n < 0) + { + error("Error reading from socket"); + result = -1; + } + else { + fputs(buffer, stdout); + fflush(stdout); + } + } while (1); + return result; +} +int read_all_from_socket(int sockfd) +{ + int result; + result = read_all_from_socket_in_process(sockfd); + return result; +} + int main(int argc, char *argv[]) { - int sockfd, portno, n; + int sockfd, portno; struct sockaddr_in serv_addr; struct hostent *server; - - char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } + + #ifdef WIN32 WSADATA wsa; if (WSAStartup(MAKEWORD(2,2), &wsa)) error("WSAStartup"); @@ -221,16 +326,12 @@ int main(int argc, char *argv[]) serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); - printf("Sleeping 1 second\n"); - sleep(1); - bzero(buffer,256); - n = read(sockfd,buffer,255); - if (n < 0) - error("ERROR reading from socket"); - printf("%s\n",buffer); + read_all_from_socket(sockfd); close(sockfd); #ifdef WIN32 WSACleanup(); #endif return 0; } + + diff --git a/clt_writer.c b/clt_writer.c new file mode 100644 index 00000000000000..29998b9cad883b --- /dev/null +++ b/clt_writer.c @@ -0,0 +1,46 @@ +#include +#include +#include + +#include +#if WIN32 +#include +#endif +int write_all(int max_line) +{ + int i; + int result; + char buffer[256]; + result = 0; + for (i = 0; i < max_line; i++) + { + int write_size; + size_t n; + memset(buffer, 0, 256); + write_size = sprintf(buffer, "% 4d I got the connection\n",i); + n = fwrite(buffer, 1, write_size, stdout); + if (n < write_size) + { + fputs("ERROR writing to socket\n", stderr); + result = -1; + break; + } + } + + return result; +} + + +int main(int argc, char *argv[]) +{ + int result; + int count_of_line; + if (argc < 2) { + count_of_line = 10; + } else { + count_of_line = atoi(argv[1]); + } + result = write_all(count_of_line); + return result; +} + diff --git a/server.c b/server.c index 8b6d52d3fac6f0..1529af81651e1e 100644 --- a/server.c +++ b/server.c @@ -97,18 +97,161 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz) } #define accept mingw_accept +int win_start_process(const char* exename, char * args, int exestdin, int exestdout, int exestderr) +{ + int result; + STARTUPINFO si; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + si.dwFlags = STARTF_USESTDHANDLES; + { + struct { + int fd; + HANDLE fall_back_hdl; + HANDLE *si_hdl; + } ioe[] = { + { + exestdin, + GetStdHandle(STD_INPUT_HANDLE), + &si.hStdInput + }, + { + exestdout, + GetStdHandle(STD_OUTPUT_HANDLE), + &si.hStdOutput + }, + { + exestderr, + GetStdHandle(STD_ERROR_HANDLE), + &si.hStdError + }, + + }; + int i; + for (i = 0; i < sizeof(ioe) / sizeof(ioe[0]); i++) { + if (ioe[i].fd < 0) { + *ioe[i].si_hdl = ioe[i].fall_back_hdl; + } + else { + HANDLE hdl; + hdl = (HANDLE)_get_osfhandle(ioe[i].fd); + if (hdl != INVALID_HANDLE_VALUE) { + *ioe[i].si_hdl = hdl; + } + else { + *ioe[i].si_hdl = ioe[i].fall_back_hdl; + } + } + } + } + + { + PROCESS_INFORMATION pi; + BOOL state; + state = CreateProcess(exename, args, NULL, NULL, TRUE, 0, + NULL, + NULL, + &si, + &pi); + + result = state ? 0 : -1; + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + } + + return result; +} #endif +int write_all_to_socket_with_process(int sockfd, int max_line) +{ + char buffer[128]; + int result; + memset(buffer, 0, sizeof(buffer)); + sprintf(buffer, " %d", max_line); + result = win_start_process("clt_writer.exe", buffer, -1, sockfd, -1); + return result; +} + +int write_all_to_socket_in_process(int sockfd, int max_line) +{ + int i; + int result; + char buffer[256]; + result = 0; + for (i = 0; i < max_line; i++) + { + int write_size; + int n; + bzero(buffer,256); + write_size = sprintf(buffer, "% 4d I got the connection\n",i); + n = write(sockfd, buffer, write_size); + if (n < 0) + { + error("ERROR writing to socket"); + result = -1; + break; + } + } + + return result; +} + +int write_all_to_socket(int sockfd, int line_count, int in_process) +{ + int result; + if (in_process) + { + result = write_all_to_socket_in_process(sockfd, line_count); + } + else + { + result = write_all_to_socket_with_process(sockfd, line_count); + } + return result; +} + int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; socklen_t clilen; - char buffer[256]; - struct sockaddr_in serv_addr, cli_addr; + struct sockaddr_in serv_addr, cli_addr; int n; + int line_count; + int in_proc; + line_count = 10; + in_proc = 1; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } + { + int i; + int parse_mode; + const static int PM_NORMAL = 0; + const static int PM_LINE = 1; + const static int PM_PROC = 2; + parse_mode = PM_NORMAL; + for (i = 0; i < argc; i++) + { + if (parse_mode == PM_NORMAL) + { + if (strcmp(argv[i], "-l") == 0) + { + parse_mode = PM_LINE; + } + else if (strcmp(argv[i], "-p") == 0) + { + in_proc = 0; + } + } + else if (parse_mode == PM_LINE) + { + line_count = atoi(argv[i]); + parse_mode = PM_NORMAL; + } + } + } + #ifdef WIN32 WSADATA wsa; if (WSAStartup(MAKEWORD(2,2), &wsa)) error("WSAStartup"); @@ -131,9 +274,7 @@ int main(int argc, char *argv[]) &clilen); if (newsockfd < 0) error("ERROR on accept"); - bzero(buffer,256); - n = write(newsockfd,"I got the connection",20); - if (n < 0) error("ERROR writing to socket"); + write_all_to_socket(newsockfd, line_count, in_proc); close(newsockfd); close(sockfd); #ifdef WIN32