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
46
votes
6 answers

NetworkStream.ReadAsync with a cancellation token never cancels

Here the proof. Any idea what is wrong in this code ? [TestMethod] public void TestTest() { var tcp = new TcpClient() { ReceiveTimeout = 5000, SendTimeout = 20000 }; tcp.Connect(IPAddress.Parse("176.31.100.115"), 25); …
Softlion
  • 12,281
  • 11
  • 58
  • 88
45
votes
6 answers

Automatic way of enabling the access to port 4900 from the internet

I am writing a custom p2p program that runs on port 4900. In some cases when the person is behind a router, this port is not accessible from the internet. Is there an automatic way of enabling the access to the port from the internet. I am not…
Jayesh
  • 3,891
  • 11
  • 55
  • 83
45
votes
3 answers

How to handle OpenSSL SSL_ERROR_WANT_READ / WANT_WRITE on non-blocking sockets

The OpenSSL library allows to read from an underlying socket with SSL_read and write to it with SSL_write. These functions maybe return with SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE depending on their ssl protocol needs (for example when…
dantje
  • 1,739
  • 1
  • 13
  • 25
45
votes
4 answers

Socket programming - What's the difference between listen() and accept()?

I've been reading this tutorial to learn about socket programming. It seems that the listen() and accept() system calls both do the same thing, which is block and wait for a client to connect to the socket that was created with the socket() system…
Zen Hacker
  • 733
  • 2
  • 6
  • 13
45
votes
7 answers

Unix Domain Socket: Using datagram communication between one server process and several client processes

I would like to establish an IPC connection between several processes on Linux. I have never used UNIX sockets before, and thus I don't know if this is the correct approach to this problem. One process receives data (unformated, binary) and shall…
BigMick
  • 595
  • 1
  • 4
  • 7
45
votes
5 answers

What is a good buffer size for socket programming?

We are using .Net and sockets. The server is using the Socket.Sender(bytes[]) method so it just sends the entire payload. On the other side we are clients consuming the data. Socket.Receive(buffer[]). In all the examples from Microsoft (and others)…
uriDium
  • 13,110
  • 20
  • 78
  • 138
45
votes
14 answers

How do I determine all of my IP addresses when I have multiple NICs?

I have multiple Network Interface Cards on my computer, each with its own IP address. When I use gethostbyname(gethostname()) from Python's (built-in) socket module, it will only return one of them. How do I get the others?
Wilson F
  • 1,250
  • 1
  • 20
  • 37
45
votes
2 answers

SocketException: address incompatible with requested protocol

I was trying to run a .Net socket server code on Win7-64bit machine . I keep getting the following error: System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used. Error Code: 10047 The code snippet…
Amitd
  • 4,769
  • 8
  • 56
  • 82
45
votes
2 answers

java socket / output stream writes : do they block?

If I am only WRITING to a socket on an output stream, will it ever block? Only reads can block, right? Someone told me writes can block but I only see a timeout feature for the read method of a socket - Socket.setSoTimeout(). It doesn't make sense…
jbu
  • 15,831
  • 29
  • 82
  • 105
44
votes
3 answers

How to set timeout on client socket connection?

I am trying to set the timeout of a connection on the client socket in Java. I have set the connection timeout to 2000 milliseconds, i.e: this.socket.connect(this.socketAdd, timeOut); This I am trying on a web application. When a user makes a…
Vardhaman
  • 899
  • 3
  • 11
  • 10
44
votes
1 answer

What are the differences from running PHP-FPM over an Unix Socket vs a TCP/IP Socket?

There are these two ways of running PHP-FPM. I know that nothing is bullet-proof in tech, but what are the pros and cons from both methods?
Leo Cavalcante
  • 2,327
  • 2
  • 22
  • 30
44
votes
2 answers

Java socket IOException - permission denied

i am trying to connect to a server on my network running a tcp listener using the following java code. I am getting am IOException - Permission Denied. It is from an android 2.2 emulator. Does anyone know why? Socket socket = new Socket("1.1.1.1",…
Grant
  • 11,138
  • 32
  • 94
  • 140
44
votes
2 answers

Getting Errno 9: Bad file descriptor in python socket

My code is this: while 1: # Determine whether the server is up or down try: s.connect((mcip, port)) s.send(magic) data = s.recv(1024) s.close() print data except Exception, e: print e …
Mike Savi
  • 653
  • 2
  • 6
  • 11
43
votes
2 answers

Using .Net 4.5 Async Feature for Socket Programming

I've previously used BeginAccept() and BeginRead(), but with Visual Studio 2012 I want to make use of the new asynchronous (async, await) features in my socket server program. How can I complete the AcceptAsync and ReceiveAsync functions? using…
Daniel Eugen
  • 2,712
  • 8
  • 33
  • 56
43
votes
4 answers

Handling a timeout error in Python sockets

I am trying to figure out how to use the try and except to handle a socket timeout. from socket import * def main(): client_socket = socket(AF_INET,SOCK_DGRAM) client_socket.settimeout(1) server_host = 'localhost' server_port =…
Greg Brown
  • 1,227
  • 2
  • 20
  • 44