22184

What does it mean that accept() creates a new socket?

Question:

My question is based on the following understanding

<ul><li>Socket is defined by ip:port. Both server and client will have their own socket</li> <li>Socket connection is defined by five touples server_ip, server_port, client_ip, client_port, protocol</li> <li>A socket descriptor is an integer value that identifies a socket</li> </ul>

I tried to implement my own server where socket() and accept() returns different socket descriptor value (Is it always the case?). My question is why is it said that accept() creates a new socket if no new port is opened in server and ip:port is same for both the socket descriptors returned by socket() and accept(). If new socket is created by accept() how is it different than the socket created by socket()?

Answer1:

<ol><li><blockquote>

I tried to implement my own server where socket() and accept() returns different socket descriptor value (Is it always the case?).

</blockquote></li> </ol>

Yes.

<ol start="2"><li><blockquote>

My question is why is it said that accept() creates a new socket if no new port is opened in server and ip:port is same for both the socket descriptors returned by socket() and accept(). If new socket is created by accept() how is it different than the socket created by socket()?

</blockquote></li> </ol>

Because the initial socket is used to wait for communication while the second is used to communicate. A call to socket (+bind+listen) prepare a communication end-point, aka <strong>socket</strong> to receive incoming calls. A call to accept on a well prepared communication point, waits for an incoming call, and when this happens that creates a communication channel (2 end-points + protocol) represented by the <strong>connected socket</strong> returned by the call.

C API may confuse you because both are called socket, but are really not for the same use. In some other languages/API differentiation is made. For example in Java you have ServerSocket that is used to wait for incoming calls, and Socket that are used to communicate.

Recommend

  • Contact form doesn't send if there is a space in name
  • Rails: NameError: uninitialized constant OrderItem
  • cURL request to Unix socket from php
  • Ranges surrounding values in data frame R dplyr
  • How to renumber result of intersection/group_indices in R?
  • Removing multiple recurring text from pandas rows`
  • Using SQL with IBM_DB connector in Python
  • How to match and point to a particular data in array in Blogger
  • iPhone app startup
  • Is it possible to find the address of a non-primitive variable in Java?
  • Open protected word document with OpenXML
  • Match “/app” and not “/appinst” with android:pathPattern (working on an intent-filter)
  • Keep playing a sound over and over again in Matlab?
  • How to open files from a specific route in ASP-NET c#?
  • cannot convert from 'const char [3]' to 'char *' x100000 (Qt Creator C++ Windows
  • Force user change password after first login asp.net mvc with Forms authentication
  • How do i check if TcpClient is connected?
  • How to save in openpyxl without losing formulae?
  • Open view controller when remote notification pressed
  • Erlang raise number of opened sockets, MacOS and CentOs
  • ANTLR4: Unrecognized constant value in a lexer command
  • How to read data from socket connection - android
  • jQuery, Calling multiple animations in a row on click
  • What does the lower left pane in OllyDbg displays?
  • Cannot send user message with Spring Websocket
  • Weighted round robin dns between 2 Cloudfront distributions
  • Python Paramiko send CTRL+C to an ssh shell
  • Entity Framework unable to delete database, database in use
  • How to use animated gif in Firemonkey?
  • Error in making a socket connection
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • Z3: Convert between FP and BitVector?
  • Django simple Captcha “No module named fields” error
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • C# - Serializing and deserializing static member
  • Counter field in MS Access, how to generate?
  • Bug in WPF DataGrid
  • Incrementing object id automatically JS constructor (static method and variable)
  • Convert array of 8 bytes to signed long in C++
  • Free memory of cv::Mat loaded using FileStorage API