Loco
07-24-2002, 11:14 PM
Taken from the original FAQ.
NOTE: I cut out some commentaries, they added too much noise to this discussion. Please check the original FAQ for complete answers. I added some commenaries in italics...
1.4 - What are Sockets?
Sockets are just like "worm holes" in science fiction. When things go into one end, they (should) come out of the other. Different kinds of sockets have different properties. Sockets are either connection-oriented or connectionless. Connection-oriented sockets allow for data to flow back and forth as needed, while connectionless sockets (also known as datagram sockets) allow only one message at a time to be transmitted, without an open connection. There are also different socket families. The two most common are AF_INET for internet connections, and AF_UNIX for unix IPC (interprocess communication). As stated earlier, this FAQ deals only with AF_INET sockets.
From: cara
do you mean to say a socket is a stream that is created for
data to flow between your client application and server
application.
could you explain in detail the exact meaning of socket, is it an
actual physical stream, a software package, a function or what?
From: Jose
I'm back. I found a better answer over the internet at http://www.calweb.com/~chriss/tech/socklink.html. It looks like this: "A socket is an endpoint for communication. Two cooperating sockets,
one on the local machine and one on the remote machine, form a
connection. Each of the two sockets has a unique address that is
described generically by the 16-byte sockaddr C programming
language structure."
Not a very technical explanation, however, can be useful for beginers!
From: Haru
I like Jose's answer better. But I still don't have a clear
idea. Could someone give a more detailed answer with perhaps
an example. Thanks.
From: Mahafuz
In plain words, socket is the communication tunnel/channel
endpoint used by your program.
Technically, Socket is the file system like interface
(file desriptor) provided by the OS, wrapping your communication point. Through socket you send / receive data to other programs ( usually over network).
pardon my english.
From: Mahafuz
Adding to my last comment,
Like file, a socket can be opend written to and read from.
Only special thing that you bind a socket to a process/program in a machine (server point of view) or you connect to a socket (client point of view). As a result after inding & connecting its like a pipe. When server writes it it client can read what server sent or the other way round.
This is a general picture with errors in it.
From: Helge
Take a look at this:
http://www2.hursley.ibm.com/rexxtut/socktut2.htm
From:
Did I misspell descriptor, and what is it? I need a definition>
WRobb
From: Rob Seace
A descriptor is simply a small integer value, which serves
as a sort of index into a process's list of open files...
Whether or not it is truly an "index" in any implementation
sense is irrelevant... It can just be thought of as an
opaque identifying token, which represents a particular open
file (or socket, or pipe, etc.)... You pass around this
descriptor to all I/O functions, and they in turn pass it on
to the kernel, which knows what real open file is associated
with a given descriptor...
From: Rohan
A socket is basically a tool used to communicate.
It is an abstraction of the communication hardware.
Not technically right
From: Annonymous Coward
Come on folks, if you can't understand what this is saying, not to mention all the other explanations here, you really shouldn't be trying to program. You should continue your life as a pathetic end-user, wandering around asking people who don't have time to answer a question that answers itself to explain to you why your computer won't turn on when you don't have it plugged in... Go Away!
From: yahoo onlooker
You bunch of yahoos couldn't explain a thing in simple english because you don't really know how it works.
Tell me once you create a socket in a program, how does that data stream pass to the communication buffer area and distinguish itself with other communication processes.
It is a simple question, but the answer varies depending on the OS.
Besides, it is not the main subject of the FAQ.
From: Sivapriya
But what is the physical (or atleast the logical)
structure of a socket?
If it is an abstraction to the communication hardware then what is that communication hadware and tell me how that is implemented.
From: **** YOU COWARDS
Hey, it is obvious that the people that are looking for answers here are LEARNING these things you ******* dork. You obviously are a great programmer because you have no people skills at all. I hate people like you with a ******* passion. Go ahead with your cowardly life sitting in front of a computer while all of us "end-users" are still having fun. Blow me you pathetic piece of ****.
It's partially true, however, does not give an answer to the original question
From: Question
Is socket the same as port ?
From: Ravi
what type of signals do socket convey through them
is it analog or digital
What planet do you live in???
From: mike
Can you have multiple sockets over a single port? I.e. is it safe to say that when i have multiple netscape windows open, that each of these creates a socket which communcates over the TCP port?
From: joanna
Nothing understandable upto now ...no clear definiton with
clear examples are provided.
From: Chris
Pick a port. 9960.
A socket has three streams: In, Out, and err.
How many sockets can you have open on a single port?
I dunno.. how much ram/bandwidth you got? The answer is 'theoretically' unlimited.
How many streams can you have on a socket *3*. PERIOD. If you have a multithreaded
server listening on a port for client connections and you try
making all the clients use the same streams on the same socket,
you are in for some bad mojo. It is all quite simple. Remember,
port is just the address. INFINITE sockets on a port. *3* Streams on a socket.
From: Loco
I think you have IN, OUT, OOB in, OOB out streams. I've never heard about an "err" stream in a socket but i may be wrong.
About the number of sockets you can open in a single port:
In TCP and UDP you can only bind one socket to one port and address... If you have multiple addresses, each one of them can have one socket bound to each port.
However, if your clients connect to a port bound on your machine it will have as many sockets as clients connected to that port plus the bound socket. It doesn't mean that you can bind many sockets on that port.
The number of sockets connected to a port doesn't depend directly on bandwidth but on file descriptors and memory available. There are many discussions about this in the FAQ and i don't want to begin another one, so i suggest you read the questions.
What can i say about sockets?
It is a programming interface that is used to open communication pathways between two (probably different) or more processes which are usually (but not necessarily) running in different computers. You can create connection oriented sockets, packet oriented sockets, and even raw sockets that let you work on all or part of the information used by the underlying protocol. With connection oriented sockets you create a set of pipes between two parties, these pipes assures reliable transmission of the information (for example: packet reordering if the protocol divides the data that is sent in many packets and it is received in a different order) no packet boundaries (it means that you can send a lot of information in just one funciton call without worrying about the size of the packet). The datagram oriented sockets just allow you to send and receive packets, which are not guaranteed to arrive at their destination, you don't receive confirmation of their arrival, and can be in a different order of that they were when they were sent, as well as be duplicated (i've seen this, some routers duplicate broadcast packets)
Finally, raw sockets are something like datagram sockets but they allow you to do crazy stuff on the packets of the underlying protocol (for example change header flags of a packet, even play with the source address and alike).
The interface consists of sockets descriptors, some structures, and a set of functions to perform operations on the descriptors.
The socket descriptor is just a file descriptor which allows you to use some of the functions normally used on file descriptors (such as read and write). As this interface shields the programmer from the network communication details you don't need to understand what lies beneath to program sockets.
Programming sockets is not difficult for doing simple tasks. Using TCP sockets (TCP/IP protocol, STREAM sockets) involves two parties: the server and the client. The difference between the parties is that the server creates the socket, binds it to a specific port, signal that it can receive connections, and waits for those connections; while the client just creates the socket and connects to the server to the port it is waiting to receive connections.
The server normally expects some clients will be connected to it, so it has to give some way for them to find it. That is why it uses one socket to receive connections, and for each connection it uses a separate socket, so it will have as many sockets as the number of clients connected.
The server side code would look like this:
int srvsock; // A socket descriptor is just an int
int peersock; // You need a place to hold your peer sockets
int len;
struct sockaddr_in address;
// Create descriptor
srvsock = socket(AF_INET, SOCK_STREAM, 0);
address.sin_port = htons(8080); // Bind to por 8080
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
// Bind socket to specific port and address(es)
bind(srvsock, (void*)&address, sizeof(address));
// This socket shall be a server
listen(srvsock, 10); // 10 is queue length
// Wait for new connections
do {
len = sizeof(address);
// Here address is used to receive the address of the
// connecting party
peersock = accept(srvsock, &address, &len);
// peersock is the socket descriptor used to communicate
// to a specific client
// In this example, i just send a message, close the
// connection and release the file descriptor
send(peersock, "Hi there\n", 9, 0);
shutdown(peersock, 2);
close(peersock);
} while (1)
This basically is the code everybody uses in a server.
For more information i suggest you read the man pages.
One last thing: sockets are not bound to any especific protocol, but they are used as an abstraction to many protocols that share similar functionalities. I have used X.25 sockets, TCP/IP sockets (UDP and TCP), and have read about UNIX sockets, and NETLINK sockets (in Linux)
:D (HAL)
This seems to be the best answer in this subject ;)
From: Hector Lasso
Loco,
All sockets have an error queue, which can be read using recvmsg() with the MSG_ERRQUEUE flag. Maybe this is what Chris was talking about.
Who does this guy thinks he is?
From: Vinny
Why are sockets used ?? What kinda applications would need to use sockets (other than FTP, Telnet, Web Browsers) ? Can I use sockets to do inter process communication between two process connected by ethernet and use sockets to issue commands to the client process which will process it and return status back ? Can i trnasfer huge files 64MB using sockets ? Is it used as a layer over TCP/IP, so that we dont have to use the complex TCP/IP specification ?
This guy slept through all the message
From: Geetha Ramanathan
When you create a socket of type raw socket, how do you
specify the address of the other end when trying to send
data using the send API. In the same way, how do you specify
from which process it has to receive data in the recv
command.
Thanks,
Geetha
From: arun
A Socket can be assumed as a communication tunnel established between two comm ports for data to be transfered. Socket is not a physical entity but a virtual one established when IP addess is being initialized for a network or node
From: Practic
I am a novice programmer so I do not really understand the inner workings of sockets myself. However, I believe I can explain what one is in simple terminology. Think of this: say you have two programs that are either on the same computer, or on different computers, and they want to communicate. How can they do this? They can't call functions in one another like one object to another within a program. They need some sort of common reference point. One way would be that they communicate by writing to a file. They both have a pre-agreed 'physical' file on a hardisk. They both write to it and read to it, with possible proper headers. However, writing to a hard disk is slow. So instead, they can both communicate through the operating system on the computer. What sockets are (I think) is basically a point of reference managed by the operating system. Basically when you want to 'listen' to a socket, your program tells the operating system (by a function call) that it wants to listen to a specific socket, identified by an integer. Then, whenver another program writes information to that socket, ie, sends the operating system that information with the attached idea of "send this info to the socket", the OS then checks its list of listeners to that socket, and then sends the data that is to be written to the socket to all the programs 'listenig' to that socket. That is, in simple terms, what a socket it. It is merely a centralized mechanism in the operating system by which different programs communicate. It is not 'physical'. It is not on the hard disk, etc. It is basically one program telling something to the operating system, which then the operating system sends to any program that wants to listen.
Somewhere near the truth... very bad approach!
From: Lamer
Could somebody please explain how the
MSG_ERRQUEUE stream work?
From: Rahul
I like practic's explanation. Very straightforward.
From: Naren
Socket is an end point for communication between two machines. I feel the term socket is taken from the Electrical. ex.. 5 pin socket. This will give a very good idea for sockets in a computer language.
From: Joe Jazz
Can you give me the definition of socket programming?
From: Gops
Socket Programming essetially involves writing programs which use Sockets to communicate with other programs. These programs are meant to perform functions which involve interaction with other programs. FTP Server / Client program is a good example of socket programming. FTP Server listens to port 21 for any request from a corresponding FTP Client who wishes to get a file from the sever. This kind of hand-shaking is done using sockets.
From: Gops
Refer
http://world.std.com/~jimf/papers/sockets/winsock.html#Analogy
for more analogy stuff.
From: chiku
socket is one endpoint of two-way communication link between two progams running on the network
From: ShaolinTiger
In very simple terms a socket is the UNIX/Linux method for accomplishing inter-process communication (IPC). What this means is as a commonly used analogy is that a socket is used to allow one process to speak to another, very much like the telephone is used to allow one person to speak to another. The socket is a programming interface that is used to open communication pathways between two or more generally different processes. These processes are usually, but not necessarily running on different computers. If two processes wish to communicate either on the same machine or on different computers they can’t call functions within each other like objects within a program so they need some common reference point. If they were on the same machine they could both write to a file on the hard disk, this requires both having pre-agreed a ‘physical’ file and would be slow.
Instead of this they communicate either between the operating systems on two machines or within the operating system of a single machine. Sockets are the common point of reference the processes require managed by the operating system. The program tells the operating system by a function call that it wants to listen to a specific socket identified by an integer (known as a Descriptor). Whenever another program writes information to that socket the operating system checks the listeners of the given descriptor. Effectively the socket is a centralised way of communication between applications controlled by the operating system.
Descriptors are simply integer values that serve as an index for a process’s list of open files.
There are three phases associated with all descriptor use:
· Creation
· Reading/Writing
· Destruction
There are different socket families but the two most common are AF_INET for Internet connections, and AF_UNIX which uses UNIX/Linux pathnames to identify sockets and is used for IPC between processes on the same machine (Similar to DDE in the Windows API). AF_INET uses the IP/TCP/UDP standard Internet protocols, IP addresses (dotted quads) and port numbers, which allow more than one socket on each machine. IPV6 compatibility has recently been added to UNIX/Linux sockets.
In TCP and UDP you can only bind one socket to one port and address. If you have multiple addresses on a single machine (e.g. a routing machine), each one of them can have one socket bound to each port. However, if your clients connect to a port bound on your machine it will have as many sockets as clients connected to that port plus the bound socket. The number of sockets connected is reliant partly on bandwidth but mainly on file descriptors and memory available.
There are also three types of socket currently available:
· Stream Socket (SOCK_STREAM)
· Datagram Socket (SOCK_DGRAM)
· Raw Socket. (SOCK_RAW)
Although only two of these are commonly used, the Stream Socket (SOCK_STREAM) and the Datagram Socket (SOCK_DGRAM).
· Socket Stream indicates that data will come across the socket as a stream of characters.
· Socket Datagram indicates that data will come in bunches or packets (called datagram’s).
Stream Socket is more commonly used and generally easier to implement.
Stream Socket provides a bi-directional, reliable, sequenced and unduplicated flow of data where as Datagram Socket provides a bi-directional flow of data that isn’t promised to be sequenced, reliable or unduplicated.
Raw Socket (SOCK_RAW) allows a programmer to work with the information used by the underlying protocols. Raw sockets are similar to Datagram sockets but they allow you to customise the packet totally. In packets the underlying protocol generally assigns such things as header flags, source address, source port and so on but in Raw Socket you can assign these yourself and change the header flags of the packet itself.
Once the type of Socket family and type of Socket has been chosen the socket must be created using the socket() function. The function bind(), is then used to bind a socket to a port and address and the listen() function to establish a socket which can accept calls. The listen() function gives the socket the ability to set up the maximum number of requests that can be queued (usually 5 or 10) before data starts to be denied. After you have created the socket you must wait for data sent to that socket. The accept() function is used to do this, accept() returns a new socket which is connected to the caller after the socket is established using the above functions and establish().
Usually the connections are split of into child processes using the fork() function. This allows the parent process (the socket) to continually accept new connections whilst the child processes deal directly with their clients. This allows each child process to act as though there were only one client in the world.
When you have created a listening socket on one machine you then need to use connect() to create a socket to send through and read() and write() to send and receive data. When all transmission is complete you use close() to close each end of the socket connection cleanly.
This one is near the truth, however, has made some BIG mistakes... I like mine better ;)
From: A.
Why do you care WHAT is socket? Like nobody knows WHAT is time, but most people have no problem with knowing what is coming on time (either they come on time or not)
It's like this: if you need to communicate with another computer in one way or another, you run a special procedure such as socket(...), and get back an integer number from your computer, that you can call "socket" for your pleasure or you can call it SomeIntegerNumber.
This exact connection has some properties, dos and donts, and you use some other functions like setsockopt(...) to inform the computer what properties you want.
Later on you again use just this integer number in other procedures, such as sendto, or connect() or bind(), because it is required as an argument, and because the computer - by common sense! - really needs to know the number and properties of your exact connection.
Then you run some closing procedure, and the number is
discarded.
It's like a label you get when you enter a party... for the guards to know, that you paid.
As for sample programs and stuff, just go to www.yahoo.com, and search for "socket", or "socket tutorial" - and you'll get plenty of links to all sorts of tutorials, it's just that easy, I don't feel like writing a tutorial here.
I just don't see then - what's the problem with that magical word of socket?!
It's just an integer, after all, that computer assigns to your connection, and uses to keep track of what you do with this exact connection of yours... it has to keep track somehow, right, and is there any simpler way at all?
NOTE: I cut out some commentaries, they added too much noise to this discussion. Please check the original FAQ for complete answers. I added some commenaries in italics...
1.4 - What are Sockets?
Sockets are just like "worm holes" in science fiction. When things go into one end, they (should) come out of the other. Different kinds of sockets have different properties. Sockets are either connection-oriented or connectionless. Connection-oriented sockets allow for data to flow back and forth as needed, while connectionless sockets (also known as datagram sockets) allow only one message at a time to be transmitted, without an open connection. There are also different socket families. The two most common are AF_INET for internet connections, and AF_UNIX for unix IPC (interprocess communication). As stated earlier, this FAQ deals only with AF_INET sockets.
From: cara
do you mean to say a socket is a stream that is created for
data to flow between your client application and server
application.
could you explain in detail the exact meaning of socket, is it an
actual physical stream, a software package, a function or what?
From: Jose
I'm back. I found a better answer over the internet at http://www.calweb.com/~chriss/tech/socklink.html. It looks like this: "A socket is an endpoint for communication. Two cooperating sockets,
one on the local machine and one on the remote machine, form a
connection. Each of the two sockets has a unique address that is
described generically by the 16-byte sockaddr C programming
language structure."
Not a very technical explanation, however, can be useful for beginers!
From: Haru
I like Jose's answer better. But I still don't have a clear
idea. Could someone give a more detailed answer with perhaps
an example. Thanks.
From: Mahafuz
In plain words, socket is the communication tunnel/channel
endpoint used by your program.
Technically, Socket is the file system like interface
(file desriptor) provided by the OS, wrapping your communication point. Through socket you send / receive data to other programs ( usually over network).
pardon my english.
From: Mahafuz
Adding to my last comment,
Like file, a socket can be opend written to and read from.
Only special thing that you bind a socket to a process/program in a machine (server point of view) or you connect to a socket (client point of view). As a result after inding & connecting its like a pipe. When server writes it it client can read what server sent or the other way round.
This is a general picture with errors in it.
From: Helge
Take a look at this:
http://www2.hursley.ibm.com/rexxtut/socktut2.htm
From:
Did I misspell descriptor, and what is it? I need a definition>
WRobb
From: Rob Seace
A descriptor is simply a small integer value, which serves
as a sort of index into a process's list of open files...
Whether or not it is truly an "index" in any implementation
sense is irrelevant... It can just be thought of as an
opaque identifying token, which represents a particular open
file (or socket, or pipe, etc.)... You pass around this
descriptor to all I/O functions, and they in turn pass it on
to the kernel, which knows what real open file is associated
with a given descriptor...
From: Rohan
A socket is basically a tool used to communicate.
It is an abstraction of the communication hardware.
Not technically right
From: Annonymous Coward
Come on folks, if you can't understand what this is saying, not to mention all the other explanations here, you really shouldn't be trying to program. You should continue your life as a pathetic end-user, wandering around asking people who don't have time to answer a question that answers itself to explain to you why your computer won't turn on when you don't have it plugged in... Go Away!
From: yahoo onlooker
You bunch of yahoos couldn't explain a thing in simple english because you don't really know how it works.
Tell me once you create a socket in a program, how does that data stream pass to the communication buffer area and distinguish itself with other communication processes.
It is a simple question, but the answer varies depending on the OS.
Besides, it is not the main subject of the FAQ.
From: Sivapriya
But what is the physical (or atleast the logical)
structure of a socket?
If it is an abstraction to the communication hardware then what is that communication hadware and tell me how that is implemented.
From: **** YOU COWARDS
Hey, it is obvious that the people that are looking for answers here are LEARNING these things you ******* dork. You obviously are a great programmer because you have no people skills at all. I hate people like you with a ******* passion. Go ahead with your cowardly life sitting in front of a computer while all of us "end-users" are still having fun. Blow me you pathetic piece of ****.
It's partially true, however, does not give an answer to the original question
From: Question
Is socket the same as port ?
From: Ravi
what type of signals do socket convey through them
is it analog or digital
What planet do you live in???
From: mike
Can you have multiple sockets over a single port? I.e. is it safe to say that when i have multiple netscape windows open, that each of these creates a socket which communcates over the TCP port?
From: joanna
Nothing understandable upto now ...no clear definiton with
clear examples are provided.
From: Chris
Pick a port. 9960.
A socket has three streams: In, Out, and err.
How many sockets can you have open on a single port?
I dunno.. how much ram/bandwidth you got? The answer is 'theoretically' unlimited.
How many streams can you have on a socket *3*. PERIOD. If you have a multithreaded
server listening on a port for client connections and you try
making all the clients use the same streams on the same socket,
you are in for some bad mojo. It is all quite simple. Remember,
port is just the address. INFINITE sockets on a port. *3* Streams on a socket.
From: Loco
I think you have IN, OUT, OOB in, OOB out streams. I've never heard about an "err" stream in a socket but i may be wrong.
About the number of sockets you can open in a single port:
In TCP and UDP you can only bind one socket to one port and address... If you have multiple addresses, each one of them can have one socket bound to each port.
However, if your clients connect to a port bound on your machine it will have as many sockets as clients connected to that port plus the bound socket. It doesn't mean that you can bind many sockets on that port.
The number of sockets connected to a port doesn't depend directly on bandwidth but on file descriptors and memory available. There are many discussions about this in the FAQ and i don't want to begin another one, so i suggest you read the questions.
What can i say about sockets?
It is a programming interface that is used to open communication pathways between two (probably different) or more processes which are usually (but not necessarily) running in different computers. You can create connection oriented sockets, packet oriented sockets, and even raw sockets that let you work on all or part of the information used by the underlying protocol. With connection oriented sockets you create a set of pipes between two parties, these pipes assures reliable transmission of the information (for example: packet reordering if the protocol divides the data that is sent in many packets and it is received in a different order) no packet boundaries (it means that you can send a lot of information in just one funciton call without worrying about the size of the packet). The datagram oriented sockets just allow you to send and receive packets, which are not guaranteed to arrive at their destination, you don't receive confirmation of their arrival, and can be in a different order of that they were when they were sent, as well as be duplicated (i've seen this, some routers duplicate broadcast packets)
Finally, raw sockets are something like datagram sockets but they allow you to do crazy stuff on the packets of the underlying protocol (for example change header flags of a packet, even play with the source address and alike).
The interface consists of sockets descriptors, some structures, and a set of functions to perform operations on the descriptors.
The socket descriptor is just a file descriptor which allows you to use some of the functions normally used on file descriptors (such as read and write). As this interface shields the programmer from the network communication details you don't need to understand what lies beneath to program sockets.
Programming sockets is not difficult for doing simple tasks. Using TCP sockets (TCP/IP protocol, STREAM sockets) involves two parties: the server and the client. The difference between the parties is that the server creates the socket, binds it to a specific port, signal that it can receive connections, and waits for those connections; while the client just creates the socket and connects to the server to the port it is waiting to receive connections.
The server normally expects some clients will be connected to it, so it has to give some way for them to find it. That is why it uses one socket to receive connections, and for each connection it uses a separate socket, so it will have as many sockets as the number of clients connected.
The server side code would look like this:
int srvsock; // A socket descriptor is just an int
int peersock; // You need a place to hold your peer sockets
int len;
struct sockaddr_in address;
// Create descriptor
srvsock = socket(AF_INET, SOCK_STREAM, 0);
address.sin_port = htons(8080); // Bind to por 8080
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
// Bind socket to specific port and address(es)
bind(srvsock, (void*)&address, sizeof(address));
// This socket shall be a server
listen(srvsock, 10); // 10 is queue length
// Wait for new connections
do {
len = sizeof(address);
// Here address is used to receive the address of the
// connecting party
peersock = accept(srvsock, &address, &len);
// peersock is the socket descriptor used to communicate
// to a specific client
// In this example, i just send a message, close the
// connection and release the file descriptor
send(peersock, "Hi there\n", 9, 0);
shutdown(peersock, 2);
close(peersock);
} while (1)
This basically is the code everybody uses in a server.
For more information i suggest you read the man pages.
One last thing: sockets are not bound to any especific protocol, but they are used as an abstraction to many protocols that share similar functionalities. I have used X.25 sockets, TCP/IP sockets (UDP and TCP), and have read about UNIX sockets, and NETLINK sockets (in Linux)
:D (HAL)
This seems to be the best answer in this subject ;)
From: Hector Lasso
Loco,
All sockets have an error queue, which can be read using recvmsg() with the MSG_ERRQUEUE flag. Maybe this is what Chris was talking about.
Who does this guy thinks he is?
From: Vinny
Why are sockets used ?? What kinda applications would need to use sockets (other than FTP, Telnet, Web Browsers) ? Can I use sockets to do inter process communication between two process connected by ethernet and use sockets to issue commands to the client process which will process it and return status back ? Can i trnasfer huge files 64MB using sockets ? Is it used as a layer over TCP/IP, so that we dont have to use the complex TCP/IP specification ?
This guy slept through all the message
From: Geetha Ramanathan
When you create a socket of type raw socket, how do you
specify the address of the other end when trying to send
data using the send API. In the same way, how do you specify
from which process it has to receive data in the recv
command.
Thanks,
Geetha
From: arun
A Socket can be assumed as a communication tunnel established between two comm ports for data to be transfered. Socket is not a physical entity but a virtual one established when IP addess is being initialized for a network or node
From: Practic
I am a novice programmer so I do not really understand the inner workings of sockets myself. However, I believe I can explain what one is in simple terminology. Think of this: say you have two programs that are either on the same computer, or on different computers, and they want to communicate. How can they do this? They can't call functions in one another like one object to another within a program. They need some sort of common reference point. One way would be that they communicate by writing to a file. They both have a pre-agreed 'physical' file on a hardisk. They both write to it and read to it, with possible proper headers. However, writing to a hard disk is slow. So instead, they can both communicate through the operating system on the computer. What sockets are (I think) is basically a point of reference managed by the operating system. Basically when you want to 'listen' to a socket, your program tells the operating system (by a function call) that it wants to listen to a specific socket, identified by an integer. Then, whenver another program writes information to that socket, ie, sends the operating system that information with the attached idea of "send this info to the socket", the OS then checks its list of listeners to that socket, and then sends the data that is to be written to the socket to all the programs 'listenig' to that socket. That is, in simple terms, what a socket it. It is merely a centralized mechanism in the operating system by which different programs communicate. It is not 'physical'. It is not on the hard disk, etc. It is basically one program telling something to the operating system, which then the operating system sends to any program that wants to listen.
Somewhere near the truth... very bad approach!
From: Lamer
Could somebody please explain how the
MSG_ERRQUEUE stream work?
From: Rahul
I like practic's explanation. Very straightforward.
From: Naren
Socket is an end point for communication between two machines. I feel the term socket is taken from the Electrical. ex.. 5 pin socket. This will give a very good idea for sockets in a computer language.
From: Joe Jazz
Can you give me the definition of socket programming?
From: Gops
Socket Programming essetially involves writing programs which use Sockets to communicate with other programs. These programs are meant to perform functions which involve interaction with other programs. FTP Server / Client program is a good example of socket programming. FTP Server listens to port 21 for any request from a corresponding FTP Client who wishes to get a file from the sever. This kind of hand-shaking is done using sockets.
From: Gops
Refer
http://world.std.com/~jimf/papers/sockets/winsock.html#Analogy
for more analogy stuff.
From: chiku
socket is one endpoint of two-way communication link between two progams running on the network
From: ShaolinTiger
In very simple terms a socket is the UNIX/Linux method for accomplishing inter-process communication (IPC). What this means is as a commonly used analogy is that a socket is used to allow one process to speak to another, very much like the telephone is used to allow one person to speak to another. The socket is a programming interface that is used to open communication pathways between two or more generally different processes. These processes are usually, but not necessarily running on different computers. If two processes wish to communicate either on the same machine or on different computers they can’t call functions within each other like objects within a program so they need some common reference point. If they were on the same machine they could both write to a file on the hard disk, this requires both having pre-agreed a ‘physical’ file and would be slow.
Instead of this they communicate either between the operating systems on two machines or within the operating system of a single machine. Sockets are the common point of reference the processes require managed by the operating system. The program tells the operating system by a function call that it wants to listen to a specific socket identified by an integer (known as a Descriptor). Whenever another program writes information to that socket the operating system checks the listeners of the given descriptor. Effectively the socket is a centralised way of communication between applications controlled by the operating system.
Descriptors are simply integer values that serve as an index for a process’s list of open files.
There are three phases associated with all descriptor use:
· Creation
· Reading/Writing
· Destruction
There are different socket families but the two most common are AF_INET for Internet connections, and AF_UNIX which uses UNIX/Linux pathnames to identify sockets and is used for IPC between processes on the same machine (Similar to DDE in the Windows API). AF_INET uses the IP/TCP/UDP standard Internet protocols, IP addresses (dotted quads) and port numbers, which allow more than one socket on each machine. IPV6 compatibility has recently been added to UNIX/Linux sockets.
In TCP and UDP you can only bind one socket to one port and address. If you have multiple addresses on a single machine (e.g. a routing machine), each one of them can have one socket bound to each port. However, if your clients connect to a port bound on your machine it will have as many sockets as clients connected to that port plus the bound socket. The number of sockets connected is reliant partly on bandwidth but mainly on file descriptors and memory available.
There are also three types of socket currently available:
· Stream Socket (SOCK_STREAM)
· Datagram Socket (SOCK_DGRAM)
· Raw Socket. (SOCK_RAW)
Although only two of these are commonly used, the Stream Socket (SOCK_STREAM) and the Datagram Socket (SOCK_DGRAM).
· Socket Stream indicates that data will come across the socket as a stream of characters.
· Socket Datagram indicates that data will come in bunches or packets (called datagram’s).
Stream Socket is more commonly used and generally easier to implement.
Stream Socket provides a bi-directional, reliable, sequenced and unduplicated flow of data where as Datagram Socket provides a bi-directional flow of data that isn’t promised to be sequenced, reliable or unduplicated.
Raw Socket (SOCK_RAW) allows a programmer to work with the information used by the underlying protocols. Raw sockets are similar to Datagram sockets but they allow you to customise the packet totally. In packets the underlying protocol generally assigns such things as header flags, source address, source port and so on but in Raw Socket you can assign these yourself and change the header flags of the packet itself.
Once the type of Socket family and type of Socket has been chosen the socket must be created using the socket() function. The function bind(), is then used to bind a socket to a port and address and the listen() function to establish a socket which can accept calls. The listen() function gives the socket the ability to set up the maximum number of requests that can be queued (usually 5 or 10) before data starts to be denied. After you have created the socket you must wait for data sent to that socket. The accept() function is used to do this, accept() returns a new socket which is connected to the caller after the socket is established using the above functions and establish().
Usually the connections are split of into child processes using the fork() function. This allows the parent process (the socket) to continually accept new connections whilst the child processes deal directly with their clients. This allows each child process to act as though there were only one client in the world.
When you have created a listening socket on one machine you then need to use connect() to create a socket to send through and read() and write() to send and receive data. When all transmission is complete you use close() to close each end of the socket connection cleanly.
This one is near the truth, however, has made some BIG mistakes... I like mine better ;)
From: A.
Why do you care WHAT is socket? Like nobody knows WHAT is time, but most people have no problem with knowing what is coming on time (either they come on time or not)
It's like this: if you need to communicate with another computer in one way or another, you run a special procedure such as socket(...), and get back an integer number from your computer, that you can call "socket" for your pleasure or you can call it SomeIntegerNumber.
This exact connection has some properties, dos and donts, and you use some other functions like setsockopt(...) to inform the computer what properties you want.
Later on you again use just this integer number in other procedures, such as sendto, or connect() or bind(), because it is required as an argument, and because the computer - by common sense! - really needs to know the number and properties of your exact connection.
Then you run some closing procedure, and the number is
discarded.
It's like a label you get when you enter a party... for the guards to know, that you paid.
As for sample programs and stuff, just go to www.yahoo.com, and search for "socket", or "socket tutorial" - and you'll get plenty of links to all sorts of tutorials, it's just that easy, I don't feel like writing a tutorial here.
I just don't see then - what's the problem with that magical word of socket?!
It's just an integer, after all, that computer assigns to your connection, and uses to keep track of what you do with this exact connection of yours... it has to keep track somehow, right, and is there any simpler way at all?