Jump to content
  • Advertisement
Sign in to follow this  
alvidux

Having problem with sockets

This topic is 2589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[color="#000000"][color="#FF0000"]Problem
[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);
}

Share this post


Link to post
Share on other sites
Advertisement
You did a great job at making your post beautiful and well organized, but you forgot one of the most important parts: the problem. What's the issue?

Share this post


Link to post
Share on other sites
-1 is being returned because send() is failing. Check your errors, with winsock I think you can get an error string from WSALastError, from berkley standard sockets I believe lasterror(sock_fd); should provide more information. In most cases send will return -1 because the operation completely failed, depending on your blocking status the other end may have terminated unexpectedly or you may have never had a good connection in the first place. Anyway, the simple answer is you are getting -1 because send() is failing.

Share this post


Link to post
Share on other sites
-1 because send() is failing.


if(!strcmp(message, (char *)MSG_RECEIV))
{
// The point is that it never gets here...
}

Share this post


Link to post
Share on other sites
A few things I see.

#1, when sending character string, you're only sending the characters, and not the terminating \0. This will cause you problems, since, you receive the data, you only write the 6 characters, but the 7th byte could be anything. if it's not a 0, it will fail.

I would suggest, if you want to work this way, always sending the \0, and setting MSG_LENGTH 7
An even better alternative would be to send the length of the message as the 1st byte (or bytes), so you can send anything, and it doesn't have to be X bytes long every time.

#2, Why, when you create the server thread, do you immediately send? Who are you sending to? There will not be anyone connected at that time.

Maybe you need to show CreateSocket(), and Join Socket(), because, you may be using the sockets incorrectly as well. Does it take the socketCommunication as a reference? Is it global? Do you have a listening socket t begin with, and then the client communications socket after?

Share this post


Link to post
Share on other sites
\0


IT WORKED ! Thank you... I thought that i dont need to use "\0" end symbol.
Is there any way to do strcmp or something similar without \0 ?


#2, Why, when you create the server thread, do you immediately send? Who are you sending to? There will not be anyone connected at that time.

Maybe you need to show CreateSocket(), and Join Socket(), because, you may be using the sockets incorrectly as well. Does it take the socketCommunication as a reference? Is it global? Do you have a listening socket t begin with, and then the client communications socket after?


It sends when Server and Client connects

[color="#FF0000"]Socket functions ( "..." represents error)
#ifndef _SOCKET_SERVER_
#define _SOCKET_SERVER_

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#pragma comment(lib, "WS2_32.lib")


int CreateSocket(char *IP, int port, SOCKET &communicationSocket)
{
WSADATA wsaData;
SOCKET connectSocket = INVALID_SOCKET;
int result;

sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(IP);
service.sin_port = htons(port);


// initiates use of the Winsock DLL by a process
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
...

// creates a socket that is bound to a specific transport service provider
connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
...

// function associates a local address with a socket
result = bind(connectSocket, (SOCKADDR*)&service, sizeof(service));
...

// function places a socket in a state in which it is listening for an incoming connection.
result = listen(connectSocket, SOMAXCONN);
...

// function permits an incoming connection attempt on a socket.
communicationSocket = accept(connectSocket, NULL, NULL);
...

return 0;
}


int JoinSocket(char *IP, int port, SOCKET &communicationSocket)
{
WSADATA wsaData;
communicationSocket = INVALID_SOCKET;
int result;

/* initialize Winsock */
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
...

/* create socket */
communicationSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
...

/* connect */
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(IP);
clientService.sin_port = htons(port);
result = connect(communicationSocket, (SOCKADDR*)&clientService, sizeof(clientService));
...

return 0;
}

#endif

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!