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
63
votes
5 answers

How to debug a socket hang up error in NodeJS?

I am getting the following error: events.js:48 throw arguments[1]; // Unhandled 'error' event ^ Error: socket hang up at createHangUpError (http.js:1091:15) at Socket.onend (http.js:1154:27) at TCP.onread…
user971956
  • 3,088
  • 7
  • 30
  • 47
62
votes
6 answers

Communicating with a socket.io server via c#

Is there a c# client that follows the socket.io protocol? I have a socket.io server that is communicating with a socket.io javascript client via a website, but i also need to connect a c# piece to it that can send and receive messages. Is there a…
Dested
  • 6,294
  • 12
  • 51
  • 73
62
votes
4 answers

Why is bind() used in TCP? Why is it used only on server side and not in client side?

I wanted to know the exact function of bind() in TCP. What does it mean by 'binding' a local address to the socket? If it's assigning a port number to the socket, then why don't we use it in the client? I know that port is assigned by OS…
Crocode
  • 3,056
  • 6
  • 26
  • 31
61
votes
15 answers

Cannot assign requested address using ServerSocket.socketBind

When I'm trying to set up a socket server, I've got an error message: Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method) at…
Adrian Adamczyk
  • 3,000
  • 5
  • 25
  • 41
61
votes
12 answers

gaierror: [Errno 8] nodename nor servname provided, or not known (with macOS Sierra)

socket.gethostbyname(socket.gethostname()) worked well on OS X El Capitan. However, it's not working now after the Mac updated to macOS Sierra. Thanks! import socket socket.gethostbyname(socket.gethostname()) Traceback (most recent call last): …
FBL
  • 611
  • 1
  • 5
  • 4
61
votes
7 answers

how can an application use port 80/HTTP without conflicting with browsers?

If I understand right, applications sometimes use HTTP to send messages, since using other ports is liable to cause firewall problems. But how does that work without conflicting with other applications such as web-browsers? In fact how do multiple…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
61
votes
6 answers

How to avoid a NoRouteToHostException?

Disclosure: the code I'm working on is for university coursework. Background: The task I'm trying to complete is to report on the effect of different threading techniques. To do this I have written several classes which respond to a request from a…
Grundlefleck
  • 124,925
  • 25
  • 94
  • 111
60
votes
6 answers

Asynchronous IO in Java?

What options for async io (socket-based) are there in java other then java.nio? Also does java.nio use threads in the backround (as I think .NET's async-socket-library does, maybe it's been changed) or is it "true" async io using a proper select…
thr
  • 19,160
  • 23
  • 93
  • 130
60
votes
1 answer

What does it mean to bind() a socket to any address other than localhost?

I don't understand what it means to bind a socket to any address other than 127.0.0.1 (or ::1, etc.). Am I not -- by definition -- binding the socket to a port on my own machine.. which is localhost? What sense does it make to bind or listen to…
user541686
  • 205,094
  • 128
  • 528
  • 886
60
votes
3 answers

What is "backlog" in TCP connections?

Below, you see a python program that acts as a server listening for connection requests to port 9999: # server.py import socket import time # create a socket object serversocket = socket.socket( …
user3739941
60
votes
7 answers

Passing a structure through Sockets in C

I am trying to pass whole structure from client to server or vice-versa. Let us assume my structure as follows struct temp { int a; char b; } I am using sendto and sending the address of the structure variable and receiving it on the other side…
codingfreak
  • 4,467
  • 11
  • 48
  • 60
60
votes
4 answers

How to make an HTTP get request in C without libcurl?

I want to write a C program to generate a Get Request without using any external libraries. Is this possible using only C libraries, using sockets ? I'm thinking of crafting a http packet(using proper formatting) and sending it to the server. Is…
asudhak
  • 2,929
  • 4
  • 22
  • 27
59
votes
12 answers

How can I get the IP Address of a local computer?

In C++, what's the easiest way to get the local computer's IP address and subnet mask? I want to be able to detect the local machine's IP address in my local network. In my particular case, I have a network with a subnet mask of 255.255.255.0 and my…
djeidot
  • 4,542
  • 4
  • 42
  • 45
58
votes
5 answers

Explain http keep-alive mechanism

Keep-alives were added to HTTP to basically reduce the significant overhead of rapidly creating and closing socket connections for each new request. The following is a summary of how it works within HTTP 1.0 and 1.1: HTTP 1.0 The HTTP 1.0…
good_evening
  • 21,085
  • 65
  • 193
  • 298
57
votes
3 answers

how to create Socket connection in Android?

I have an application in which I need to create a socket connection. My requirement is: once my socket connection is established it needs to be alive until I personally close it. And every 3 minutes I have to send data packets to the other end. Can…
Nilanchala
  • 5,891
  • 8
  • 42
  • 72