Author: zhu
Date: Tue Aug 23 02:58:27 2016
New Revision: 72437
URL:
http://svn.reactos.org/svn/reactos?rev=72437&view=rev
Log:
Multi-threaded the server side of the TCP test.
Modified:
branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpclient/main.c
branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpserver/main.c
Modified: branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpclient/main.c
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2016/lwIP-tcpip/base/appli…
==============================================================================
--- branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpclient/main.c [iso-8859-1]
(original)
+++ branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpclient/main.c [iso-8859-1]
Tue Aug 23 02:58:27 2016
@@ -3,7 +3,7 @@
#include <winsock2.h>
#define LENGTH 255
-#define NUM_CLIENTS 2
+#define NUM_CLIENTS 128
DWORD WINAPI ClientThreadMain(LPVOID lpParam) {
SOCKET Sock;
@@ -69,6 +69,8 @@
int wmain(int argc, LPWSTR argv[]) {
WSADATA wsa;
+ SYSTEMTIME StartTime;
+ SYSTEMTIME EndTime;
HANDLE ClientThreadHandles[NUM_CLIENTS];
DWORD ClientThreadIDs[NUM_CLIENTS];
@@ -82,6 +84,8 @@
printf("Windows Socket API Startup Failed: %d\n", WSAGetLastError());
return 1;
}
+
+ GetSystemTime(&StartTime);
for (i = 0; i < NUM_CLIENTS; i++) {
ClientThreadHandles[i] = CreateThread(
@@ -100,11 +104,19 @@
WaitForMultipleObjects(NUM_CLIENTS, ClientThreadHandles, TRUE, INFINITE);
+ GetSystemTime(&EndTime);
+
+
for (i = 0; i < NUM_CLIENTS; i++) {
CloseHandle(ClientThreadHandles[i]);
}
fgets(buff, LENGTH, stdin);
+ printf("Elapsed: %d.%03d s\n",
+ EndTime.wSecond - StartTime.wSecond, EndTime.wMilliseconds -
StartTime.wMilliseconds);
+
+ fgets(buff, LENGTH, stdin);
+
return 0;
}
Modified: branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpserver/main.c
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2016/lwIP-tcpip/base/appli…
==============================================================================
--- branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpserver/main.c [iso-8859-1]
(original)
+++ branches/GSoC_2016/lwIP-tcpip/base/applications/network/tcpserver/main.c [iso-8859-1]
Tue Aug 23 02:58:27 2016
@@ -4,28 +4,77 @@
#define backlog 4
#define LENGTH 256
+struct ClientInfo {
+ int ClientID;
+ SOCKET ClientSock;
+};
+
+DWORD WINAPI ServerThreadMain(LPVOID lpParam) {
+ SOCKET ClientSock;
+
+ struct ClientInfo *CInfo;
+
+ char buff[LENGTH];
+
+ int count;
+ int ret;
+
+ CInfo = (struct ClientInfo *)lpParam;
+
+ count = CInfo->ClientID;
+ ClientSock = CInfo->ClientSock;
+
+ printf("Connection established to client %d\n", count);
+
+ ret = recv(ClientSock, buff, LENGTH, 0);
+ if (ret < 0) {
+ printf(" Receive failure on socket %d: %d\n", ClientSock,
WSAGetLastError());
+ return 1;
+ }
+ buff[ret] = '\0';
+ printf(" Received %d-byte message from client %d: %s\n", ret, count,
buff);
+
+ ret = send(ClientSock, buff, ret, 0);
+ if (ret == SOCKET_ERROR) {
+ printf("Send failed: %d\n", WSAGetLastError());
+ closesocket(ClientSock);
+ fgets(buff, LENGTH, stdin);
+ return 1;
+ }
+ printf("Message echoed\n");
+
+ closesocket(ClientSock);
+
+ free(CInfo);
+
+ return 0;
+}
+
int wmain(int argc, LPWSTR argv[]) {
- WSADATA wsa;
+ DWORD ServerThreadID;
+ WSADATA wsa;
SOCKET ListenSock;
SOCKET ClientSock;
+
+ struct ClientInfo *CInfo;
struct sockaddr_in ListenAddr;
struct sockaddr_in ClientAddr;
char buff[LENGTH];
- int ret;
+ int ret;
int addrSize;
int count;
printf("Server startup\n");
- ret = WSAStartup(MAKEWORD(2, 2), &wsa);
- if (ret != 0) {
- printf("Windows Socket API Startup Failed: %d\n", WSAGetLastError());
+ ret = WSAStartup(MAKEWORD(2, 2), &wsa);
+ if (ret != 0) {
+ printf("Windows Socket API Startup Failed: %d\n", WSAGetLastError());
fgets(buff, LENGTH, stdin);
- return 1;
- }
+ return 1;
+ }
memset(&ListenAddr, 0, sizeof(struct sockaddr_in));
ListenAddr.sin_family = AF_INET;
@@ -62,7 +111,6 @@
printf("Server listening socket is now listening\n");
count = 0;
- // Only a single server handler for now.
while (1) {
addrSize = sizeof(struct sockaddr_in);
memset(buff, 0, sizeof(buff));
@@ -73,26 +121,20 @@
closesocket(ListenSock);
WSACleanup();
fgets(buff, LENGTH, stdin);
- return 1;
+ exit(1);
}
- printf("Connection established to client %d\n", count);
+
+ CInfo = malloc(sizeof(*CInfo));
+ CInfo->ClientID = count;
+ CInfo->ClientSock = ClientSock;
- ret = recv(ClientSock, buff, LENGTH, 0);
- buff[LENGTH - 1] = '\0';
- printf(" Received %d-byte message from client %d: %s\n", ret, count,
buff);
-
- ret = send(ClientSock, buff, ret, 0);
- if (ret == SOCKET_ERROR) {
- printf("Send failed: %d\n", WSAGetLastError());
- closesocket(ListenSock);
- closesocket(ClientSock);
- WSACleanup();
- fgets(buff, LENGTH, stdin);
- return 1;
- }
- printf("Message echoed\n");
-
- closesocket(ClientSock);
+ CreateThread(
+ NULL,
+ 0,
+ ServerThreadMain,
+ (LPVOID)CInfo,
+ 0,
+ &ServerThreadID);
count++;
}
@@ -101,5 +143,5 @@
fgets(buff, LENGTH, stdin);
- return 0;
+ return 0;
}