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
75
votes
7 answers

Get IP address of an interface on Linux

How can I get the IPv4 address of an interface on Linux from C code? For example, I'd like to get the IP address (if any) assigned to eth0.
leeeroy
  • 11,216
  • 17
  • 52
  • 54
75
votes
5 answers

htons() function in socket programing

I am new to socket programming and I am trying to understand the operation of htons(). I've read a few tutorials on the Internet like this and this one for instance. But I couldn't understand what htons() does exactly. I tried the following…
User123422
  • 889
  • 2
  • 11
  • 16
75
votes
13 answers

"A connection attempt failed because the connected party did not properly respond after a period of time" using WebClient

I am using the following code which is working on local machine, but when i tried the same code on server it throws me error A connection attempt failed because the connected party did not properly respond after a period of time, or established…
Ram Singh
  • 6,664
  • 35
  • 100
  • 166
75
votes
19 answers

Errno 10061 : No connection could be made because the target machine actively refused it ( client - server )

I have a problem with these client and server codes, I keep getting the [Errno 10061] No connection could be made because the target machine actively refused it I'm running the server on a virtual machine with Windows XP SP3 and the client on…
havox
  • 4,537
  • 3
  • 17
  • 7
74
votes
3 answers

What is the purpose of epoll's edge triggered option?

From epoll's man page: epoll is a variant of poll(2) that can be used either as an edge-triggered or a level-triggered interface When would one use the edge triggered option? The man page gives an example that uses it, but I don't see why it is…
Dan
  • 12,409
  • 3
  • 50
  • 87
74
votes
2 answers

ResourceWarning unclosed socket in Python 3 Unit Test

I'm modifying some code to be compatible between Python 2 and Python 3, but have observed a warning in unit test output. /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py:601: ResourceWarning: unclosed…
j12y
  • 2,112
  • 3
  • 17
  • 22
73
votes
6 answers

How to tell if a connection is dead in python

I want my python application to be able to tell when the socket on the other side has been dropped. Is there a method for this?
directedition
  • 11,145
  • 18
  • 58
  • 79
73
votes
9 answers

Proper way to stop TcpListener

I am currently using TcpListener to address incoming connections, each of which are given a thread for handling the communication and then shutdown that single connection. Code looks as follows: TcpListener listener = new TcpListener(IPAddress.Any,…
Christian P.
  • 4,784
  • 7
  • 53
  • 70
73
votes
1 answer

Creating Rooms in Socket.io

I would like to ask for your help. I'm having a hard time in my client side of socket.io, I would like to call this code in my client side to create a room in socket.io: var rooms = []; socket.on('create', function (roomname) { rooms[room] =…
Joenel de Asis
  • 1,401
  • 3
  • 15
  • 14
72
votes
6 answers

Java sending and receiving file (byte[]) over sockets

I am trying to develop a very simple client / server where the client converts a file to bytes, sends it to the server, and then converts the bytes back in to a file. Currently the program just creates an empty file. I'm not a fantastic Java…
Rookie
  • 1,879
  • 3
  • 17
  • 18
71
votes
3 answers

When does socket.recv(recv_size) return?

From test, I concluded that in following three cases the socket.recv(recv_size) will return. After the connection was closed. For example, the client side called socket.close() or any socket error occurred, it would return empty string. Some data…
redice
  • 8,437
  • 9
  • 32
  • 41
71
votes
4 answers

How to set socket timeout in C when making multiple connections?

I'm writing a simple program that makes multiple connections to different servers for status check. All these connections are constructed on-demand; up to 10 connections can be created simultaneously. I don't like the idea of one-thread-per-socket,…
RichardLiu
  • 1,902
  • 1
  • 19
  • 18
71
votes
9 answers

Why am I getting the error "connection refused" in Python? (Sockets)

I'm new to Sockets, please excuse my complete lack of understanding. I have a server script(server.py): #!/usr/bin/python import socket #import the socket module s = socket.socket() #Create a socket object host = socket.gethostname() #Get the…
Sheldon
  • 9,639
  • 20
  • 59
  • 96
71
votes
5 answers

socket programming multiple client to one server

How do you handle multiple client to connect to one server? I have this LogServer.java import javax.net.ssl.*; import javax.net.*; import java.io.*; import java.net.*; public class LogServer { private static final int PORT_NUM = 5000; public…
Harts
  • 4,023
  • 9
  • 54
  • 93
70
votes
9 answers

How to prevent hangs on SocketInputStream.socketRead0 in Java?

Performing millions of HTTP requests with different Java libraries gives me threads hung on: java.net.SocketInputStream.socketRead0() which is a native function. I tried to set up the Apache Http Client and RequestConfig to have timeouts on (I…
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82