I am trying to write a cpp server that connects to node js client via socket programming. Bascially, i want cpp server to take a photo using canon SDK on client's signal.
I was able to connect server to the client but when the program tries to take a photo, it seems like all the code below "EdsSendCommand" function are not being executed at all (output on terminal does not say anything below the function and also it does not take any photo from camera). I have attached a code as well as image of the terminal on signal.
Can anyone see what the problem is here?
// realtest.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#pragma comment(lib, "Ws2_32.lib") // add library to additional dependencies tab of linker/Input settings for the project.
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <map>
#include <string>
#include <process.h>
#include "EDSDK.h"
#include "EDSDKErrors.h"
#include "EDSDKTypes.h"
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
#define noop (EdsObjectEventHandler)0
#define noopTwo (EdsPropertyEventHandler)0
#define noopThree (EdsStateEventHandler)0
void applicationRun();
EdsError getFirstCamera(EdsCameraRef* camera);
void takePicture(EdsCameraRef camera);
void threadProc(void* lParam);
void threadProc(void* lParam)
{
printf("entered thread proc function\n");
EdsCameraRef camera = (EdsCameraRef) lParam;
EdsError err;
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (!SUCCEEDED(hr))
{
printf("could not initialize COM");
}
else if (SUCCEEDED(hr)) {
printf("initilized COM");
}
err = EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0);
//err = EdsSendCommand(camera, kEdsCameraCommand_PressShutterButton, kEdsCameraCommand_ShutterButton_Completely);
if (err == EDS_ERR_OK) {
printf("succesfully pressed\n");
}
else {
printf("could not shutter button off error2: 0x%lx\n", err);
}
//err = EdsSendCommand(camera, kEdsCameraCommand_PressShutterButton, kEdsCameraCommand_ShutterButton_OFF);
if (err == EDS_ERR_OK) {
printf("succesfully pressed\n");
}
else {
printf("could not shutter button off error2: 0x%lx\n", err);
}
CoUninitialize();
_endthread();
}
void takePicture(EdsCameraRef camera)
{
printf("entered taking picture function\n");
// Executed by another thread
HANDLE hThread = (HANDLE)_beginthread(threadProc, 0, camera);
if (hThread) {
// Block until finished
::WaitForSingleObject(hThread, INFINITE);
}
else {
printf("no thread?\n");
}
}
EdsError getFirstCamera(EdsCameraRef* camera)
{
EdsError err = EDS_ERR_OK;
EdsCameraListRef cameraList = NULL;
EdsUInt32 count = 0;
// Get camera list
err = EdsGetCameraList(&cameraList);
// Get number of cameras
if (err == EDS_ERR_OK)
{
err = EdsGetChildCount(cameraList, &count);
if (count == 0)
{
printf("ERROR: camera list not found\n");
err = EDS_ERR_DEVICE_NOT_FOUND;
}
}
// Get first camera retrieved
if (err == EDS_ERR_OK)
{
printf("Got the first Camera\n");
err = EdsGetChildAtIndex(cameraList, 0, camera);
}
// Release camera list
if (cameraList != NULL) {
EdsRelease(cameraList);
cameraList = NULL;
printf("Relesaed camera list\n");
}
return err;
}
void applicationRun()
{
EdsError err = EDS_ERR_OK;
EdsCameraRef camera = NULL;
bool isSDKLoaded = false;
// Initialize SDK
err = EdsInitializeSDK();
if (err == EDS_ERR_OK)
{
printf("Canon SDK is succesfully loaded\n");
isSDKLoaded = true;
}
// Get first camera
if (err == EDS_ERR_OK)
{
err = getFirstCamera(&camera);
}
// Set Object event handler
if (err == EDS_ERR_OK)
{
err = EdsSetObjectEventHandler(camera, kEdsObjectEvent_All,
noop, NULL);
}
// Set Property event handler
if (err == EDS_ERR_OK)
{
err = EdsSetPropertyEventHandler(camera, kEdsPropertyEvent_All,
noopTwo, NULL);
}
// Set State event handler
if (err == EDS_ERR_OK)
{
err = EdsSetCameraStateEventHandler(camera, kEdsStateEvent_All,
noopThree, NULL);
}
// Open session with camera
if (err == EDS_ERR_OK)
{
err = EdsOpenSession(camera);
if (err == EDS_ERR_OK) {
printf("session is opened\n");
}
else {
printf("session is not opened with error: %#lx\n",err);
}
}
else {
printf("failure here\n");
}
/*
Do something here
*/
takePicture(camera);
// Close session with camera
if (err == EDS_ERR_OK)
{
printf("Closing session\n");
err = EdsCloseSession(camera);
}
// Release camera
if (camera != NULL)
{
printf("releasing camera\n");
EdsRelease(camera);
}
// Terminate SDK
if (isSDKLoaded)
{
printf("terminate SDK\n");
EdsTerminateSDK();
}
}
int main()
{
//printf("Hello world from printf\n");
//std::cout << "Hello World!\n";
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo* result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for the server to listen for client connections.
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Request command received at server: %s\n", recvbuf);
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
char cmd[DEFAULT_BUFLEN] = "photo";
printf("Bytes sent: %d\n", iSendResult);
applicationRun();
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
enter image description here
I have tried googling code examples that use canon SDK but couldn't find the solution. I have copied and paste the code example from canon SDK but still does not work..
Related
If you try and run my script you can see that GetQueuedCompletionStatus not will block after second wsarecv call. I don't understand why this code won't work.
The code is a bit messy but quite simple so it should work.
server:
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <thread>
#include <iostream>
using namespace std;
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
WSADATA wsaData;
int iResult;
HANDLE CompletionPort;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
OVERLAPPED Overlapped;
WSABUF DataBuf;
CHAR Buffer[200];
DWORD BytesSEND;
DWORD BytesRECV;
DWORD RecvBytes;
sockaddr_in local;
DWORD Flags;
int iSendResult;
void serverWorkerThread() {
OVERLAPPED lpol2;
DWORD Flags = 0;
DWORD BytesTransferred;
DWORD is = 0;
DWORD sentBytes;
bool kage = 0;
WSABUF wsabuf;
CHAR Buffer2[200];
while (true) {
if (GetQueuedCompletionStatus(CompletionPort, &BytesTransferred, &is, (LPOVERLAPPED*)& lpol2, INFINITE) == 0)
{
printf("GetQueuedCompletionStatus() failed with error %d\n", GetLastError());
}
else
printf("GetQueuedCompletionStatus() is OK!\n");
cout << DataBuf.buf << endl;
cout << is;
DataBuf.len = 14;
ZeroMemory(&(Overlapped), sizeof(OVERLAPPED));
if (WSASend(ClientSocket, &DataBuf, 1, &sentBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
}
}
else
printf("WSASend() is OK!\n");
ZeroMemory(&(lpol2), sizeof(OVERLAPPED));
wsabuf.buf = Buffer2;
wsabuf.len = 200;
BytesTransferred = 0;
is = 0;
ZeroMemory(&(Overlapped), sizeof(OVERLAPPED));
Flags = 0;
if (WSARecv(ClientSocket, &wsabuf, 1, &RecvBytes, &Flags, &Overlapped, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
}
}
else printf("WSARecv() is OK!\n");
}
}
int __cdecl main(void)
{
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
//Now we populate the sockaddr_in structure
local.sin_family = AF_INET; //Address family
local.sin_addr.s_addr = INADDR_ANY; //Wild card IP address
local.sin_port = htons((u_short)20248); //port to use
// Create a SOCKET for connecting to server
ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, (sockaddr*)& local, sizeof(local));
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
DataBuf.len = 2000;
DataBuf.buf = Buffer;
thread t1(serverWorkerThread);
t1.detach();
DWORD iss = 34543;
while (TRUE)
{
ClientSocket = WSAAccept(ListenSocket, NULL, NULL, NULL, 0);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
if (CreateIoCompletionPort((HANDLE)ClientSocket, CompletionPort, 43343, 0) == NULL)
{
printf("CreateIoCompletionPort() failed with error %d\n", GetLastError());
return 1;
}
else
printf("CreateIoCompletionPort() is OK!\n");
Flags = 0;
if (WSARecv(ClientSocket, &DataBuf, 1, &RecvBytes, &Flags, &Overlapped, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
return 1;
}
}
else printf("WSARecv() is OK!\n");
}
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
client:
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
using namespace std;
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(int argc, char** argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
SOCKADDR_IN ServerAddr;
// Server/receiver port to connect to
unsigned int Port = 20248;
//struct addrinfo* result = NULL,
// * ptr = NULL,
// hints;
const char* sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
cout << sendbuf << endl;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
// Create a SOCKET for connecting to server
ConnectSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 2;
}
// IPv4
ServerAddr.sin_family = AF_INET;
// Port no.
ServerAddr.sin_port = htons(Port);
// The IP address
ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
// Connect to server.
iResult = connect(ConnectSocket, (SOCKADDR*)& ServerAddr, sizeof(ServerAddr));
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 3;
}
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 4;
}
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 5;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 6;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while (iResult > 0);
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
i found the problem:
the clients shoutdown the connetion which made the GetQueuedCompletionStatus keep running
I'm trying to use two threads to run a server and a client.
I use a semaphore to block the client thread from doing too much before the server has been set up (ListenSocket, etc.).
When The server gets far enough, I use ReleaseSemaphore() but it returns an Invalid Handle error.
I've read that there are many possible issues which can be the source of this error.
The exact line is if (ReleaseSemaphore(semaphore, 1, NULL)){} which can be found about 55% of the way into the Server.cpp file
Header.h:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
unsigned int __stdcall servermain(void*);
unsigned int __stdcall clientmain(void*);
static HANDLE semaphore;
Source.cpp:
#include "Header.h"
int main(int argc, char* argv[])
{
HANDLE serverHandle, clientHandle;
semaphore = CreateSemaphore(
NULL,
0, //initial count
1, //max count
NULL);
if (semaphore == NULL){
printf("CreateSemaphore error: %d\n", GetLastError());
return 1;
}
/*call to the methods in the Client and Server files*/
serverHandle = (HANDLE)_beginthreadex(0, 0, &servermain, 0, 0, 0);
clientHandle = (HANDLE)_beginthreadex(0, 0, &clientmain, 0, 0, 0);
WaitForSingleObject(serverHandle, INFINITE);
WaitForSingleObject(clientHandle, INFINITE);
CloseHandle(serverHandle);
CloseHandle(clientHandle);
CloseHandle(semaphore);
return 0;
}
Server.cpp:
#include "Header.h"
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
unsigned int __stdcall servermain(void* data)
{
printf("SERVER STARTED. Thread: %i\n", GetCurrentThreadId());
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo
*result = NULL,
hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); //initiates the use of the Winsock DLL
if (iResult != 0) {
printf("%i Server:\t", GetCurrentThreadId());
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
//////////////////////////////////////
// Creating a Socket for the Server //
//////////////////////////////////////
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the local address and port to be used by the server
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("%i Server:\t", GetCurrentThreadId());
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for the server to listen for client connections
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("%i Server:\t", GetCurrentThreadId());
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
//////////////////////
// Binding a Socket //
//////////////////////
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("%i Server:\t", GetCurrentThreadId());
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
char answer = 'y'; // char answer[8]; // char *answer = "";
do{
printf("%i Server:\t", GetCurrentThreadId());
printf("Would you like to wait for client?(Y/N)\t");
answer = getchar(); // fgets(answer, 1, stdin);
printf("%c\n", answer);
} while (answer != 'y' && answer != 'n'); // double quotes "" are for c-strings. single quotes '' are for chars.
if (answer == 'n'){
return 1;
}
else printf("%i Server:\tListening for a client...\n", GetCurrentThreadId());
freeaddrinfo(result);
printf("Semaphore value: %i\n", semaphore);
//Now that the server is running, signal the semaphore to allow client to connect.
if (ReleaseSemaphore(semaphore, 1, NULL)) //ReleaseSemaphore() returns a bool value
{
printf("%i Server: Semaphore Released\n", GetCurrentThreadId());
}
else
{
printf("Server Release Semaphore error: %d\n", GetLastError());
printf("Server Semaphore value: %i\n", semaphore);
return 1;
}
///////////////////////////
// Listening on a Socket //
///////////////////////////
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("%i Server:\t", GetCurrentThreadId());
printf("Listen failed with error: %ld\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
////////////////////////////
// Accepting a Connection //
////////////////////////////
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("%i Server:\t", GetCurrentThreadId());
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket); // This will need to be changed if multiple clients shall be allowed
//////////////////////////////////////////////
// Receiving and Sending Data on the Server //
//////////////////////////////////////////////
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("%i Server:\t", GetCurrentThreadId());
printf("Bytes received: %d\n", iResult);
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket); //why close a socket if something doesn't send correctly?
WSACleanup();
return 1; //kills main()
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup(); //function terminates use of the Winsock 2 DLL (Ws2_32.dll)
return 1;
}
} while (iResult > 0);
// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("%i Server:\t", GetCurrentThreadId());
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
_endthreadex(0); //end thread within the thread.
return 0;
}
Client.cpp:
#include "Header.h"
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
unsigned int __stdcall clientmain(void* data)
{
printf("CLIENT STARTED. Thread: %i\n", GetCurrentThreadId());
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Validate the parameters
/*if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
return 1;
}*/
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("%i Client:\t", GetCurrentThreadId());
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
printf("Client winsock initialized\n");
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
/*iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}*/
printf("%i Client: before semaphore\n", GetCurrentThreadId());
//Wait for Server to be up and running
bool cont = TRUE;
while (cont)
{
DWORD returnval = WaitForSingleObject(semaphore, 0L); // 0 vs 0L ?
//printf("Semaphore value: %i\n", semaphore);
switch (returnval)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
printf("Thread %d: wait succeeded\n", GetCurrentThreadId());
break;
// The semaphore was nonsignaled, so a time-out occurred.
case WAIT_TIMEOUT:
printf("Thread %d: wait timed out\n", GetCurrentThreadId());
break;
}
}
printf("%i Client: after semaphore\n", GetCurrentThreadId());
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("%i Client:\t", GetCurrentThreadId());
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("%i Client:\t", GetCurrentThreadId());
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("%i Client:\t", GetCurrentThreadId());
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("%i Client:\t", GetCurrentThreadId());
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive until the peer closes the connection
do {
printf("%i Client:\t", GetCurrentThreadId());
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while (iResult > 0);
// cleanup
closesocket(ConnectSocket);
WSACleanup();
printf("%i Client:\t", GetCurrentThreadId());
printf("\nReturn to close.\n");
getchar(); //allow user to do see screen before shutting down
_endthreadex(0);
return 0;
}
You declare semaphore as a static HANDLE in header.h. That means that every source module that includes "header.h" will have its own copy of that variable. Thus, the semaphore in main.cpp that holds the Windows semaphore handle is a different variable than the semaphore referenced in server.cpp.
Remove the static from the declaration in header.h, replace it with extern, and add a definition for it in main.cpp.
I made a window using the Win32 API and it needs to enter a message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
How do I create a socket server and start receiving clients, if my main thread is processing UI messages?
I tried putting all of the server code in a thread using CreateThread() from WinMain(), but it just crashes with an error:
Exception thrown at 0x00000000 in test.exe: 0xC0000005: Access violation executing location 0x00000000.
Here is the thread code:
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
// Initialize Winsock
WSADATA wsaData;
auto iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return EXIT_FAILURE;
}
addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
PADDRINFOA pAddrInfo = nullptr;
iResult = getaddrinfo(nullptr, PORT, &hints, &pAddrInfo);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return EXIT_FAILURE;
}
while (true) {
// Create a SOCKET for connecting to server
auto ListenSocket = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(pAddrInfo);
WSACleanup();
return EXIT_FAILURE;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, pAddrInfo->ai_addr, static_cast<int>(pAddrInfo->ai_addrlen));
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(pAddrInfo);
closesocket(ListenSocket);
WSACleanup();
return EXIT_FAILURE;
}
freeaddrinfo(pAddrInfo);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return EXIT_FAILURE;
}
// Accept a client socket
auto ClientSocket = accept(ListenSocket, nullptr, nullptr);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return EXIT_FAILURE;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
for (;;)
{
char recvbuf[1] = { 0 };
auto cbLen = recv(ClientSocket, recvbuf, sizeof(recvbuf), 0);
if (cbLen > 0) {
if (recvbuf[0] == 0xDA)
{
Hide();
}
continue;
}
else if (cbLen == 0) {
printf("Connection closing...\n");
break;
}
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return EXIT_FAILURE;
}
}
// shutdown receive operations since we're done
iResult = shutdown(ClientSocket, SD_RECEIVE);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return EXIT_FAILURE;
}
// cleanup
closesocket(ClientSocket);
}
WSACleanup();
return 0;
}
This is a very confusing question as it asks one question in the title, but the content addresses a different problem (threads crashing).
To address the primary question: Microsoft did add a way to handle socket communications in a GUI thread friendly way: WSAAsyncSelect. This will send socket events as messages to your applications message queue - typically an invisible window is created to handle the messages.
I created a simple client-server application using winsock.
I want to send message to the server as a response to the user input - but it doesnt work.
I created a function sendData() which sends "test" to the server.
If I call the func Immediately after connectind the server - it works fine, but if i call it from the main or another class - the message isn't accept by the server.
any suggestions?
Client code:
#include "StdAfx.h"
#include "CommHandler.h"
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
SOCKET CommHandler::m_socket = INVALID_SOCKET;
CommHandler::CommHandler(void)
{
init();
}
//===============================================================
CommHandler::~CommHandler(void)
{
// cleanup
closesocket(m_socket);
WSACleanup();
}
//===============================================================
bool CommHandler::init()
{
return (establishConnection("127.0.0.1"));
}
//===============================================================
bool CommHandler::sendData()
{
if (m_socket == INVALID_SOCKET) {
printf("send msg failed; INVALID_SOCKET: %d\n", WSAGetLastError());
return false;
}
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
// Send an initial buffer
int iResult = send(m_socket, sendbuf, (int) strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(m_socket);
WSACleanup();
return false;
}
printf("sending initial message to server!\n");
printf("Bytes Sent: %ld\n", iResult);
iResult = recv(m_socket, recvbuf, DEFAULT_BUFLEN, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
return true;
}
//===============================================================
bool CommHandler::establishConnection(const std::string ip)
{
return (initWinsock() && createSocket(ip));
}
//===============================================================
bool CommHandler::initWinsock()
{
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2,2), &m_wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return false;
}
printf("WSAStartup succeeded.\n");
return true;
}
//===============================================================
bool CommHandler::createSocket(const std::string ip)
{
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
int iResult = getaddrinfo(ip.c_str(), DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return false;
}
// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr=result;
// Create a SOCKET for connecting to server
m_socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (m_socket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return false;
}
printf("socket creation succeeded.\n");
// Connect to server.
iResult = connect( m_socket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(m_socket);
m_socket = INVALID_SOCKET;
}
freeaddrinfo(result);
if (m_socket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return false;
}
printf("connected to server!\n");
// Send an initial buffer
sendData(); //this send data is received by the server
sendData(); //this send data is received by the server
return true;
}
Server code:
#include "StdAfx.h"
#include "CommHandler.h"
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
CommHandler::CommHandler(void):m_listenSocket(INVALID_SOCKET), m_clientSocket(INVALID_SOCKET)
{
}
//===============================================================
CommHandler::~CommHandler(void)
{
}
//===============================================================
bool CommHandler::init()
{
if (establishConnection()) {
return (receiveData());
}
return false;
}
//===============================================================
bool CommHandler::receiveData()
{
char recvbuf[DEFAULT_BUFLEN];
int iResult, iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
// Receive until the peer shuts down the connection
do {
printf("in receiveData function\n");
iResult = recv(m_clientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
std::cout<<recvbuf<<std::endl;
// Echo the buffer back to the sender
iSendResult = send(m_clientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(m_clientSocket);
WSACleanup();
return false;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0) {
printf("Connection closing...\n");
}
else {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(m_clientSocket);
WSACleanup();
return false;
}
} while (iResult > 0);
printf("exit receiveData function with no errors (finish while)\n");
return true;
}
//===============================================================
bool CommHandler::establishConnection()
{
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2,2), &m_wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return false;
}
printf("WSAStartup succeeded.\n");
struct addrinfo *result = NULL, *ptr = NULL, hints;
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET; //used to specify the IPv4 address family
hints.ai_socktype = SOCK_STREAM; //used to specify a stream socket
hints.ai_protocol = IPPROTO_TCP; //used to specify the TCP protocol
hints.ai_flags = AI_PASSIVE;
// Resolve the local address and port to be used by the server
int iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return false;
}
// Create a SOCKET for the server to listen for client connections
m_listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (m_listenSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return false;
}
printf("socket creation succeeded.\n");
//bind the socket:
// Setup the TCP listening socket
iResult = bind( m_listenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(m_listenSocket);
WSACleanup();
return false;
}
freeaddrinfo(result);
printf("bind the socket.\n");
//listen the socket
if ( listen( m_listenSocket, SOMAXCONN ) == SOCKET_ERROR ) {
printf( "Listen failed with error: %ld\n", WSAGetLastError() );
closesocket(m_listenSocket);
WSACleanup();
return false;
}
printf("listen the socket.\n");
// Accept a client socket
m_clientSocket = accept(m_listenSocket, NULL, NULL);
if (m_clientSocket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(m_listenSocket);
WSACleanup();
return false;
}
printf("accept client socket.\n");
return true;
}
client's main:
int main()
{
CommHandler ch;
if (!ch.init())
return 1;
ch.sendData(); //this msg isn't received by server !!!
system("pause");
return(0);
}
Thanks!
i found the problem. I call init() functions twice and it broke the connections :|
I want to create a slot in Qt or something similar in C++ for recv() function in TCP,so that when the socket receive the data it will call the slot automatically,I can do it by using QTcpSockets but I can't use them due to some limitations.currently i am using timer to receive data from recv() function.
Please guide me in right direction.
tcp.h
ifndef TCP_H
#define TCP_H
#pragma once
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>
#include "config.h"
#include "tcp_packets.h"
#include "change_ending.h"
#include "md5.h"
#pragma comment(lib, "Ws2_32.lib")
class CTcpClient
{
private:
char* szServerName;
char* Port;
SOCKET ConnectSocket;
md5checksum Md5Var;
public:
int Size;
bool Send(char* szMsg,int len);
CTcpClient(char* servername,char* port)
{
Port=port;
szServerName = servername;
ConnectSocket = INVALID_SOCKET;
Size=0;
}
bool Start();
void Stop();
// Free the resouces
// Receive message from server
char recvbuf[DEFAULT_BUFFER_PKT_LENGTH];
void Recv();
};
tcp.cpp
#endif // TCP_H
tcp.cpp
#include "tcp.h"
#include "config.h"
bool CTcpClient::Start() {
WSADATA wsaData;
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return false;
}
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
//hints.ai_protocol = SOL_SOCKET;
//Resolve the server address and port
iResult = getaddrinfo(szServerName,Port, &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return false;
}
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(): %d\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return false;
}
// 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 false;
}
return true;
}
void CTcpClient::Stop() {
int iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", WSAGetLastError());
}
closesocket(ConnectSocket);
WSACleanup();
}
bool CTcpClient::Send(char* szMsg,int len)
{
int iResult = send(ConnectSocket, szMsg, len, 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
Stop();
return false;
}
return true;
}
void CTcpClient::Recv()
{
int iResult = recv(ConnectSocket, recvbuf, DEFAULT_REC_BUFFER_PKT_LENGTH, 0);
Size=iResult;
if (iResult > 0)
{
char msg[DEFAULT_REC_BUFFER_PKT_LENGTH];
memset(&msg, 0, sizeof(msg));
strncpy(msg, recvbuf, iResult);
printf("Received: %s\n", msg);
}
//return false;
}
If you can, you may create separate thread for Recv() function and give it callback function as parameter. Then in separate thread call callback function every time you get data from socket.
void callback(char *data){
printf("Received: %s\r\n", data);
}
void* thread_function(void *input){
void (*callb)(char*) = (void (*)(char*))input;
int recieve;
while(1){
receive = recv();
if(receive > 0)
callb();
}
return NULL;
}
int main(){
pthread_t thread;
pthread_create(thread, NULL, thread_function, &callback);
/* do some stuff */
pthread_join(thread);
return 0;
}
Something like this. A lot of variables and arguments are missing, but it shows main idea.