LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2010, 02:42 PM   #1
vlyamtse
Member
 
Registered: Apr 2007
Posts: 54

Rep: Reputation: 15
tcp client socket : how to get own port number ??


Hi,
I am working with simple tcp client to establish connection to sockaddr_in servAddress and send data segment:
...
connect(sockfd,&servAddr,sizeof(servAddr)) < 0)
send(sockfd, pkt, pktLen, 0);
close(sockfd);
...
Elementary... Here's the tricky part: next i have to handle user level ack the server is sending to "client"... To do that i have to open server socket on the same port number the system assigned to my client socket before. How can i get it ( in user level code)?
Thanks,
-V
 
Old 10-01-2010, 05:26 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Code:
connect(sockfd,&servAddr,sizeof(servAddr)) < 0)
send(sockfd, pkt, pktLen, 0);
recv(...);
close(sockfd);
..from memory
 
Old 10-01-2010, 07:26 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I sounds like you are asking how to do bi-directional communcations on a socket. The answer is too simple: each end of the connection is a read/write file descriptor. Simply leave the socket open until all of the communication has transpired in each direction.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind a PF_PACKET socket to a specific port number gr1980 Programming 1 10-27-2010 10:57 PM
[SOLVED] How could server detect closed client socket using TCP and c++? Lobinho Programming 6 08-10-2010 02:28 PM
TCP and UDP socket on Same port ? bkankru Linux - Networking 1 11-20-2009 09:07 AM
port reuse problem :TCP socket sweetytweety Linux - Newbie 1 05-22-2008 04:05 AM
How to limit the number of TCP connections for a certain port? mussons Linux - Security 1 01-28-2004 06:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration