Advertisment


Author Topic: [Sockets] Having some problems.  (Read 4502 times)

0 Members and 1 Guest are viewing this topic.

Offline SupremePower

  • Newbie
  • *
  • Posts: 4
  • Reputation 0
  • I need to set my profile up still!
    • View Profile
[Sockets] Having some problems.
« on: February 18, 2013, 06:54:02 pm »
I'm unable to send() to multiple clients. I have a different thread accepting clients, I can see when they join/leave, and stuff, but I'm having a problem sending messages/commands.

Here's what happens:
>I open server
>Open client
>Friend opens another client
>I can send messages and commands to MYSELF, but not the friend. If my friend opened the client first, I can send messages and commands to him, but not myself.

Code: [Select]
WaitForSingleObject(hMutex, INFINITE);        // Lock the mutex

        FD_SET TempSet = masterSet; // Make a temp location so we can unlock our mutex and let our accept thread run ASAP
        ReleaseMutex(hMutex);        // Unlock the mutex

        if (TempSet.fd_count == 0) // If a set is empty and we call form it, it returns a error, thats too much coding for me
        {                          // So we will simply check if there is anything in it, if so we will do something, else screw it -.-;
continue;
        }

        // select() has 5 member datas of use to us they are
        // - the number of file descriptors
        // - a FD_SET  checks readability
        // - a FD_SET  checks for writeability
        // - a FD_SET  checks for errors
        // - a wait time
        timeval waitTime; // Set up our interval
        waitTime.tv_sec = 0; // Set it to 0
        waitTime.tv_usec = 0; // Set it to 0

        int result = select(TempSet.fd_count, &TempSet, NULL, NULL, &waitTime); // Select a socket in out TempSet

if (result == 0)
{
continue;
}

        if (result == SOCKET_ERROR)
        {
            cout << "Server: Sorry Sir! there was a error in the Select() macro!\n";
continue;
        }
cout << "Type: ";
gets(outMessage);

for (unsigned int i = 0; i < TempSet.fd_count; i++)
{
                        DWORD clientSocket = TempSet.fd_array[i];

DWORD messageSize = strlen(outMessage);
messageSize = htonl(messageSize);
if ((nBytes = send(clientSocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR)
{
Log("send() failed");
}
messageSize = ntohl(messageSize);
if ((nBytes = send(clientSocket, outMessage, messageSize, 0)) == SOCKET_ERROR)
{
Log("send() failed");
}
                        //Bunch of stuff
                }

The client basically just recv()s and checks for certain commands (ex: "/CloseSocket") and displays the message if it doesn't match any of the commands. I just copy-pasted the code from one of celestialkey's tutorial and put them into my own executable and .dll.

Also, select() takes a long time to respond. Is there a way to shorten how long it waits by altering waitTime?

Tell me if you guys need more of the code, as I assume the problem is somewhere here.

[I barely understand sockets. I suck at C++. Bare with me.]

* Server.cpp (7.64 kB - downloaded 401 times.)
« Last Edit: February 19, 2013, 10:17:13 am by SupremePower »

Online Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3955
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: [Sockets] Having some problems.
« Reply #1 on: February 18, 2013, 11:55:02 pm »
I'm unable to send() to multiple clients. I have a different thread accepting clients, I can see when they join/leave, and stuff, but I'm having a problem sending messages/commands.

Here's what happens:
>I open server
>Open client
>Friend opens another client
>I can send messages and commands to MYSELF, but not the friend. If my friend opened the client first, I can send messages and commands to him, but not myself.

Code: [Select]
WaitForSingleObject(hMutex, INFINITE);        // Lock the mutex

        FD_SET TempSet = masterSet; // Make a temp location so we can unlock our mutex and let our accept thread run ASAP
        ReleaseMutex(hMutex);        // Unlock the mutex

        if (TempSet.fd_count == 0) // If a set is empty and we call form it, it returns a error, thats too much coding for me
        {                          // So we will simply check if there is anything in it, if so we will do something, else screw it -.-;
continue;
        }

        // select() has 5 member datas of use to us they are
        // - the number of file descriptors
        // - a FD_SET  checks readability
        // - a FD_SET  checks for writeability
        // - a FD_SET  checks for errors
        // - a wait time
        timeval waitTime; // Set up our interval
        waitTime.tv_sec = 0; // Set it to 0
        waitTime.tv_usec = 0; // Set it to 0

        int result = select(TempSet.fd_count, &TempSet, NULL, NULL, &waitTime); // Select a socket in out TempSet

if (result == 0)
{
continue;
}

        if (result == SOCKET_ERROR)
        {
            cout << "Server: Sorry Sir! there was a error in the Select() macro!\n";
continue;
        }
cout << "Type: ";
gets(outMessage);

for (unsigned int i = 0; i < TempSet.fd_count; i++)
{
                        DWORD clientSocket = TempSet.fd_array[i];

DWORD messageSize = strlen(outMessage);
messageSize = htonl(messageSize);
if ((nBytes = send(clientSocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR)
{
Log("send() failed");
}
messageSize = ntohl(messageSize);
if ((nBytes = send(clientSocket, outMessage, messageSize, 0)) == SOCKET_ERROR)
{
Log("send() failed");
}
                        //Bunch of stuff
                }

The client basically just recv()s and checks for certain commands (ex: "/CloseSocket") and displays the message if it doesn't match any of the commands. I just copy-pasted the code from one of celestialkey's tutorial and put them into my own executable and .dll.

Also, select() takes a long time to respond. Is there a way to shorten how long it waits by altering waitTime?

Tell me if you guys need more of the code, as I assume the problem is somewhere here.

[I barely understand sockets. I suck at C++. Bare with me.]

Could you post your entire code? You might need to upload your project if it is large. Select should not be taking 'long' to respond. Your initialization might be screwed up, or you are filling your socketset incorrectly. There can be a lot of issues. Once I have a better layout of your code, I can help you debug this.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline SupremePower

  • Newbie
  • *
  • Posts: 4
  • Reputation 0
  • I need to set my profile up still!
    • View Profile
Re: [Sockets] Having some problems.
« Reply #2 on: February 19, 2013, 10:19:36 am »
Attached.

Offline powerfear

  • - Secret Permissions Level 1 -
  • Jr. Member
  • **
  • Posts: 58
  • Reputation 64
  • Gender: Male
    • View Profile
Re: [Sockets] Having some problems.
« Reply #3 on: February 19, 2013, 01:04:08 pm »
A bit off-topic but if you wish to support concurrency and you have no experience using winsock is an horrible idea. I would go for a library like Boost Asio or POCO, they both are very efficient and will take care of the concurrency behind a worker thread system. They don't enforce any premade protocol on you either, only give you an equivalent read / write system. This will most likely save you a lots of time and you won't run into errors like this one, and judging by your current code which is not using the most efficient way to handle networking on windows, you will gain performance, and also cross-platform ability.

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio.html
http://pocoproject.org/

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 131
  • Reputation 34
  • Gender: Male
    • View Profile
Re: [Sockets] Having some problems.
« Reply #4 on: February 19, 2013, 11:09:40 pm »
Yes. Winsock is too pro. Go with Boost

Online Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3955
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: [Sockets] Having some problems.
« Reply #5 on: February 19, 2013, 11:56:25 pm »
Looking over your code now. It is late though, I'll post my response tomorrow.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline SupremePower

  • Newbie
  • *
  • Posts: 4
  • Reputation 0
  • I need to set my profile up still!
    • View Profile
Re: [Sockets] Having some problems.
« Reply #6 on: February 22, 2013, 08:22:26 pm »
Been a while.. Are you stuck on it?  ???
The first send() in the loop goes to every client, but the second doesn't seem to. I don't know enough about winsock to safely remove the first send () without affecting the other (due to the ntohl/htonl), but I'll play around with it tomorrow.

I considered the suggestions to use boost, but the template stuff sure will take some time to learn/memorize :P

Online Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3955
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: [Sockets] Having some problems.
« Reply #7 on: February 24, 2013, 08:06:20 pm »
Haven't had a chance to do more then download it. I keep getting called into work or dragged off to places with friends. I'll make some time tomorrow afternoon. I have nothing planned then, so I should be able to debug it.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Online Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3955
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: [Sockets] Having some problems.
« Reply #8 on: February 28, 2013, 09:51:09 pm »
Okay, here is the issue. Your TempSet has no sockets stored in it. Initially when you set it to the masterSet, everything was good. Once you used select though, you are polling for sockets that have data pending on them, not polling for sockets to send to, but sockets to recv from. Since you are trying to send a message out through the server, you shouldn't be selecting sockets to listen from, but to send to. I attached a solution that has a client/server example. I simply commented out your code that is causing the server not to select sockets and replaced a few types (int for SOCKET as a example).

Any questions can be answered.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1428
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: [Sockets] Having some problems.
« Reply #9 on: February 28, 2013, 10:31:41 pm »
A bit off-topic but if you wish to support concurrency and you have no experience using winsock is an horrible idea. I would go for a library like Boost Asio or POCO, they both are very efficient and will take care of the concurrency behind a worker thread system. They don't enforce any premade protocol on you either, only give you an equivalent read / write system. This will most likely save you a lots of time and you won't run into errors like this one, and judging by your current code which is not using the most efficient way to handle networking on windows, you will gain performance, and also cross-platform ability.

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio.html
http://pocoproject.org/

I recommend pytypes: http://www.melikyan.com/ptypes/
Projects:
[ Axios Engine ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline SupremePower

  • Newbie
  • *
  • Posts: 4
  • Reputation 0
  • I need to set my profile up still!
    • View Profile
Re: [Sockets] Having some problems.
« Reply #10 on: March 01, 2013, 05:27:11 pm »
Thank you very much. This was supposed to be the server socket for a hack, which is the client. I was using a different server that used list<> to sort the things out in the time being. There's probably a lot I can learn from this fixed code.