[color="#000000"]Client writes that he received message "receiv", his ClientCiklas = 1, but [color="#FF0000"][color="#000000"]SOMEHOW ClientBytesSent [color="#000000"][color="#FF0000"]= [color="#FF0000"]-1
So my question is:
How is this possible that [color="#000000"][color="#FF0000"][color="#000000"][color="#FF0000"]ClientBytesSent = -1, if message is "receiv";
How it failed this line if(!strcmp(message, (char *)MSG_RECEIV))
[color="#000000"]
If need any additional information please write.
Im using: AF_INET, SOCK_STREAM, IPPROTO_TCP
[color="#FF0000"]How my socket shoud work:
1 Step: When Client connects to Server, server sends message [color="#808080"]send(socketCommunication, MSG_RECEIV, MSG_LENGTH, 0);
2 Step: If Client gets [color="#808080"]MSG_RECEIV [color="#000000"]it will send inputs from Client;
3 Step: Go back to 1st step;
[color="#FF0000"]Defines
// socket message types
#define MSG_LENGTH 6
#define MSG_RECEIV "receiv" // client/server got message
#define MSG_PLAYER "player" // received information is about player
#define MSG_INPUTS "inputs" // received information is players inputs
#define MSG_MESSAG "messag" // received information is message (pm)
Vec2f cursor;
bool mouseLeft = false;
bool mouseRight = false;
bool key[256];
bool keyP[256];
int ClientMsgBytes = -1;
int ClientBytesSent = -1;
int ClientCiklas = 0; // how much times program thread repeated
int CluentMsgGet[6];
int ServerBytesReceived = -1;
int ServerSendReceived = -1;
int ServerMsgBytes = -1;
int ServerCiklas = 0;; // how much times program thread repeated
[color="#FF0000"]Initialize
void InitEverything()
{
srand((unsigned int)time(NULL));
// initiate LAN play, server, client
// initiate server socket
if(MessageBox(hwnd, "Do you want to host a game", "Lan gameplay", MB_YESNO) == IDYES)
{
if(CreateSocket("0.0.0.0", 666, socketCommunication) != 0)
MessageBox(hwnd, "Failed to host", "Lan gameplay", MB_OK);
else
CreateThread(NULL, 0, Thread_Server, 0, 0, NULL);
thisIsServer = true;
}
else
{
if(JoinSocket("192.168.1.144", 666, socketCommunication) != 0)
MessageBox(hwnd, "Failed to join", "Lan gameplay", MB_OK);
else
CreateThread(NULL, 0, Thread_Client, 0, 0, NULL);
thisIsServer = false;
}
InitGuns();
InitPlayers();
}
[color="#FF0000"]Threads
DWORD WINAPI Thread_Server(__in LPVOID lpParameter)
{
char *buffer = (char *)malloc(256);
char message[MSG_LENGTH];
bool temp_key[256];
int bytes = 0;
int totalBytes = 0;
bytes = send(socketCommunication, MSG_RECEIV, MSG_LENGTH, 0);
EnterCriticalSection(&lock);
ServerSendReceived = bytes;
LeaveCriticalSection(&lock);
while(true)
{
bytes = recv(socketCommunication, message, MSG_LENGTH, 0);
EnterCriticalSection(&lock);
ServerMsgBytes = bytes;
LeaveCriticalSection(&lock);
if(!strcmp(message, MSG_INPUTS))
{
totalBytes = 0;
while(totalBytes < 256)
{
bytes = recv(socketCommunication, buffer, 256 - totalBytes, 0);
memcpy(temp_key + totalBytes, buffer, bytes);
totalBytes += bytes;
}
EnterCriticalSection(&lock);
ServerBytesReceived = totalBytes;
player[1].sInputs(temp_key, Vec2f(), false, false);
LeaveCriticalSection(&lock);
if(send(socketCommunication, MSG_RECEIV, MSG_LENGTH, 0) != 6)
MessageBox(hwnd, "Incorrect message", "ERROR", MB_OK);
}
else
{
MessageBox(hwnd, "Incorrect message", "ERROR", MB_OK);
}
ServerCiklas++;
}
free(buffer);
}
DWORD WINAPI Thread_Client(__in LPVOID lpParameter)
{
char *buffer = (char *)malloc(MSG_LENGTH + 256);
char message[MSG_LENGTH];
int bytes = 0;
int totalBytes = 0;
int match = 0;
while(true)
{
memset(message, 0, MSG_LENGTH);
bytes = recv(socketCommunication, message, MSG_LENGTH, 0);
EnterCriticalSection(&lock);
ClientMsgBytes = bytes;
memcpy(CluentMsgGet, message, MSG_LENGTH);
if(!strcmp(message, (char *)MSG_RECEIV))
{
memcpy(buffer, MSG_INPUTS, MSG_LENGTH);
memcpy(buffer + MSG_LENGTH, key, 256);
totalBytes = 0;
while(totalBytes < (MSG_LENGTH + 256))
{
bytes = send(socketCommunication, buffer + totalBytes, (MSG_LENGTH + 256) - totalBytes, 0);
error = WSAGetLastError();
totalBytes += bytes;
}
ClientBytesSent = totalBytes;
}
ClientCiklas++;
LeaveCriticalSection(&lock);
}
free(buffer);
}