-
winsock
I am making a simple chat program using winsock.
if I don't use WSAAsyncSelect(.....) and just use
d=connect(s,(struct sockaddr *)&a,sizeof(a));
d gives 0 "No error" but I can send to server only I can't get data from server.
but if I use
WSAAsyncSelect(.....)
d=connect(s,(struct sockaddr *)&a,sizeof(a));
d gives error but it works and I can send and receive.
So how to check wether connection has established or not?
-
Better way would be using WSAAsynkSelect() because it'll run in non-blocking mode. After you call this function and tell it the message you want to post, you will recieve a message FD_ACCEPT. This means you have been connected (if there's no error). Note that FD_ACCEPT is also sent when you're in listening mode and a clients requests a connecting so you'll have to know in which state you are (listening or connecting).
If you don't use the above function (although there're better ways to do it) then your connecting will be in blocking mode and you'll have to check for any recieved data in a loop. This will prevent you from doing other stuff unless you have a multithreading application.
-
1 Attachment(s)
check out my sample code for both with Async and w/o Async :)
-
Thanks
int i;
SOCKET s[];
i=1;
s[i]=soceket(....) gives some lib error
How to create an array of socket?
-
int i;
SOCKET s[20];
i=1;
s[i]=socket(....)
That was just an example but you can use a vector to dynamically resize the array if you want unlimited sockets.
-
how about:
typedef struct mysocket
{
SOCKET s;
mysocket *psocket;
}
NEWSOCKET;
then just line the sockets up in your own dynamic linked list.
i do not know that much about sockets but this should be possible if sockets work in the same ways as other variables like the int or char variables.