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
11
votes
1 answer

Mocking a server-client connection with Mockito

Introduction I'm attempting to test a socket connection by sending a string from one thread to another, where server and client sockets are mocked with Mockito v1.9.5. Here's the test I'm trying to run: @Test public void testConnection() { …
karobar
  • 1,250
  • 8
  • 30
  • 61
11
votes
1 answer

Why is netcat unable to receive the second broadcast message?

While experimenting with broadcast messages (on a Debian 8.3 VM running on VirtualBox 5.0.14 on a Windows 7 laptop) I found that netcat (nc) receives only the first broadcast message. It does not receive the second broadcast message. Programs Here…
Susam Pal
  • 32,765
  • 12
  • 81
  • 103
11
votes
2 answers

Asynchronous socket I/O on Android

Is there a decent mechanism for doing asynchronous I/O using sockets on Android? I'm aware of the existence of nio channels, but they don't work for me because I need to be able to use MulticastSockets and BluetoothSockets, neither of which support…
David Given
  • 13,277
  • 9
  • 76
  • 123
11
votes
1 answer

What additional purpose can ai_protocol serve in hints while calling getaddrinfo() when ai_socktype is already specified?

The getaddrinfo accepts struct addrinfo *hints as the third argument which can be used to specify the criteria for selecting socket addresses to be returned by this function. The documentation says that we could set ai_socktype as well as…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
11
votes
2 answers

HttpsURLconnection to post and get in Android

I am developing an simple application which uses https protocol to post and get data from the server. I searched on internet but there are few resources available, I tried most of them but couldn't do it successfully. I tried with HttpClient it was…
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
11
votes
4 answers

React and Socket.io: Able to get initial data - but view doesn't update when a new post comes in

Not sure if the issue is how I have my sockets setup - or if I am incorrectly trying to render the data with React. I can successfully pull in data with my socket - yet it doesn't live update state when new data is posted to the server. My intention…
fresh5447
  • 1,242
  • 3
  • 14
  • 27
11
votes
2 answers

Is zeroing out the "sockaddr_in" structure necessary?

Everywhere I look, I see the following piece of code: struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = ip; In C++, the same idea is usually expressed…
SergeyA
  • 61,605
  • 5
  • 78
  • 137
11
votes
3 answers

Can't use ServerSocket on Android

I'm trying to listen on a port using ServerSocket on an Android device. I want to be able to connect to this port over WiFi using a computer on the same network. I get no exception when binding it to a port, however when I check netstat it…
shuwo
  • 453
  • 2
  • 6
  • 10
11
votes
5 answers

Proper way of cancelling accept and closing a Python processing/multiprocessing Listener connection

(I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably work if you run python 2.6 or use the multiprocessing backport) I currently have a program that listens to a unix socket (using a…
Henrik Gustafsson
  • 51,180
  • 9
  • 47
  • 60
11
votes
1 answer

Using SSL with UDP

Is it possible to use SSL with UDP for sockets? I know how to create TCP socket with SSL, s = socket (AF_INET, SOCK_STREAM, 0); And then I can use OpenSSL with it, but what about UDP (SOCK_DGRAM)? P.S. I want to use OpenSSL
Shaci
  • 316
  • 1
  • 4
  • 18
11
votes
3 answers

netcat sending extra "X" UDP packets

Stealing from here I have set up a small Python script which listens on a port and prints out all of the UDP packets it receives: import socket UDP_IP = "127.0.0.1" UDP_PORT = 5005 sock = socket.socket(socket.AF_INET,…
qntm
  • 4,147
  • 4
  • 27
  • 41
11
votes
2 answers

what happens at the server when client closes the connection by using readTimeout

What happens at the server when client closes the connection to the API by using readTimeout.Will the execution for the request will be done or it will break as soon as timeout occurs OR the execution will complete and the response stream clogged…
niks
  • 161
  • 1
  • 8
11
votes
5 answers

How do I receive raw, layer 2 packets in C/C++?

How do I receive layer 2 packets in POSIXy C++? The packets only have src and dst MAC address, type/length, and custom formatted data. They're not TCP or UDP or IP or IGMP or ARP or whatever - they're a home-brewed format given unto me by the…
cole-brown
  • 113
  • 1
  • 6
11
votes
1 answer

Running 'ipython notebook' gets [Errno 49] Can't assign requested address

I'm on Mac OS 10.10.4. When I run ipython notebook from command line, it gives me an error Cannot bind to localhost, using 127.0.0.1 as default ip [Errno 49] Can't assign requested address: Yans-MacBook-Pro:/ yanyang$ ipython notebook [W…
Yan Yang
  • 1,804
  • 2
  • 15
  • 37
11
votes
7 answers

can't close socket on KeyboardInterrupt

from socket import socket, AF_INET, SOCK_STREAM sock = socket(AF_INET, SOCK_STREAM) sock.bind(("localhost", 7777)) sock.listen(1) while True: try: connection, address = sock.accept() print("connected from " + address) …
Zion
  • 1,570
  • 6
  • 24
  • 36