|
You last visited: Today at 02:49
Advertisement
[C++] Winsock + Select
Discussion on [C++] Winsock + Select within the C/C++ forum part of the Coders Den category.
11/30/2009, 19:33
|
#1
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
[C++] Winsock + Select
I solved this Problem adding a "continue;" at
PHP Code:
if(FD_ISSET(aSocket, &fdSet)) { AddSocket(accept(aSocket, NULL, NULL)); continue; }
|
|
|
12/02/2009, 08:49
|
#2
|
elite*gold: 20
Join Date: Mar 2007
Posts: 4,277
Received Thanks: 2,990
|
is recv executed? Where does it stop - FD_ISSET(m_clients[i], &fdSet)? Give more info.
|
|
|
12/02/2009, 13:18
|
#3
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
Code:
while(1)
{
char buf[256];
long rc;
FD_ZERO(&fdSet);
FD_SET(aSocket, &fdSet);
FOR_EACH(m_clients, i)
{
FD_SET(m_clients[i], &fdSet);
FD_SET(m_servers[i], &fdSet);
}
select(0, &fdSet, NULL, NULL, NULL);
if(FD_ISSET(aSocket, &fdSet))
{
AddSocket(accept(aSocket, NULL, NULL));
}
FOR_EACH(m_clients, i)
{
if(FD_ISSET(m_clients[i], &fdSet))
{
[COLOR="Red"] rc = recv(m_clients[i], buf, 256, 0);
if(rc == 0 || rc == SOCKET_ERROR)
{
Close(i);
}
else
{
send(m_servers[i], buf, rc, 0);
}[/COLOR]
}
if(FD_ISSET(m_servers[i], &fdSet))
{
[COLOR="Red"] rc = recv(m_servers[i], buf, 256, 0);
if(rc == 0 || rc == SOCKET_ERROR)
{
Close(i);
}
else
{
send(m_clients[i], buf, rc, 0);
}[/COLOR]
}
}
}
The red highlighted Code is never executed. So the if statement returns always false, but why?
|
|
|
12/02/2009, 17:51
|
#4
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
|
is a client connected and accepted?
is wsastartup initialized?
|
|
|
12/02/2009, 18:11
|
#5
|
elite*gold: 20
Join Date: Mar 2007
Posts: 4,277
Received Thanks: 2,990
|
1)Why are you using select?
2)Why are you not checking return val of select()?
3)Problem(first execution):
Code:
FD_ZERO(&fdSet); //fdSet.fd_count = 0;
FD_SET(aSocket, &fdSet); //fdSet.fd_count = 1;(listening socket)
FOR_EACH(m_clients, i) //this isn't executed. --> fdSet.fd_count = 1
{
FD_SET(m_clients[i], &fdSet);
FD_SET(m_servers[i], &fdSet);
}
select(0, &fdSet, NULL, NULL, NULL); //fdSet contains only one socket,the Lsock
if(FD_ISSET(aSocket, &fdSet))
{
AddSocket(accept(aSocket, NULL, NULL)); //fdSet contains Lsock and Connected sock
}
FOR_EACH(m_clients, i) //executed once
{
if(FD_ISSET(m_clients[i], &fdSet)) //false.Did you select the m_clients[0] socket? No,so it won't be executed.
{
...
}
|
|
|
12/02/2009, 18:23
|
#6
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
@MrSm!th:
WSAStartup -> Yes
Client connected and accepted -> yes
1) Because i want to handle many clients
2) I'll check it now (wait please :P)
3) Should i add another select? Or how could i resolve the problem?
|
|
|
12/02/2009, 18:25
|
#7
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
|
Quote:
Originally Posted by InvincibleNoOB
1)Why are you using select?
2)Why are you not checking return val of select()?
3)Problem(first execution):
Code:
FD_ZERO(&fdSet); //fdSet.fd_count = 0;
FD_SET(aSocket, &fdSet); //fdSet.fd_count = 1;(listening socket)
FOR_EACH(m_clients, i) //this isn't executed. --> fdSet.fd_count = 1
{
FD_SET(m_clients[i], &fdSet);
FD_SET(m_servers[i], &fdSet);
}
select(0, &fdSet, NULL, NULL, NULL); //fdSet contains only one socket,the Lsock
if(FD_ISSET(aSocket, &fdSet))
{
AddSocket(accept(aSocket, NULL, NULL)); //fdSet contains Lsock and Connected sock
}
FOR_EACH(m_clients, i) //executed once
{
if(FD_ISSET(m_clients[i], &fdSet)) //false.Did you select the m_clients[0] socket? No,so it won't be executed.
{
...
}
|
cause select is a goog way to do that
|
|
|
12/02/2009, 18:36
|
#8
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
Edit:
The recv Function is blocking the whole while loop. How to resolve this problem?
Thanks for your Help
|
|
|
12/02/2009, 20:01
|
#9
|
elite*gold: 20
Join Date: Mar 2007
Posts: 4,277
Received Thanks: 2,990
|
Perhaps use WSAAsyncSelect() or WSAEventSelect.You wouldn't even need a while loop.
|
|
|
12/02/2009, 20:05
|
#10
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
Okay thanks!
I will try it.
|
|
|
12/02/2009, 20:12
|
#11
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
|
Quote:
Originally Posted by InvincibleNoOB
Perhaps use WSAAsyncSelect() or WSAEventSelect.You wouldn't even need a while loop.
|
but a message loop dont you?
i dont like the message loop^^
|
|
|
12/02/2009, 20:17
|
#12
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
Edited:
mhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
|
|
|
12/02/2009, 22:00
|
#13
|
elite*gold: 20
Join Date: Mar 2007
Posts: 4,277
Received Thanks: 2,990
|
Quote:
Originally Posted by MrSm!th
but a message loop dont you?
i dont like the message loop^^
|
There is always a message loop,using that technique only requires a few conditional instructions to be written inside the message loop.
|
|
|
12/03/2009, 17:43
|
#14
|
elite*gold: 0
Join Date: Oct 2008
Posts: 680
Received Thanks: 337
|
I solved this Problem adding a "continue;" at
PHP Code:
if(FD_ISSET(aSocket, &fdSet)) { AddSocket(accept(aSocket, NULL, NULL)); continue; }
|
|
|
|
Similar Threads
|
[Advanced] Winsock in C#
05/23/2013 - CO2 Programming - 7 Replies
Requires ws2_32.dll, and "using System.Runtime.InteropServices"
// fuck creating my own socketerror enum LOL
using SocketError = System.Net.Sockets.SocketError;
// Interface to ws2_32.dll
public unsafe partial class Native
|
Need Help about (Winsock Packet Editor (WPE) Pro 0.9a)
09/26/2009 - CO2 Programming - 18 Replies
Hey All
I need help to know some thing about
( Winsock Packet Editor (WPE) Pro 0.9a in Network Monitors )
need to know..
How Can i open one Sv in different User account at One Pc
How is it work..?
Thankz any way
i w8 Replies
Plz don't avoid me
|
Vb6 : A Simple Winsock Connection !
04/12/2009 - .NET Languages - 0 Replies
I made a little tut for a simple winsock connection
In visual basic 2006 !
So here it comes
Start with opening vb6
http://img412.imageshack.us/img412/9527/54328939. jpg
Now open Standard EXE
http://img258.imageshack.us/img258/5764/73925545. jpg
|
winsock in vb.net?
12/29/2007 - Conquer Online 2 - 4 Replies
anyone know how to get winsock into vb.net?ive been googleing for an answer and cant findone.
i downloaded oswinsock and still not sure how to use it??
Ive been using vb6 for a while and am using vb 2005 for the first time and this is pissing me off.i read i should use systemcom.net or somthing?
just anyone know how i can use winsock in vb.net
or explain what i need to do,before i give up on this .net framework and stick with my trusty vb6
if anyone has a simple source proxy in...
|
vb6 winsock tutorial
10/24/2006 - .NET Languages - 2 Replies
Ich weiß, vb6 ist aaaaaaaalllt und 2005 ist sooooo besser (kann ich auch gleich zu C# gehen), aber fürs erste will ich das noch probieren. Okay, ich suche ein Winsock tutorial was allerdings direkt HEX packets schickt/verarbeitet und nicht im ASCII format, ich hab zwar ein paar Beispiel sources, allerdings versteh ich nicht so recht wie ich das machen soll. Also ne TCP verbindung.
|
All times are GMT +2. The time now is 02:49.
|
|