MOA81
asked on
Ports & Sockets General Networking Questions from Networking and Programming point of view
Dear Experts
This should be an easy one.
I would like someone to explain the following:
- what is a ports (Both physically and logically) appreciate if you provide examples.
- what is a socket (Both physically and logically) appreciate if you provide examples.
I believe this question will be a split cause I need the above explanations in both perspectives Networks and Programming.
Appreciate all the helpfull feedback.
Thanks in advance
This should be an easy one.
I would like someone to explain the following:
- what is a ports (Both physically and logically) appreciate if you provide examples.
- what is a socket (Both physically and logically) appreciate if you provide examples.
I believe this question will be a split cause I need the above explanations in both perspectives Networks and Programming.
Appreciate all the helpfull feedback.
Thanks in advance
Socket and ports have no real physical meaning, but I'll try to give them physical analogies.
A socket is basically a channel that a program establishes in order to communicate over a network. The socket must be associated with a port (an address on the computer) in order to send and receive information. There are 65,536 ports available on a computer, but there is no limit imposed on the number of sockets.
A port is sort of like a post office box. The person who owns that PO box is the socket. If a person wants to send and receive letters, they need to gain ownership of a PO box at the post office. There can be any number of people, but only a certain number of boxes are available. Naturally, two people can't use the same box to send and receive mail.
Still in the post office analogy, when you want to send a message to another person (socket) in another city (computer), you must label that message with the destination city (IP address) and PO box address (port) of the recipient. All messages that come into that mailbox are received by its current owner (the socket). The recipient can have the same address as you in their city, or a different one.
I hope that makes things more clear.
A socket is basically a channel that a program establishes in order to communicate over a network. The socket must be associated with a port (an address on the computer) in order to send and receive information. There are 65,536 ports available on a computer, but there is no limit imposed on the number of sockets.
A port is sort of like a post office box. The person who owns that PO box is the socket. If a person wants to send and receive letters, they need to gain ownership of a PO box at the post office. There can be any number of people, but only a certain number of boxes are available. Naturally, two people can't use the same box to send and receive mail.
Still in the post office analogy, when you want to send a message to another person (socket) in another city (computer), you must label that message with the destination city (IP address) and PO box address (port) of the recipient. All messages that come into that mailbox are received by its current owner (the socket). The recipient can have the same address as you in their city, or a different one.
I hope that makes things more clear.
ASKER
Dear jjeff1
Thanks for the input I went through these links actually before issuing the question
I would appreciate more examples from the real life
Dear gis-jedi
Thanks thats clear, basic but clear,
Can we dig in more details and go technical with examples
I will Increase the points as we go through the process
If you also can supply some code and comment on it would be appreciated
What I want is to see a live case example where sockets and ports are used
As I said
I will Increase the points as we go through the process
Thanks for the input I went through these links actually before issuing the question
I would appreciate more examples from the real life
Dear gis-jedi
Thanks thats clear, basic but clear,
Can we dig in more details and go technical with examples
I will Increase the points as we go through the process
If you also can supply some code and comment on it would be appreciated
What I want is to see a live case example where sockets and ports are used
As I said
I will Increase the points as we go through the process
Well, socket programming is a very broad field. There are a number of different ways you can use sockets.
In Windows, socket programming is done with the WinSock API. This is some example code for a server in a client/server application in C++ (taken from http://www.codeproject.com/internet/winsockintro01.asp):
// the socket handle
SOCKET server;
// WSADATA is a structure that holds information on WinSock
WSADATA wsaData;
//The sockaddr_in specifies the address of the socket for TCP/IP sockets.
sockaddr_in local;
//WSAStartup initializes WinSock.
//The first parameter specifies the highest version of the WinSock the program is allowed to use (1.1)
int wsaret=WSAStartup(0x101, &wsaData);
//WSAStartup returns zero on success.
if(wsaret != 0)
{
return 0;
}
The above block of code basically sets up WinSock. No actual networking is done at this point. We have just loaded the DLL and such.
//Now we populate the sockaddr_in structure
local.sin_family = AF_INET; //Address family
local.sin_addr.s_addr = INADDR_ANY; // Wild card IP address
local.sin_port = htons((u_short)20248); // port to use
What we are doing here is telling the server socket which port to connect to. The server socket will sit on this port 20248 and wait for someone to connect to it. No IP address is given because we are not going to try to connect to another machine, just wait passively.
// create the socket
server=socket(AF_INET,SOCK _STREAM,0) ;
// If the socket() function fails we exit
if(server==INVALID_SOCKET)
{
return 0;
}
//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
if (bind(server, (sockaddr*)&local, sizeof(local)) != 0)
{
return 0;
}
The socket was created and bound to its port, but still no networking was done.
// listen instructs the socket to listen for incoming connections from clients. The second arg is the backlog
if (listen(server,10) != 0)
{
return 0;
}
Now we are actually sitting and waiting for someone to connect. This is called "synchronous" networking because the server program will sit and wait, doing nothing, until a connection comes in. You can also do asynchronous socket programming which will send a Windows message to the program when a connection comes in, if you need to be doing other things on that thread instead of just waiting.
Once the listen function returns we know someone is trying to connect.
// this is the handle to the client socket.
SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);
while(true)//we are looping endlessly
{
char temp[512];
//accept() will accept an incoming client connection
client=accept(server, (struct sockaddr*)&from,&fromlen);
sprintf(temp,"Your IP is %s\r\n", inet_ntoa(from.sin_addr));
We accepted the connection and printed out the client's IP address.
//we simply send this string to the client
send(client,temp,strlen(te mp),0);
cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
Send the client the string telling them their IP.
//close the client socket
closesocket(client);
}
Closing the client socket made up disconnect. Now we have to close the server socket and cleanup the API:
//closesocket() closes the socket and releases the socket descriptor
closesocket(server);
WSACleanup();
As you can see it is not too bad. This code would just be for a server that sits and waits for a client, and then sends back a single response string giving them their IP before disconnecting.
The client program would use similar code that connects to the server's IP address at port 20248. You can do this with a Telnet program as explained in the article I linked.
Let me know if this makes sense. I don't know how well you know programming so I apologize if went too fast or too slow.
In Windows, socket programming is done with the WinSock API. This is some example code for a server in a client/server application in C++ (taken from http://www.codeproject.com/internet/winsockintro01.asp):
// the socket handle
SOCKET server;
// WSADATA is a structure that holds information on WinSock
WSADATA wsaData;
//The sockaddr_in specifies the address of the socket for TCP/IP sockets.
sockaddr_in local;
//WSAStartup initializes WinSock.
//The first parameter specifies the highest version of the WinSock the program is allowed to use (1.1)
int wsaret=WSAStartup(0x101, &wsaData);
//WSAStartup returns zero on success.
if(wsaret != 0)
{
return 0;
}
The above block of code basically sets up WinSock. No actual networking is done at this point. We have just loaded the DLL and such.
//Now we populate the sockaddr_in structure
local.sin_family = AF_INET; //Address family
local.sin_addr.s_addr = INADDR_ANY; // Wild card IP address
local.sin_port = htons((u_short)20248); // port to use
What we are doing here is telling the server socket which port to connect to. The server socket will sit on this port 20248 and wait for someone to connect to it. No IP address is given because we are not going to try to connect to another machine, just wait passively.
// create the socket
server=socket(AF_INET,SOCK
// If the socket() function fails we exit
if(server==INVALID_SOCKET)
{
return 0;
}
//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
if (bind(server, (sockaddr*)&local, sizeof(local)) != 0)
{
return 0;
}
The socket was created and bound to its port, but still no networking was done.
// listen instructs the socket to listen for incoming connections from clients. The second arg is the backlog
if (listen(server,10) != 0)
{
return 0;
}
Now we are actually sitting and waiting for someone to connect. This is called "synchronous" networking because the server program will sit and wait, doing nothing, until a connection comes in. You can also do asynchronous socket programming which will send a Windows message to the program when a connection comes in, if you need to be doing other things on that thread instead of just waiting.
Once the listen function returns we know someone is trying to connect.
// this is the handle to the client socket.
SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);
while(true)//we are looping endlessly
{
char temp[512];
//accept() will accept an incoming client connection
client=accept(server, (struct sockaddr*)&from,&fromlen);
sprintf(temp,"Your IP is %s\r\n", inet_ntoa(from.sin_addr));
We accepted the connection and printed out the client's IP address.
//we simply send this string to the client
send(client,temp,strlen(te
cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
Send the client the string telling them their IP.
//close the client socket
closesocket(client);
}
Closing the client socket made up disconnect. Now we have to close the server socket and cleanup the API:
//closesocket() closes the socket and releases the socket descriptor
closesocket(server);
WSACleanup();
As you can see it is not too bad. This code would just be for a server that sits and waits for a client, and then sends back a single response string giving them their IP before disconnecting.
The client program would use similar code that connects to the server's IP address at port 20248. You can do this with a Telnet program as explained in the article I linked.
Let me know if this makes sense. I don't know how well you know programming so I apologize if went too fast or too slow.
ASKER
Dear gis-jedi:
appreciate your feedback, I will check the code when I am a bit free, its month end and I got my hands pretty full.
I'll get back to you on this one.
appreciate your feedback, I will check the code when I am a bit free, its month end and I got my hands pretty full.
I'll get back to you on this one.
ASKER
Hi gis-jedi:
hope oyu still remember this
First of all thanks for the effort and patience
That cleared things out a bit, I think now its up to me to start making some test applications so I go through troubleshooting and stuff where the info is more comprehensive and makes more logic to my sense.
just one more thing can we use any of the ports evern the ones that are dedicated to specific use like the 8080 internet port or am I now making stories ;) (as in the code above you used 20248)
one more last question, can you give me a live case or example where you had to create a program and used sockets and ports, or anyone you know, and that would be it
thanks a lot I really appreciate it
I raised the points I hope they are good enough
thanks again appreciated.
hope oyu still remember this
First of all thanks for the effort and patience
That cleared things out a bit, I think now its up to me to start making some test applications so I go through troubleshooting and stuff where the info is more comprehensive and makes more logic to my sense.
just one more thing can we use any of the ports evern the ones that are dedicated to specific use like the 8080 internet port or am I now making stories ;) (as in the code above you used 20248)
one more last question, can you give me a live case or example where you had to create a program and used sockets and ports, or anyone you know, and that would be it
thanks a lot I really appreciate it
I raised the points I hope they are good enough
thanks again appreciated.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
thanks a lot
gis-jed
appreciate all your input
gis-jed
appreciate all your input
Port - http://en.wikipedia.org/wiki/TCP_and_UDP_port
Socket - http://en.wikipedia.org/wiki/Internet_socket