Re: Socket relared problem (getting free port number dynamically)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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,

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
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Problem with socket
    ... Be aware that those port numbers are part of the IANA-assigned range. ... socket operations on sockets for which there are no handles... ... The result of using comma lists is ... you have used the completely meaningless word "crash" to describe your ...
    (microsoft.public.vc.mfc)
  • RE: call is blocked in recvfrom() and no further proceedings in Win CE
    ... In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... > My program has to send request to service through port 5070(in this port only ...
    (microsoft.public.windowsce.embedded)
  • Re: ISA Event
    ... applying ISA SP1 resolved the issue. ... So it's worth asking - is this SBS ... > Web Proxy service failed to bind its socket to 192.168.4.9 port 443. ...
    (microsoft.public.backoffice.smallbiz2000)
  • Re: Freebsd IP Forwarding performance (question, and some info) [7-stable, current, em, smp]
    ... INADDR_ANY and a specific port. ... sendtoon a specific address and port on a socket that has been bound to ... bogus exit code) and adds a sleep after send failure. ... unless HZ is too small or the queue is too large. ...
    (freebsd-net)
  • Re: RMI binding to SAME port but DIFFERENT IP address on SAME host
    ... A socket is identified by its IP address, its port number, and its ... via the Sockets API. ... > port number for each listening interface, ...
    (comp.lang.java.programmer)