Jump to content

what is a socket connection?


skaterdav85

Recommended Posts

I am reading a book and one of the exercises is to build a versatile remote file connector. The last method of attempt to retrieve the contents of a remote file is through a socket connection. However, the book doesn't really explain what a socket connection is. It just says that it is the least freindly way of accessing a remote file. Can anyone explain simply what a socket connection is?

Link to comment
Share on other sites

Basically, sockets are like ports. A web server listens on port 80, so it has a socket open on port 80 waiting for connections. A web browser also opens a socket on port 80 and uses it to connect with the web server. A socket is like a connection (or impending connection) where a port is like an address or location for the connection.

Link to comment
Share on other sites

I don't mean to hijack the thread, but is it possible to create an SSL connection with PHP? If just an email address and password are being handled (and nothing any more confidential inside their account), would it be necessary to create a secure connection?

Link to comment
Share on other sites

I read up on sockets today in my net+ book. It said that the port differs from the socket as a socket is attached to an ip address. ie: 192.168.1.1:80

Link to comment
Share on other sites

  • 8 months later...
Right, the socket is like the actual connection, the port is the place where you connect to.
Question ONE: Calls to the fsockopen() and pfsockopen() using a host name and port return numbered resources. Is the word socket just another name for a resource used to establish a connection over the internet?Question TWO: Both of the aforementioned functions require a hostname and port as parameters. Moreover, both functions appear restricted to the TCP and UDP protocols. Is there anything else that makes a socket different from other methods of establishing a connection over the internet? For example, must the ports of the client and the server be the same?Roddy, the Gravedigger
Link to comment
Share on other sites

Calls to the fsockopen() and pfsockopen() using a host name and port return numbered resources. Is the word socket just another name for a resource used to establish a connection over the internet?
A "resource" is a generic term used in PHP to describe any data structure which doesn't fit into another category. Resources include things like an open connection to a MySQL database, an open file handle, etc. When you try to print a resource it always prints the word "Resource" followed by a sequential number. If you have 5 resources in a PHP script, they will be numbered 1 through 5 in the order they were opened (or 0 through 4).With regard to your other question, it seems like PHP is geared towards web server connections, specifically TCP/UDP. You may be able to open a generic stream using the stream functions and wrappers. To my knowledge the server and client ports don't need to be the same, in fact they usually aren't. When you connect using fsockopen you only specify the server port, not the client port. The client typically uses any available port. Since internet communications start with the client sending a request to the server it's not necessary for the server to know how to contact the client. Only the client needs to know how to contact the server, the server just sends a response that uses the connection opened by the client. The server doesn't need to open its own connection to the client to send the response.
Link to comment
Share on other sites

A "resource" is a generic term used in PHP to describe any data structure which doesn't fit into another category. Resources include things like an open connection to a MySQL database, an open file handle, etc. When you try to print a resource it always prints the word "Resource" followed by a sequential number. If you have 5 resources in a PHP script, they will be numbered 1 through 5 in the order they were opened (or 0 through 4).
I did not ask what a resource was, I asked whether a socket is a resource with a specific purpose. Perhaps this clarification will help you to provide a more enlightening response to my question.
When you connect using fsockopen you only specify the server port, not the client port. The client typically uses any available port. Since internet communications start with the client sending a request to the server it's not necessary for the server to know how to contact the client. Only the client needs to know how to contact the server, the server just sends a response that uses the connection opened by the client. The server doesn't need to open its own connection to the client to send the response.
This was very helpful. Thank you.Roddy, The Resurrecting
Link to comment
Share on other sites

I did not ask what a resource was, I asked whether a socket is a resource with a specific purpose. Perhaps this clarification will help you to provide a more enlightening response to my question.
:)
Link to comment
Share on other sites

I did not ask what a resource was, I asked whether a socket is a resource with a specific purpose. Perhaps this clarification will help you to provide a more enlightening response to my question.
Every resource has a specific purpose. A resource is just an abstract term for them, similar to an object in Javascript. In other words, there's no such thing as a resource without a specific purpose. You can't create just an abstract resource object that doesn't do anything. In Javascript you can create an empty object, but that's not the case with a resource in PHP.
Link to comment
Share on other sites

Every resource has a specific purpose. A resource is just an abstract term for them, similar to an object in Javascript. In other words, there's no such thing as a resource without a specific purpose. You can't create just an abstract resource object that doesn't do anything. In Javascript you can create an empty object, but that's not the case with a resource in PHP.
So from this discussion might we then conclude that a socket is a resource? If so, what exactly does it do?For example, does it receive a signal and assign it to a specific port? Does it have the ability to reject an improper signal? Roddy
Link to comment
Share on other sites

So from this discussion might we then conclude that a socket is a resource?
In PHP, a socket connection is represented using a resource. I'm not sure if that's what you meant.
If so, what exactly does it do?
Once connected, it provides a stream for writing data to the connection or reading data from it.
For example, does it receive a signal and assign it to a specific port?
I'm not sure what you mean. You can set up a server which listens for connections, or you can set up a client which makes a connection to a server.http://www.php.net/manual/en/function.stre...cket-server.phphttp://www.php.net/manual/en/function.stre...cket-client.php
Does it have the ability to reject an improper signal?
I'm not sure what you mean by "reject" or "improper". It's up to the server to decide what to do with an incoming connection. It can get the data and process it and, if the data is not in an expected format, I guess the server can just ignore it and close the connection. It's probably more useful to send an error response to the client though.
Link to comment
Share on other sites

Does it have the ability to reject an improper signal?
Looking at a socket as a connection between two end points; you can send close requests and nonsense signals to a socket but its up to the receiving endpoint to decide what to do with the signal. The socket itself is like a bridge. The endpoints are programs, processes etc that respond to these signals.So as a concept no, a socket doesn't have that ability but a typical Socket Class in a programming language will have methods to close, refuse data etc however the class/routines are not Sockets, they just create one and let you manage it.
Link to comment
Share on other sites

The outlet is the port and open socket on the server, and the plug is the socket on the client. The toaster is just a toaster.
what if i want to make waffles ?:)ok, seriously;
so would this be a comparable analogy... socket is like the plug to an outlet? lol
i'm not using sockets yet, (it's not winter where i am, so i don't even need socks...) but i believe a good analogy is;"port" is the port (you know, for ships, those huge floating things you see out at sea) , "socket" is the actual dock (where the ship actually berths... no, not at the maternity ward... :) ) , or;if "socket" is the plug hole (ie. socket(!!) ) then, the "port" is the power-strip.
Link to comment
Share on other sites

what if i want to make waffles ?:)ok, seriously;i'm not using sockets yet, (it's not winter where i am, so i don't even need socks...) but i believe a good analogy is;"port" is the port (you know, for ships, those huge floating things you see out at sea) , "socket" is the actual dock (where the ship actually berths... no, not at the maternity ward... :) ) , or;if "socket" is the plug hole (ie. socket(!!) ) then, the "port" is the power-strip.
Well you seem to be enjoying this more than anyone else. :P
Link to comment
Share on other sites

Well you seem to be enjoying this more than anyone else. :)
indeed i am, the best time to give newbie advice is when you're still one, or at least almost about to progress to "the next level". that way you still have the perspective of one, and able to describe it for one.the danger of course is imperfect advice (being a newbie), but since this is a well-moderated forum, i'm sure the seniors will step in correct any false statements that i do make.i like your sig, and thanks for that link to Firebug, i think i'm ready to progress from my trusty "Notepad" - the more complicated the script, the more tools you need to help with the basic syntax errors - and i'm starting to spend more time correcting missing "curlies' and "semi-colons" !
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...