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
70
votes
3 answers

Why do we cast sockaddr_in to sockaddr when calling bind()?

The bind() function accepts a pointer to a sockaddr, but in all examples I've seen, a sockaddr_in structure is used instead, and is cast to sockaddr: struct sockaddr_in name; ...; if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) { …
sashoalm
  • 75,001
  • 122
  • 434
  • 781
70
votes
5 answers

How to support both IPv4 and IPv6 connections

I'm currently working on a UDP socket application and I need to build in support so that IPV4 and IPV6 connections can send packets to a server. I was hoping that someone could help me out and point me in the right direction; the majority of the…
Charles
  • 2,615
  • 3
  • 29
  • 35
69
votes
5 answers

Basic Python client socket example

I've been trying to wrap my head around how sockets work, and I've been trying to pick apart some sample code I found at this page for a very simple client socket program. Since this is basic sample code, I assumed it had no errors, but when I try…
dukbur
  • 737
  • 2
  • 7
  • 8
69
votes
2 answers

select vs poll vs epoll

I am designing a new server which needs to support thousands of UDP connections (somewhere around 100,000 sessions). Any input or suggestions on which one to use?
ravi
  • 745
  • 1
  • 7
  • 4
69
votes
7 answers

How to talk to UDP sockets with HTML5?

What I have : A C++ application server running, Ready to send data to client which is supposed to a HTML5 page or app. What I want : Is there any way to communicate using udp port with HTML5 given both c++ server and HTML5 app are local to system…
mkkhedawat
  • 1,687
  • 2
  • 17
  • 34
68
votes
2 answers

TCPClient vs Socket in C#

I don't see much use of TCPClient, yet there is a lot of Socket? What is the major difference between them and when would you use each? I understand that .NET Socket is written on top of WINSOCK, and TCPClient is a wrapper over Socket class. Thus…
Sasha
67
votes
5 answers

Python send UDP packet

I am trying to write a program to send UDP packets, as in https://wiki.python.org/moin/UdpCommunication The code appears to be in Python 2: import socket UDP_IP = "127.0.0.1" UDP_PORT = 5005 MESSAGE = "Hello, World!" print "UDP target IP:",…
user2059619
  • 814
  • 1
  • 6
  • 7
65
votes
22 answers

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I've run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista. Here's my…
bitcycle
  • 7,632
  • 16
  • 70
  • 121
65
votes
3 answers

How to increase MySQL connections(max_connections)?

Every socket of MySQL Database will have defaults connections as 100 but I am looking for any way to increase the number of possible connections > 100 to a socket connection of MySQL Database.
shekhar
  • 867
  • 2
  • 7
  • 15
65
votes
5 answers

C# An established connection was aborted by the software in your host machine

These errors are getting more and more frequent on my Game Server. They are causing the server to keep closing and restarting... System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host…
Richard Williams
  • 683
  • 1
  • 5
  • 4
64
votes
6 answers

Will (and should) there be sockets in C++11?

Is the new C++11 going to contain any socket library? So that one could do something std::socket-ish? Seeing as how std::thread will be added, it feels as if sockets should be added as well. C-style sockets are a pain... They feel extremely…
thomasvakili
  • 1,138
  • 2
  • 10
  • 17
64
votes
3 answers

Why is sin_addr inside the structure in_addr?

My doubt is related to the following structure of sockets in UNIX : struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see…
Deepankar Bajpeyi
  • 5,661
  • 11
  • 44
  • 64
64
votes
1 answer

Python socket.error: [Errno 111] Connection refused

I am trying to write a program for file transfer using sockets. The server end of the code is running fine. However, in the client side I get the following error Traceback (most recent call last): File "client.py", line 54, in…
srnvs
  • 997
  • 2
  • 10
  • 22
64
votes
3 answers

How to bind to any available port?

I need an app that sends an UDP packet to some network server and receives the response. The server replies to the same port number where request came from, so I first need to bind() my socket to any UDP port number. Hardcoding the UDP port number…
Soonts
  • 20,079
  • 9
  • 57
  • 130
63
votes
2 answers

Reasoning behind C sockets sockaddr and sockaddr_storage

I'm looking at functions such as connect() and bind() in C sockets and notice that they take a pointer to a sockaddr struct. I've been reading and to make your application AF-Independent, it is useful to use the sockaddr_storage struct pointer and…
Matt Vaughan
  • 1,135
  • 2
  • 13
  • 19