NotionCommotion Posted December 31, 2014 Share Posted December 31, 2014 I recently came across http://forums.phpfreaks.com/topic/293466-working-with-sockets/. Sockets? Never heard of them... I looked at the PHP documentation http://php.net/manual/en/intro.sockets.php, and it tells how to implement them, but not really what their purpose is. I then search a bit, and learned about their history http://en.wikipedia.org/wiki/Berkeley_sockets, but still not really why they are used. Okay, sockets are used to connect an application to a given port and IP, and PHP can be set up as either a socket host or socket client? Is this something like a SOAP server/client, but SOAP specifies a given port and protocol? Do sockets not necessarily specify a protocol? Still a little fuzzy. Can anyone provide examples on where they might be used? BSD Sockets generally relies upon client/server architecture. For TCP communications, one host listens for incoming connection requests. When a request arrives, the server host will accept it, at which point data can be transferred between the hosts. UDP is also allowed to establish a connection, though it is not required. Data can simply be sent to or received from a host. The Sockets API makes use of two mechanisms to deliver data to the application level: ports and sockets. Ports and sockets are one of the most misunderstood concepts in sockets programming. All TCP/IP stacks have 65,536 ports for both TCP and UDP. There is a full compliment of ports for UDP (numbered 0-65535) and another full compliment, with the same numbering scheme, for TCP. The two sets do not overlap. Thus, communication over both TCP and UDP can take place on port 15 (for example) at the same time. A port is not a physical interface - it is a concept that simplifies the concept of Internet communications for humans. Upon receiving a packet, the protocol stack directs it to the specific port. If there is no application listening on that port, the packet is discarded and an error may be returned to the sender. However, applications can create sockets, which allow them to attach to a port. Once an application has created a socket and bound it to a port, data destined to that port will be delivered to the application. This is why the term socket is used - it is the connection mechanism between the outside world (the ports) and the application. A common misunderstanding is that sockets-based systems can only communicate with other sockets-based systems. This is not true. TCP/IP or UDP/IP communications are handled at the port level - the underlying protocols do not care what mechanisms exist above the port. Any Internet host can communicate with any other, be it Berkeley Sockets, WinSock, or anything else. "Sockets" is just an API that allows the programmer to access Internet functionality - it does not modify the manner in which communications occur. Reference: http://wiki.treck.com/Introduction_to_BSD_Sockets Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/ Share on other sites More sharing options...
requinix Posted December 31, 2014 Share Posted December 31, 2014 HTTP, right? It's how the web works: I send a request to you with what I want and you send a response back. Email? SMTP, more or less. Less transactional and more conversational than HTTP where I connect to your SMTP server and we exchange information, including the email content itself. DHCP and DNS? Packets fly around the network with tidbits of information, to and from the various computers and routers connected to it. All that happens over sockets. "BSD sockets" are basically the API to the operating system's TCP/IP/etc. implementation, and it's so common even Windows supports it. Okay, sockets are used to connect an application to a given port and IP,Right. "IP" isn't so much an IP address as it is an interface. For example, you can bind to the IP address corresponding to your wired Ethernet port, or to the IP address used by your wireless card, or to localhost. Or to all of them indiscriminately. You can't bind to someone else's IP address and magically receive their traffic. and PHP can be set up as either a socket host or socket client?As PHP exposes a socket API to you, yes: you can connect to anything you want at any time and as long as your script is still running you can let anybody connect to you (except on ports Is this something like a SOAP server/client, but SOAP specifies a given port and protocol?SOAP is a protocol in its own right but it isn't the full story. It piggybacks on another protocol, like HTTP: regular web stuff except the request and response bodies are XML with a defined schema. I could write the XML down on a piece of paper and give it to you and that would still be SOAP, with the "another protocol" being reading and writing. Do sockets not necessarily specify a protocol?Correct. They're just connections that enable you to send and receive data. The format and structure of that data is up to you and whatever you're communicating with. Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501366 Share on other sites More sharing options...
NotionCommotion Posted December 31, 2014 Author Share Posted December 31, 2014 Thanks Requinix, Your explanation was helpful. But why would I want to use the explicit PHP socket functions? I would assume any standard communication interfaces which PHP supports implements sockets under the hood using a lower level language. I could see using these functions if implementing a propitiatory interface or some interface which PHP doesn't natively support. Are these the only reasons, or are there others? And of course wishing all a happy New Year! Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501390 Share on other sites More sharing options...
requinix Posted January 1, 2015 Share Posted January 1, 2015 You do realize you just asked "is there any reason to use these things besides when I'm forced to use them"? You could do a lot of stuff with the raw sockets but yes, when possible it's better to stick with the more abstracted, higher-level approaches: cURL, FTP functions, etc. Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501394 Share on other sites More sharing options...
NotionCommotion Posted January 1, 2015 Author Share Posted January 1, 2015 While not in those exact words, yes, that is exactly what I asked. My uncertainty is what circumstances would I be forced to use them. Without writing a book, could you give a couple good examples of "lots of stuff"? I don't think I have any immediate needs, but without knowing what is possible, don't know for sure. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501397 Share on other sites More sharing options...
kicken Posted January 1, 2015 Share Posted January 1, 2015 If you need to interact with some service PHP doesn't already have an extension for. Such as: - VNC Server - Game Server - IRC Server - etc If you need to write your own server. Such as: - Websocket Server - Game server - Data collection server - etc There are many reasons why you might need to use sockets. Basically any time you want to have two computers share information, you'll be using sockets. The question is whether you can make use of an already established protocol/service (for example a lot of stuff uses HTTP) or if you'll have to write your own custom protocol (for example games will do this to maximize efficiency). If you can't piggyback onto an existing protocol and use existing extensions, then you fall back to generic sockets. Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501405 Share on other sites More sharing options...
NotionCommotion Posted January 2, 2015 Author Share Posted January 2, 2015 Thanks kicken, Your examples helped. If I understand things correctly, PHP is being used to implement a protocol? When would one elect to build some lower level language driver (sorry that is probably a windows term) to bridge protocols? Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501502 Share on other sites More sharing options...
requinix Posted January 2, 2015 Share Posted January 2, 2015 If I understand things correctly, PHP is being used to implement a protocol?Not "implement". The operating system has already implemented all that. BSD sockets are how an application developer accesses that implementation. The PHP folks took that, made their own similar API, and exposed that to PHP developers. So when you use PHP's socket functions you invoke PHP's own socket implementation which then invokes the OS's socket implementation. When would one elect to build some lower level language driver (sorry that is probably a windows term) to bridge protocols?Depends what you mean? The lowest level is in the operating system and you don't do that. Next up is typical compilable-language code, like C/C++, which lots of people do. If you make anything there then it would be, like, to abstract out some of the work (eg, bind and listen at once) or to be cross-platform compatible (eg, use winsocks on Windows or BSD on Linux). Quote Link to comment https://forums.phpfreaks.com/topic/293565-what-are-bsd-sockets-used-for/#findComment-1501508 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.