Tag Info

Hot answers tagged

22

sockets use different APIs That's not entirely true. There are some additional functions for use with sockets, but you can use, e.g., normal read() and write() on a socket fd. how does this "Everything is a file" apply here? In the sense that a file descriptor is involved. If your definition of "file" is a discrete sequence of bytes stored in a ...


20

In the simplest terms, a socket is a pseudo-file that represents a network connection. Once a socket has been created (using the proper primitives, and the proper parameters to identify the other host), writes to the socket are turned into network packets that get sent out, and data received from the network can be read from the socket. In one regard, ...


20

The limit on "open files" is not really just for files. It's a limit on the number of kernel handles a single process can use at one time. Historically, the only thing that programs would typically open a lot of were files, so this became known as a limit on the number of open files. There is a limit to help prevent processes from say, opening a lot of files ...


19

That's the inode number for the pipe or socket in question. A pipe is a unidirectional channel, with a write end and a read end. In your example, it looks like FD 5 and FD 6 are talking to each other, since the inode numbers are the same. (Maybe not, though. See below.) More common than seeing a program talking to itself over a pipe is a pair of separate ...


18

With socat (a 'bidirectional data relay between two data channels') you can can connect to the unix domain socket like this: $ socat - UNIX-CONNECT:/tmp/memcached.sock


17

A socket is just a logical endpoint for communication. They exist on the transport layer. You can send and receive things on a socket, you can bind and listen to a socket. A socket is specific to a protocol, machine, and port, and is addressed as such in the header of a packet. Beej's guides to Network Programming and Inter-Process Communication both have ...


16

Since kernel 3.3, it is possible using ss — see Stéphane Chazelas's answer. In older versions, according to the author of lsof, it was impossible to find this out: the Linux kernel does not expose this information. Source: 2003 thread on comp.unix.admin. The number shown in /proc/$pid/fd/$fd is the socket's inode number in the virtual socket filesystem. ...


16

UNIX domain sockets should offer better performance than TCP sockets over loopback interface (less copying of data, fewer context switches). Beware though that sockets are only reachable from programs that are running on the same server (there's no network support, obviously) and that the programs need to have the necessary permissions to access the socket ...


15

For sockets you can find more information about the inode in /proc/net/tcp, /proc/net/udp or /proc/net/unix. For example: ls -l /proc/<pid>/fd lrwx------ 1 root root 64 May 26 22:03 3 -> socket:[53710569] We see inode is 53710569. head -n1 < tcp ; grep -a 53710569 tcp sl local_address rem_address st tx_queue rx_queue tr tm->when ...


15

UNIX domain sockets and FIFO may share some part of their implementation but they are conceptually very different. FIFO functions at a very low level. One process writes bytes into the pipe and another one reads from it. A UNIX domain socket has the same behaviour than a TCP/IP socket. A socket is bidirectionnal and can be used by a lot of processes ...


14

With netcat-openbsd, there is a -U option. If you don't have it, you probably have netcat-traditional installed instead; I'd suggest switching.


11

The reason why TCP/IP sockets use file descriptors is that, when the sockets interface was first designed and implemented (in BSD Unix, in 1983), its designers felt that a network connection was analogous to a file - you can read, write, and close both, and that it would fit well with the Unix idea of "everything is a file". Other TCP/IP network stack ...


10

What and where is file per each socket? "Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other ...


10

"Everything is a file" is just an overstatement. It was novel in 1970s and it was a primary distinguishing characteristic of UNIX. But it's just a marketing concept, not a real foundation of UNIX, because it's obviously not true. It's not beneficial or sensible to treat EVERYTHING as a file. Is CPU a file? Does your program read() a CPU to get a new ...


9

You can use socat on Debian. To install it: # apt-get install socat


9

Erkki Seppala actually has a tool that retrieves this information from the Linux kernel with gdb.. It's available here.


9

These are abstract sockets, that live outside the filesystem namespace. netstat --unix, lsof -U and other commands print an @ sign instead of the nul byte that's at the start of the pathname.


9

A socket is a file. But not all files have names. Here are a few examples of files that don't have names: Any file that used to have a name, and is now deleted, but is still opened by a program. An unnamed pipe, such as one created by the | shell operator. Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can ...


8

Note: I now maintain a lsof wrapper that combines both approaches described here and also adds information for peers of loopback TCP connections at https://github.com/stephane-chazelas/misc-scripts/blob/master/lsofc Linux-3.3 and above. On Linux, since kernel version 3.3 (and provided the UNIX_DIAG feature is built in the kernel), the peer of a given ...


8

As recommended by IBM: use lsof -i -n and look for port XY. If you want parseable output from lsof, use the -F flag and parse the output with awk. You can get pre-compiled binaries for AIX V5. I don't know if there are pre-compiled binaries for V6; if there aren't, get the source and compile it.


8

To cause an exiting connection to timeout you can use iptables. Just enable a DROP rule on the port you want to disable. So to simulate a timeout for your Samaba server, while an active connection is up, execute the following on the server: sudo iptables -A INPUT -p tcp --dport 445 -j DROP The DROP target will not reply with a RST packet or ICMP error ...


7

I believe that the idea of the socket being unavailable to a program is to allow any TCP data segments still in transit to arrive, and get discarded by the kernel. That is, it's possible for an application to call close(2) on a socket, but routing delays or mishaps to control packets or what have you can allow the other side of a TCP connection to send data ...


7

Actually, the above answer is not entirely accurate. The sysctls net.inet.ip.portrange.first and net.inet.ip.portrange.last specify the range of ports the OS can allocate for random ports. You would want to make sure that the range of reserved ports for your application does not fall within these variables. Take a look in the FreeBSD Handbook, section: ...


7

Technically, there's no such thing as a "reserved port". In TCP/UDP, the only way to "reserve" a port is to actually bind() a socket to it. A bound port will not be used by other applications; an unused port is, well, unused so other applications are free to use it. If you are writing server software, then you can bind your sockets to specific ports as ...


7

When you are using TCP, you are also using the whole network stack. Even if you are on the same machine, this implies that packets are encapsulated and decapsulated to use the network stack and the related protocols. If you use unix domain sockets, you will not be forced to go through all the network protocols that are required otherwise. The sockets are ...


7

Since you are programming in C I thought of posting a little snippet which shows you if the port is open or not, I have programmed to output a string. You can easily change it to fit your need. Answer to your second question, like everyone here said, you can pretty much use any port if you are the super user (root) on the system if the port is not used by ...


6

by default NFS is enabled you can remove packages: apt-get --purge remove nfs-kernel-server nfs-common portmap or stop services temporary: /etc/init.d/portmap stop /etc/init.d/nfs-kernel-server stop or stop them permanently: service portmap stop service nfs-kernel-server stop


6

It's part of the TCP (or UDP, etc.) header, in the packet. So the server finds out because the client tells it. This is similar to how it finds out the client's IP address (which is part of the IP header). E.g., every TCP packet includes an IP header (with source IP, destination IP, and protocol [TCP], at least). Then there is a TCP header (with source and ...


6

They are commonly found in /tmp or a subdirectory thereof. Note that everything in /tmp is subject to erasure at shutdown -- not that it necessarily is erased, just beware that it can be, so if you use that, check if you have to create your subdirectory each time. You will want to use a subdirectory if you want to restrict access via permissions, since ...


6

man 7 unix: The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is independent of the file system. ...



Only top voted, non community-wiki answers of a minimum length are eligible