473,790 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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) ||
(WSAAddressToSt ring(&name,name len,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(MAKE WORD(2,0), &WSAData);
bool optVal = true;

while(true)
{
sock = socket(AF_UNSPE C, 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_ EXCLUSIVEADDRUS E,(char
*)&optVal, sizeof(bool));
host (sock, buffer, &buff_len);
cout << "from" << buffer << "\r";
closesocket(soc k);
sock = INVALID_SOCKET;
}
cout << "Cleaning up" << endl;
sock = INVALID_SOCKET;
WSACleanup();
cin >new char;
}
Aug 9 '07 #1
0 1347

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

Similar topics

21
15729
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 1234, but each instance binds to a different ip address. that is to say: instance #1 binds to 192.168.1.5/port 1234 instance #2 binds to 192.168.1.6/port 1234 instance #3 binds to 192.168.1.7/port 1234
5
4362
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 connect to. Any info would be appreciated.
8
3353
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 what they've recommended. Anybody on this forum know what is specifically needed? xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hello,
2
2150
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, William GS
0
2460
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 transfer data using Winsock but i can not able to read COM Port continously manner so I can get data. I think it is due to COM Port can be opened at once. Can You give me such code so that i can listen COM Port. I Just want to listen data and...
3
4130
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 displays the reults real-time on screen. The UDP packet is sent every 200ms. I know the port. I have Windows Vicual C++ Express Edition running under XP.
1
2719
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 messages using CSHARP/ VB.NET. Can you please forward on me some example code on how to go about this?
5
8491
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#? Thanks naamala
3
7620
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 Expressions to strip out the HTML code itself, the problem is actually getting the web pages from the internet. I tried using the Microsoft Internet Transfer Control but my client was experiencing problems with some web pages not downloading, this...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.