Re: freeing port - winsock



Well

I finnaly find out what happened.
Just check those two links, they explain everything (and google wasn't my friend this time -_-) :
http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/regentry/58811.mspx
http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/regentry/58791.mspx


Julien Ghaye a écrit :
Hi everyone,

Actually, I'm coding some kind of a http client.

Here is my problem. When I create and destroy many sockets (one after another) without specifying the local port to bind, It seems that this local port is not free after using the function closesocket().

So, How can I free it ?

Here is a small programm that I code quickly. It's goal is to create a socket, connect to a remote server on port 80, close the socket and to loop on these actions.
It displays the local port in use.
You'll see that after having used the default range 1024-5000, you get a 10048 error (adresse in use)

Any ideas ?

Thanks all,
Julien


///////////////////// CODE ////////////////////


#include <iostream>
#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib")

using namespace std;

int host(SOCKET s, char* buff, int* length)
{
sockaddr name;
int namelen = sizeof(sockaddr);
if ((getsockname(s,&name,&namelen) == SOCKET_ERROR) ||
(WSAAddressToString(&name,namelen,NULL,buff,(LPDWORD)length) == SOCKET_ERROR))
{
cout << "An error Occurs .... error code : " << WSAGetLastError() << endl;
return -1;
}
return 0;
}


void main()
{
WSADATA WSAData;
SOCKET sock = INVALID_SOCKET;
SOCKADDR_IN sin;
int buff_len = 255;
char *buffer = new char[buff_len];

cout << "Stratup" << endl;
WSAStartup(MAKEWORD(2,0), &WSAData);
bool optVal = true;

while(true)
{
sock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_family = AF_INET;
sin.sin_port = htons(80);
cout << "Connecting " ;
if(connect(sock, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
cout << " ... Error : " << WSAGetLastError() << endl;
break;
}
//setsockopt(sock,SOL_SOCKET,SO_EXCLUSIVEADDRUSE,(char *)&optVal, sizeof(bool));
host (sock, buffer, &buff_len);
cout << "from" << buffer << "\r";
closesocket(sock);
sock = INVALID_SOCKET;
}
cout << "Cleaning up" << endl;
sock = INVALID_SOCKET;
WSACleanup();
cin >> new char;
}


--
Julien Ghaye
University of Liège - Montefiore Institute
.



Relevant Pages

  • [PATCH] Delete superfluous source file "net/wanrouter/af_wanpipe.c".
    ... 2000 Nenad Corbic o Fixed the corrupt sock lcn problem. ... -/* SECURE SOCKET IMPLEMENTATION ... * routine in the wanpipe driver. ... -static void * dbg_kmalloc(unsigned int size, int prio, int line) { ...
    (Linux-Kernel)
  • [PATCH 1/6] x25: Allow 32 bit socket ioctl in 64 bit kernel
    ... The following patch provides 32 bit userland ioctl support for modular ... socket ioctls in a 64 bit kernel. ... SOCKCALL_WRAP(name, ioctl, (struct socket *sock, unsigned int cmd, \ ... SOCKCALL_WRAP(name, listen, (struct socket *sock, int len), (sock, ...
    (Linux-Kernel)
  • freeing port - winsock
    ... When I create and destroy many sockets without specifying the local port to bind, It seems that this local port is not free after using the function closesocket. ... It's goal is to create a socket, connect to a remote server on port 80, close the socket and to loop on these actions. ... int host ... SOCKET sock = INVALID_SOCKET; ...
    (comp.programming)
  • [2.6 patch] net/packet/af_packet.c: make some code static
    ... /* Private packet socket structures. ... +static void packet_sock_destruct(struct sock *sk) ... +static struct proto_ops packet_ops_spkt; ... -int packet_getsockopt(struct socket *sock, int level, int optname, ...
    (Linux-Kernel)
  • Re: maximum timeout on socket connection
    ... > operations on the socket. ... * returns zero on success, ETIME on timeout, errno on connect failure. ... int connectex ... socket_set_nonblocking(int sock, bool mode) ...
    (comp.unix.programmer)