473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

freeing port - winsock

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,(LPDWO RD)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,(ch ar
*)&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;
}
Aug 9 '07 #1
0 1329

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
5
by: Keith | last post by:
I am trying to write a simple FTP client, but I am having trouble with the PORT Command. Does anyone know how to Get an IP Address and port number to establish a data port for the server to...
8
by: MLH | last post by:
I wrote a letter to NIST in Boulder, Colorado asking how to glean time/date from their servers from inside an Access 97 app and VBA. Their reply is shown below. Trouble is, I do not know how to do...
2
by: William GS | last post by:
Hello, my app sends and receives messages over a local area network using WinSock, so I need a Port, How can I get an unused port? It must work with VC6 and Win98/ME too. Thanks in advance,...
0
by: ckp21082 | last post by:
I want to develop an application that constantly read Com port Read data comming from COM Port and transfer that data to another computer using Winsock so Hyper Terminal can read it. I am able to...
3
by: Pashlipops | last post by:
Hi there I am looking for some guidance. I am new to C++ and whilst I am reading up about the language, my actual goal is to create a program that reads a UDP packet sent by a data logger and...
1
by: trytobreak | last post by:
Hey guys, I have a netscreen firewall which is configured to forward syslog messages to port 514 of my machine (specific ip on the network) and I am really puzzled as on how to capture these log...
5
by: naamala | last post by:
Hi, How can i get unused/available port in the local system using C# ? Or Is there any way to find out whether the particular port number is being used by any other application using C#? ...
3
AaronL
by: AaronL | last post by:
Hello, I am currently working on a project that has me in sort of a bind. What I want to do is retrieve web pages from the internet, and strip them down to just text. I'll get using Regular...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.