You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.0 KiB
C
135 lines
3.0 KiB
C
1 week ago
|
#include "client.h"
|
||
|
#include "server.h"
|
||
|
|
||
|
#define DEFAULT_BUFLEN 512
|
||
|
|
||
|
SOCKET ConnectSocket = INVALID_SOCKET;
|
||
|
|
||
|
DWORD WINAPI ClientWriteToPipe(LPDWORD dummy);
|
||
|
DWORD WINAPI ClientReadFromPipe(LPDWORD dummy);
|
||
|
|
||
|
VOID StartShellClient(TCHAR *ServerIP) {
|
||
|
WSADATA wsaData;
|
||
|
int iResult;
|
||
|
struct addrinfo* result = NULL, *ptr = NULL, hints;
|
||
|
|
||
|
// Initialize Winsock
|
||
|
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||
|
if (iResult != 0) {
|
||
|
printf("WSAStartup failed: %d\n", iResult);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// Create socket for client
|
||
|
|
||
|
ZeroMemory(&hints, sizeof(hints));
|
||
|
hints.ai_family = AF_INET;
|
||
|
hints.ai_socktype = SOCK_STREAM;
|
||
|
hints.ai_protocol = IPPROTO_TCP;
|
||
|
|
||
|
// Resolve the server address and port
|
||
|
iResult = getaddrinfo(ServerIP, DEFAULT_PORT, &hints, &result);
|
||
|
if (iResult != 0) {
|
||
|
printf("getaddrinfo failed: %d\n", iResult);
|
||
|
WSACleanup();
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// Attempt to connect to the first address returned by the call to getaddrinfo
|
||
|
ptr = result;
|
||
|
|
||
|
// Create a SOCKET for connecting to server
|
||
|
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
|
||
|
ptr->ai_protocol);
|
||
|
|
||
|
if (ConnectSocket == INVALID_SOCKET) {
|
||
|
printf("Error at socket(): %ld\n", WSAGetLastError());
|
||
|
freeaddrinfo(result);
|
||
|
WSACleanup();
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// Connect to a socket
|
||
|
//
|
||
|
// Connect to server.
|
||
|
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
|
||
|
if (iResult == SOCKET_ERROR) {
|
||
|
closesocket(ConnectSocket);
|
||
|
ConnectSocket = INVALID_SOCKET;
|
||
|
}
|
||
|
|
||
|
freeaddrinfo(result);
|
||
|
|
||
|
if (ConnectSocket == INVALID_SOCKET) {
|
||
|
printf("Unable to connect to server!\n");
|
||
|
WSACleanup();
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
HANDLE threads[2];
|
||
|
threads[0] = CreateThread(NULL, 0, ClientWriteToPipe, NULL, 0, NULL);
|
||
|
threads[1] = CreateThread(NULL, 0, ClientReadFromPipe, NULL, 0, NULL);
|
||
|
|
||
|
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
|
||
|
|
||
|
for (int i = 0; i < 2; ++i)
|
||
|
CloseHandle(threads[i]);
|
||
|
|
||
|
// Finalize
|
||
|
// shutdown the send half of the connection since no more data will be sent
|
||
|
iResult = shutdown(ConnectSocket, SD_SEND);
|
||
|
if (iResult == SOCKET_ERROR) {
|
||
|
printf("shutdown failed: %d\n", WSAGetLastError());
|
||
|
closesocket(ConnectSocket);
|
||
|
WSACleanup();
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// cleanup
|
||
|
closesocket(ConnectSocket);
|
||
|
WSACleanup();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
DWORD WINAPI ClientWriteToPipe(LPDWORD dummy) {
|
||
|
UNREFERENCED_PARAMETER(dummy);
|
||
|
DWORD iResult, dwRead, bSuccess;
|
||
|
|
||
|
for (;;) {
|
||
|
CHAR chBuf[DEFAULT_BUFLEN] = "";
|
||
|
|
||
|
bSuccess =
|
||
|
ReadFile(GetStdHandle(STD_INPUT_HANDLE), chBuf, DEFAULT_BUFLEN, &dwRead, NULL);
|
||
|
if (!bSuccess) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
iResult = send(ConnectSocket, chBuf, dwRead, 0);
|
||
|
if (iResult == SOCKET_ERROR) {
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
DWORD WINAPI ClientReadFromPipe(LPDWORD dummy) {
|
||
|
UNREFERENCED_PARAMETER(dummy);
|
||
|
DWORD iResult, dwRead, bSuccess;
|
||
|
|
||
|
for (;;) {
|
||
|
CHAR chBuf[DEFAULT_BUFLEN + 1] = "";
|
||
|
|
||
|
iResult = recv(ConnectSocket, chBuf, DEFAULT_BUFLEN, 0);
|
||
|
if (iResult > 0) {
|
||
|
chBuf[strlen(chBuf)] = 0;
|
||
|
bSuccess = WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), chBuf,
|
||
|
strlen(chBuf), &dwRead, NULL);
|
||
|
if (!bSuccess) {
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
}
|