Hi there ! I try to write a very simple chat program. And I don't have problems with the 'connect' function. But 'listen'... When I call 'listen' the return value is always SOCKET_ERROR. Why ? How to use listen ?? Posted on 2001-04-19 02:34:00 by fla
did you try calling WSAGetLastError to see what error listen was returning? try posting some source code maybe i could help. smurf
Ey, smurf ! Thanx for answering me so soon ! Yes. I call WSAGetLastError and it returns WSAEINVAL. But I know I haven't bound the socket :|... Reading the Icz tut, 'bind' function is not mentioned at all... So tell me, do I have to use it and how ? I read about it in the help, but I still can't understand what does it really do... Thank u once again !
fla, The Bind function is dead easy, you want tp listen on a socket to see if anyone is connecting to it, that is what listen does, but listen doesn't know what the address is, all it has is a socket so far. Like this :- Create a socket Listen to socket Now, just doing those two things won't work because listen doesn't know about the address you want, so you 'bind' the socket to an address :- create socket bind socket to address listen to socket. Now, bind takes 3 parameters:- the socket (you have that already) the SOCKADDR structure (I'll explain this one) the size of the SOCKADDR (easy using sizeof macro) SOCKADDR is the structure which tells the bind function where it's attaching to. The only bits you need to fill in are the sa_family member, and you use AF_INET for that, the port number you want to listen on (you can only listen to one port number) and the address, use INADDR_ANY for that, it's a constant meaning "I don't mind anyone connecting to me". You can put a specific address in there but only that address will be able to connect. umbongo.
Wow ! Thank uuuuuu, umbongo !!! I haven't seen at all that listen 'doesn't know' the address it should connect to :)) Stupid :} I'll try now to 'listen' once again ;)... Thank u !
You might already know this, but it took me several hours to debug *L*. After you are connected (on the server side), when you send and recv.....use the socket descripter returned from the accept API.....not the original one you used to listen on. *unknown* P.S. Why does Windoze do that? Wouldn't it be simpler to just keep one socket descripter?
??? A... *unknown*, thank u for the advice, but... I'm newbie in the network programming. So I still gotta problem with both 'bind' and 'listen function'... :| I have had a look at a source code and I saw that the guy used the 'sockaddr_in' structure for binding the socket and not the 'sockaddr' structure... What's wrong here ?? Help me, please ! Thanx
??? A... *unknown*, thank u for the advice, but... I'm newbie in the network programming. So I still gotta problem with both 'bind' and 'listen function'... :| I have had a look at a source code and I saw that the guy used the 'sockaddr_in' structure for binding the socket and not the 'sockaddr' structure... What's wrong here ?? Help me, please ! ThanxPosted on 2001-04-22 07:19:00 by fla
Hi *unknown* --------- it uses a diffrent socket to mantain the connection while the listen socket is in charge of listening for incomming connections. if you close the listen socket you no longer accept connections. if you close the socket returned from accept you are closing the connection you have with that one client. You would have to have more then one socket for this to work. Think what would happen if you were running a big server with lots of incomming connections. Most of this socket functions are the same for all os. I know that the WSA* functions are specific for windows. fla --- 'sockaddr_in' structure is the same size as 'sockaddr'. The sockaddr structure varies depending on the the protocol selected. you should use the 'sockaddr_in' structure it is used with TCP/IP.
......I kinda figured it was somethin like that ;-) Thanx, *unknown*
Thank u, hydrobird. Now I know a little more about the WSA programming :) I'll try it and if there r some problems - I'm here agaaaaaain :]... Thank u.
Hydrobird, questions of mine again :| 1. I call the 'connect' function only once and I get connected. Is it the same with 'listen' or I gotta 'listen' in a cycle (for example on WM_*) ? 2. Why 'listen' ALWAYS returns SOCKET_ERROR ?? Looking at the code of other coders I c that they don't care about what 'listen' returns. Why ? Is it like 'connect' in a non-blocking mode ?? Thank u. Thanx everyone replying me.
You only need to do listen once. I don't know why 'listen' always gives you SOCKET_ERROR. Are you sure your 'bind' worked with out any error? Without giving the error you got with WSAGetLastError its hard for someone to know the reason. Well the other coders probley wanted to make a simple example and didn't want to make the program complicated by adding error checking.
fla, You only need to listen once, this basically sets the mode of the socket. You then need to call accept to get the handle of the caller - the problem you will find with accept is that it will not return until you have a connection, so if you call accept and 10am and no connection is made to your computer until 11am, that will be 1 hour before your call to accept returns. You can fix this by using select. select allows you to look to see how many connections requests are pending, if it's zero then no-one is trying to connect, so no need to call accept. listen shouldn't return SOCKET_ERROR, it should return 0 if it works. Time to check that code. umbongo
Hi, I can't read all the messages posted here but if you want a chat program example, you can download the one I made a few weeks ago here. It is a 'in live' chat program : like the WinChat with Windows95 CD, when you enter a letter, the other PC receive letter by letter what you write. It works just for TCP/IP protocole. Hope that helps!.;)
Vom-bonjour - thankx. But have downloaded ur program 2 days ago :) ...As far as I can c, it is only for a LAN or ?? ... Thank u all guys, u give me really the info I need. But now I got another problem - W95/Hybris.worm :) Dunno where I caught it from, but I can't remove it with the latest version of f-prot :)... So I gotta clear out this problem first and then I'll continue improving my WSA knowledges :) Thankx P.S. W95/Hybris.worm has infected 2 files of mine - a one - without extension and another - WSOCK32.DLL... And as I read a piece of the Norton Antivir info about it, this virus infects .COM files... Does anyone knows anything about this ?? P.S.2 The W95/Hybris.worm only slows down my comp (just a little) and, which is notable for it, it slows down the showing of the menus in the application windows (and the StartMenu as well). Another thing: As u start restart the comp and enter the windows for first time (this day), it takes a couple of seconds of the Explorer to find the directories of ur C:\ That's all I can c W95/Hybris.worm does.
Hi everybody. Another question troubles me these days :| I had a look again at the Vom-bonjour's prog. I c, that he's using the WM_CREATE message in order to 'bind' and 'listen' to the socket. Vom-bonjour, tell me, please, do I have to call 'bind' and 'listen' functions at both sides, or it is enough to 'bind' and 'listen' at one side and to 'connect' at the other ? Sorry, if the question is stupid, but... All the functions I use - 'bind', 'listen' and 'connect' (at the other side) work well but I do not receive the FD_* - messages. Why ? :|... Thanx everybody helping me on with my incredible efforts of writing win socks...