Click to See Complete Forum and Search --> : new to sockets in VC++, how can I use listen?


lab1
June 14th, 2004, 10:34 AM
Hello all,

I want to do something very simple with sockets. I have a TCP/IP server class in which I want to listen() for remote connections.. everytime something connects to the port that im listening on, I want it to basically call a handler?? and then I want to add that connection to a list.


whats the simple basic code used to listen for tcp/ip connections on a port? can I call a handler when something connects?

example code would be greatly appreciated!

Thanks!

Andreas Masur
June 14th, 2004, 11:50 AM
[Moved thread]

Mathew Joy
June 15th, 2004, 04:38 AM
I didn't understand your question quite clearly. If you are asking for ex codes of using listen() it would be something similar...dwErr = listen(srvSock,SOMAXCONN);

if(dwErr == SOCKET_ERROR) {

//handle error through WSAGetLastError()
return 0;
}
For accepting connections...cliSock = accept(srvSock, 0, 0);

if( cliSock == SOCKET_ERROR) {

//handle error through WSAGetLastError()
return 0;

}

// use send() and recv() to send and receive data
Note that srvSock is a bound socket and cliSock is not. cliSock is a handle and you can use it for sending and recving data to the connected client.

Hope this is what you want

lab1
June 15th, 2004, 09:11 AM
do you have any real simple example programs of a client/server? the server listening for connections and accepting them when the client connects?


thanks!

dimm_coder
June 17th, 2004, 11:04 AM
Originally posted by lab1
do you have any real simple example programs of a client/server? the server listening for connections and accepting them when the client connects?

thanks!

U can easily find examples and documentation using google.
Firstly, try to do smth by yourself. If U have encountered some problem then post the appropriate question here.