Automatic port selection using C in Linux?


 
Thread Tools Search this Thread
Top Forums Programming Automatic port selection using C in Linux?
# 1  
Old 01-26-2011
Question Automatic port selection using C in Linux?

Hi

What would be the correct way to get the port number for a free port, using C in Linux?

I've tried to create() and bind() a socket without specifying the port, and that seems to work in some cases (using gcc, and some Linux dist).
When compiling with the Intel Icc64 compiler I suddenly had to do a "dummy send" with the created socket to get a port != 0.
Then, when using that technique on mashines running Slackware 13, it doesn't work. I just get the port to be 0.

So, is there a "correct" way to do this?
I'm trying to find out free port numbers that clients can use to receive messages from a server, and I would like to retreive them before actually sending anything between server/client.

Help, please...

/GC
# 2  
Old 01-26-2011
What's create()? The irony of including the "e" ... Smilie

Your question is not clear to me - do you wish to discern which port is available on the local box (server) so that you can bind() and listen() on that particular port, *or* do you wish to know on the client which port can be used for bi-directional communication to the server (this port will be chosen automatically if you specify 0 for sin_port, and it will be used for *outgoing* packets unless you are going to listen() on that port).

Probably best to post what you have written so far.
# 3  
Old 01-26-2011
I think that it stopped working on a particular compiler is indicative of problems in your code. An important but uninitialized variable perhaps, left to have an undefined value and ending up being a number of different things depending on compiler, system, and circumstances.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

How to find port number wwn of particular port on dual port HBA,?

please find the below o/p for your reference bash-3.00# fcinfo hba-port HBA Port WWN: 21000024ff295a34 OS Device Name: /dev/cfg/c2 Manufacturer: QLogic Corp. Model: 375-3356-02 Firmware Version: 05.03.02 FCode/BIOS Version: BIOS: 2.02; fcode: 2.01;... (3 Replies)
Discussion started by: sb200
3 Replies

2. UNIX for Advanced & Expert Users

how to open port in linux

hi experts, I'm using Linux Centos kernel 2.6 Here is the print out of some my port : tcp 0 0 127.0.0.1:10080 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:10081 0.0.0.0:* LISTEN tcp 0 0... (4 Replies)
Discussion started by: justbow
4 Replies

3. UNIX for Dummies Questions & Answers

Linux - How to Open a Port

Hi, I would like to open a specific port for use with a bespoke application. Before everyone points me to other threads - I read a few but couldn't find any specific to my problem. Iptables / firewall is disabled SELinux is also disabled I would just like to assign this port to this... (0 Replies)
Discussion started by: mcclunyboy
0 Replies

4. Red Hat

RedHat Linux Automatic Updates

Hi All, we have about 50 RedHat Linux servers (v4.x, 5.x), now we planing to configure those servers to get updates(service packs, security patches...etc) automatically. whats is the best practises to do this. we also have SLES servers and we use Novell's SMT tool to get updates, we... (15 Replies)
Discussion started by: s_linux
15 Replies

5. IP Networking

Setting up port mirroring on LINUX

Hello everyone. I am supposed to configure port mirroring on Linux (the LAN traffic needs to be routed to the other interface and passed a copy to network monitoring software): eth0 - connected to local traffic eth1 - connected to the Internet The machine is running CentOS v4.4 (Server... (1 Reply)
Discussion started by: reminiscent
1 Replies

6. UNIX for Dummies Questions & Answers

automatic port designation

hi there! can anybody help me on what command will i use for UNIX to generate port numbers? i mean, other than bind? say i want to create a new session, and i dont want to reuse the port that i have used... please help. i would appreciate it so much! with much love, init6_ (2 Replies)
Discussion started by: init6_
2 Replies

7. Linux

Linux Port Unreachable

All, I have to servers (NT and REL4). We can access the application on NT from Linux but we cannot access the applications on Linux when accessed from NT. The applicaiton is running on Linux with some IP:Port. When we try to access that application from NT, it says that the port is... (1 Reply)
Discussion started by: The One
1 Replies

8. Linux

Port Linux Driver to Unix

I looking for someone who can help me port a Linux Driver to a OpenBSD( OS X ) Unix Driver. The driver is for a Logic Controls LD9000 USB Customer Display. The currently don't have a driver for the mac operating system, but they do however have one for the Linux OS. So, I am just trying to... (8 Replies)
Discussion started by: ghost1
8 Replies

9. Linux

why is fortran used to port linux to others?

what is the speciality of fortran to be used while porting linux to other language? (1 Reply)
Discussion started by: sravus
1 Replies
Login or Register to Ask a Question