The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Special Forums > IP Networking
.
google unix.com



IP Networking Learn TCP/IP, Internet Protocol, Routing, Routers, Network protocols in this UNIX and Linux forum.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please help! accept function problems in Socket programming natxie High Level Programming 1 02-07-2009 05:01 PM
number of lines returned from a grep command cesarNZ Shell Programming and Scripting 4 10-09-2008 09:57 PM
Socket programming : Accept return 0. abc.working UNIX for Dummies Questions & Answers 7 10-17-2007 04:03 AM
[Problem]Reuse port in BSD socket Namely High Level Programming 1 11-28-2003 11:36 AM
socket on serial port kintoo High Level Programming 2 07-31-2001 03:44 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-21-2009
idelovski's Avatar
idelovski idelovski is offline
Registered User
  
 

Join Date: Feb 2008
Location: Zagreb, Croatia
Posts: 1
Port number of socket returned by accept()

Hi,

I typed a few tcp/ip client/server examples from a book and it works - sort of - but I noticed something strange. When I run my server I set it to use port 3001 and the client uses the same port to connect to server. They succeed, but the server prints something that doesn't really make much sense: it prints that the client uses port 30152. Next time I run it is 30153 and next time is 30154 and so on.

Here is the code of the server, accept() call and printf() call.

Code:
      struct sockaddr_in  clntAddr, testAddr;
      socklen_t           clntAddrLen = sizeof (struct sockaddr_in);

      int                 clntSock = accept (servSock, (struct sockaddr *)&clntAddr, &clntAddrLen);

      if (clntSock < 0)
         DieWithSystemMessage ("accept() failed");

      char  clntName[INET_ADDRSTRLEN];  // str to contain client address
      
      if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName, INET_ADDRSTRLEN))
         printf ("Handling client %s/%hu\n", clntName, ntohs(clntAddr.sin_port));
I am on Mac (10.5) and the book is TCP/IP Sockets in C. (Full source code link - TCPEchoClient4.c & TCPEchoServer4.c + DieWithMessage.c & Practical.h)
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0