Re: Socket relared problem (getting free port number dynamically)
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 06 Dec 2006 13:06:20 -0500
You appear to be doing raw socket programming, which isn't really a good idea most of the
time, and you also appear to be using synchronous sockets, which is always a mistake.
Use CAsyncSocket. Note that you probably will no longer need a separate thread to handle
the socket, because the socket callbacks occur asynchronously in the main thread.
joe
On 6 Dec 2006 00:51:06 -0800, "harshalshete@xxxxxxxxx" <harshalshete@xxxxxxxxx> wrote:
Hi all,Joseph M. Newcomer [MVP]
i have one problem related to thread. i am having my main thread
running which will
on some event create a thread for validation purpose and then will
start working.
and now this validator thread will create a socket and it will bind to
port 20111.
and then will wait for client to connect.
but before waiting for client to connect it will send a message to the
main thread that
validation is done.
but in this way i need to stick with port 20111.which i really don't
want.
Is it possible for me to get a free port from operating system or from
winsock
library and then i should bind to this port??
and one more thing i want is i want to get the free port no that
operating system
has given in an variable because i will need that port number further
in the main programs lifetime.
the code is as follows
UINT Thread_Func(LPVOID pParam)
{
wsaret = WSAStartup(0x101,&wsadata);
if(wsaret != 0)
return -1;
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = INADDR_ANY;
sock.sin_port = htons((unsigned short)20111);// iwant to obtain this
port no on fly and
// to get it in some variable so that i can use it
further in my main thread
server = socket(AF_INET,SOCK_STREAM,0);
if(server == INVALID_SOCKET)
return -1;
if(bind(server,(SOCKADDR *)&sock,sizeof(sock))!=0)
return -1;
if(listen(server,1)!=0)
return -1;
cli_len = sizeof(cli);
client = accept(server,(SOCKADDR *)&cli,&cli_len);
if(client == 0)
return -1;
return 1;
}
i have not done socket programming before this is my first time i am
doing it
so if there are any mistakes please let me know
Thanks and regards
Harshal shete
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Socket relared problem (getting free port number dynamically)
- From: harshalshete@xxxxxxxxx
- Socket relared problem (getting free port number dynamically)
- Prev by Date: Re: Changing dialog background color with OnCtlColor()
- Next by Date: Re: ASyncSock
- Previous by thread: Socket relared problem (getting free port number dynamically)
- Next by thread: Socket relared problem (getting free port number dynamically)
- Index(es):
Relevant Pages
|