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
164
votes
12 answers

IPC performance: Named Pipe vs Socket

Everyone seems to say named pipes are faster than sockets IPC. How much faster are they? I would prefer to use sockets because they can do two-way communication and are very flexible but will choose speed over flexibility if it is by considerable…
user19745
  • 3,499
  • 8
  • 25
  • 21
161
votes
3 answers

What's the difference between streams and datagrams in network programming?

What's the difference between sockets (stream) and sockets (datagrams)? Why use one over the other?
RoR
  • 15,934
  • 22
  • 71
  • 92
160
votes
7 answers

socket.shutdown vs socket.close

I recently saw a bit of code that looked like this (with sock being a socket object of course): sock.shutdown(socket.SHUT_RDWR) sock.close() What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
160
votes
6 answers

socket connect() vs bind()

Both connect() and bind() system calls 'associate' the socket file descriptor to an address (typically an ip/port combination). Their prototypes are like:- int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); and…
Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25
158
votes
31 answers

Socket.io + Node.js Cross-Origin Request Blocked

I'm using node and socket.io to write a chat application. It works fine on Chrome but mozilla gives an error to enable the Cross-Origin Requests. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at…
Waleed Ahmad
  • 2,184
  • 4
  • 21
  • 23
158
votes
5 answers

TCP loopback connection vs Unix Domain Socket performance

Working on an Android and iOS based application which require communication with a server running in the same device. Currently using TCP loopback connection for communicating with App and Server (App written in user layer, server written in C++…
Rohit
  • 6,941
  • 17
  • 58
  • 102
157
votes
6 answers

How large should my recv buffer be when calling recv in the socket library

I have a few questions about the socket library in C. Here is a snippet of code I'll refer to in my questions. char recv_buffer[3000]; recv(socket, recv_buffer, 3000, 0); How do I decide how big to make recv_buffer? I'm using 3000, but it's…
adhanlon
  • 6,407
  • 13
  • 43
  • 41
155
votes
14 answers

What's causing my java.net.SocketException: Connection reset?

We are seeing frequent but intermittent java.net.SocketException: Connection reset errors in our logs. We are unsure as to where the Connection reset error is actually coming from, and how to go about debugging. The issue appears to be unrelated to…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
154
votes
14 answers

What can be the reasons of connection refused errors?

I'm trying to write a server program in C, using another client, I get this error when I try to connect through port 2080 for example. connection refused What can be the reasons of this error?
Zenet
  • 6,961
  • 13
  • 38
  • 45
151
votes
16 answers

Python [Errno 98] Address already in use

In my Python socket program, I sometimes need to interrupt it with Ctrl-C. When I do this, it does close the connection using socket.close(). However, when I try to reopen it I have to wait what seems like a minute before I can connect again. How…
skylerl
  • 4,030
  • 12
  • 41
  • 60
151
votes
6 answers

Connecting to TCP Socket from browser using javascript

I have a vb.net application that opens a socket and listens on it. I need to communicate via this socket to that application using a javascript running on a browser. That is i need to send some data on this socket so that the app which is listening…
swordfish
  • 4,899
  • 5
  • 33
  • 61
143
votes
4 answers

How does the socket API accept() function work?

The socket API is the de-facto standard for TCP/IP and UDP/IP communications (that is, networking code as we know it). However, one of its core functions, accept() is a bit magical. To borrow a semi-formal definition: accept() is used on the server…
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
143
votes
7 answers

Closing WebSocket correctly (HTML5, Javascript)

I am playing around with HTML5 WebSockets. I was wondering, how do I close the connection gracefully? Like, what happens if user refreshes the page, or just closes the browser? There is a weird behavior when a user just refresh the page without…
Andy Hin
  • 30,345
  • 42
  • 99
  • 142
143
votes
10 answers

Sockets: Discover port availability using Java

How do I programmatically determine the availability of a port in a given machine using Java? i.e given a port number, determine whether it is already being used or not?.
user54075
  • 1,431
  • 2
  • 10
  • 3
143
votes
3 answers

Are parallel calls to send/recv on the same socket valid?

Can we call send from one thread and recv from another on the same socket? Can we call multiple sends parallely from different threads on the same socket? I know that a good design should avoid this, but I am not clear how these system APIs will…
Jay
  • 24,173
  • 25
  • 93
  • 141