Questions tagged [sockets]

An endpoint of a bi-directional inter-process communication flow. This often refers to a process flow over a network connection, but by no means is limited to such. Not to be confused with WebSocket (a protocol) or other abstractions (e.g. socket.io).

In computer networking, a socket* is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet.

An internet socket address is the combination of an IP address (the location of the computer) and a port (which is mapped to the application program process) into a single identity, much like one end of a telephone connection is the combination of a phone number and a particular extension. It is primarily used in the Transport Layer.

An example IPv4 socket appears as: 10.1.1.1:80

An example IPv6 socket appears as: [fe80::1]:80 (Note the brackets)

The term is believed to have originated with the Berkeley Sockets API for Unix ca. 1983.

Related tags are , , , and .

* Not to be confused with (a protocol) or other abstractions (e.g. ).

Note to programmers, especially beginners, using the socket module: A great many questions about non-working socket programs seem to be based on a common misunderstanding that is discussed and corrected in this answer as well as this one. Please take the time to read and understand that answer if you are having problems.

66255 questions
142
votes
7 answers

iPhone Data Usage Tracking/Monitoring

I've searched over this topic but found very few details which were helpful. With these details I've tried to cook some code as follows. Note: Please compare the details shared in this post with other posts before marking this as DUPLICATE, and not…
Sahil Khanna
  • 4,262
  • 8
  • 47
  • 72
140
votes
6 answers

difference between socket programming and Http programming

What is the difference between socket programming and Http programming? can anyone help please?
Innovative Thinker
  • 1,735
  • 2
  • 14
  • 24
139
votes
5 answers

Turn a simple socket into an SSL socket

I wrote simple C programs, which are using sockets ('client' and 'server'). (UNIX/Linux usage) The server side simply creates a socket: sockfd = socket(AF_INET, SOCK_STREAM, 0); And then binds it to sockaddr: bind(sockfd, (struct sockaddr *)…
David Mape
  • 1,407
  • 2
  • 10
  • 3
136
votes
2 answers

Can TCP and UDP sockets use the same port?

First of all, is there any problem with using both UDP and TCP on the same server? Secondly, can I use the same port number?
user800799
  • 2,883
  • 7
  • 31
  • 36
132
votes
5 answers

Linux: is there a read or recv from socket with timeout?

How can I try to read data from socket with timeout? I know, select, pselect, poll, has a timeout field, but using of them disables "tcp fast-path" in tcp reno stack. The only idea I have is to use recv(fd, ..., MSG_DONTWAIT) in a loop
osgx
  • 90,338
  • 53
  • 357
  • 513
122
votes
8 answers

Interprocess communication in Python

What is a good way to communicate between two separate Python runtimes? Things tried: reading/writing on named pipes e.g. os.mkfifo (feels hacky) dbus services (worked on desktop, but too heavyweight for headless) sockets (seems too low-level;…
wim
  • 338,267
  • 99
  • 616
  • 750
118
votes
8 answers

When is TCP option SO_LINGER (0) required?

I think I understand the formal meaning of the option. In some legacy code I'm handling now, the option is used. The customer complains about RST as response to FIN from its side on connection close from its side. I am not sure I can remove it…
dimba
  • 26,717
  • 34
  • 141
  • 196
116
votes
9 answers

Java socket API: How to tell if a connection has been closed?

I am running into some issues with the Java socket API. I am trying to display the number of players currently connected to my game. It is easy to determine when a player has connected. However, it seems unnecessarily difficult to determine when a…
Dan Brouwer
  • 1,161
  • 2
  • 9
  • 3
115
votes
8 answers

Does a TCP socket connection have a "keep alive"?

I have heard of HTTP keep-alive but for now I want to open a socket connection with a remote server. Now will this socket connection remain open forever or is there a timeout limit associated with it similar to HTTP keep-alive?
Kevin Boyd
  • 12,121
  • 28
  • 86
  • 128
114
votes
7 answers

Understanding INADDR_ANY for socket programming

I am trying to program some sockets and so, on the server side, I use htonl(INADDR_ANY). To the extent I understood, it seems to me that this function generates a random IP (am I correct ?). In fact, I want to bind my socket with my localhost. But…
epsilones
  • 11,279
  • 21
  • 61
  • 85
111
votes
7 answers

How do I remove a CLOSE_WAIT socket connection

I have written a small program that interacts with a server on a specific port. The program works fine, but: Once the program terminated unexpectedly, and ever since that socket connection is shown in CLOSE_WAIT state. If I try to run a program it…
Dilletante
  • 1,801
  • 4
  • 19
  • 18
110
votes
16 answers

Do I need to heartbeat to keep a TCP connection open?

I have two components that that communicate via TCP/IP. Component A acts as a server/listener and Component B is the client. The two should communicate as quickly as possible. There can only ever be one connection at any time (though that is…
Rob Gray
  • 3,186
  • 4
  • 33
  • 34
110
votes
12 answers

How to configure socket connect timeout

When the Client tries to connect to a disconnected IP address, there is a long timeout over 15 seconds... How can we reduce this timeout? What is the method to configure it? The code I'm using to set up a socket connection is as following: try { …
ninikin
  • 1,657
  • 3
  • 12
  • 11
109
votes
3 answers

What is the meaning of SO_REUSEADDR (setsockopt option) - Linux?

From the man page: SO_REUSEADDR Specifies that the rules used in validating addresses supplied to bind() should allow reuse of local addresses, if this is supported by the protocol. This option takes an int value. This is a Boolean…
Ray Templeton
  • 1,091
  • 2
  • 8
  • 3
105
votes
15 answers

What do you use when you need reliable UDP?

If you have a situation where a TCP connection is potentially too slow and a UDP 'connection' is potentially too unreliable what do you use? There are various standard reliable UDP protocols out there, what experiences do you have with them? Please…
Len Holgate
  • 21,282
  • 4
  • 45
  • 92