NotionCommotion Posted October 30, 2015 Share Posted October 30, 2015 I have two server/clients. Server B initiates communication to Server A. Afterwards, both servers need to be able to initiate communication to one another. The router in front of Server B should not require any special configuration such as port forwarding. Is this possible using PHP? I recognize maybe PHP is maybe not the preferred language, but wish to use it as proof of concept. If possible, please provide very high level steps on how it can be accomplished. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/ Share on other sites More sharing options...
benanamen Posted October 30, 2015 Share Posted October 30, 2015 (edited) Server B initiates communication to Server A You need to define what 'kind' of communication. Edited October 30, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524752 Share on other sites More sharing options...
scootstah Posted October 30, 2015 Share Posted October 30, 2015 This is possible with PHP, yes. You don't need to use WebSockets though unless you plan on having a web browser client. You could just use a regular TCP/UDP socket instead. The firewall would need to allow communication on the selected port. Start with the manual. Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524753 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2015 Author Share Posted October 30, 2015 (edited) This is possible with PHP, yes. You don't need to use WebSockets though unless you plan on having a web browser client. You could just use a regular TCP/UDP socket instead. The firewall would need to allow communication on the selected port. Start with the manual. Thank you Scootstah, No, there is no web browser client, and I will check sockets out. Something I've always planned on doing, but never really did in earnest. Note that assuming the firewall is configured to allow communication on standard ports such as 80 or 443 is acceptable, but setting up port forwarding to a given local IP is not. It sounds like I am okay in this regard, but if you feel differently, please advise. Thanks again! EDIT. Will some network configurations prevent this? Somehow Server A needs to get back to Server B, and without port forwarding or equal, the local IP needs to be somehow bundled in the response. Edited October 30, 2015 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524755 Share on other sites More sharing options...
scootstah Posted October 30, 2015 Share Posted October 30, 2015 You can't use ports 80 or 443 if you have a webserver installed. You can't bind to the same port. If you can't open ports then you can't use sockets. Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524757 Share on other sites More sharing options...
0x00 Posted October 30, 2015 Share Posted October 30, 2015 Your questions are so open its hard to tell you anything without a million clauses...The router will need either port forwarding if its your own, else server B needs to be in the DMZ (basically same thing if I remember right). Is the router your home router or an actual internet gateway? The bit no-one has mentioned is that server B has a dynamically assigned IP, is that on the internal network, or is that the router itself or does that have a static IP? If so, then server B needs to start the communication to server A's static IP, or it needs to indicate somewhere what its IP is for server A to contact it.If this is all using web servers, then if a dynamically assigned IP client server (i.e. previously unknown) contacts the host server, then the following may be used to get it's details (note that some of these may be empty, spoofed or dangerous): $remote_addr=@$_SERVER['REMOTE_ADDR']; $remote_host=@$_SERVER['REMOTE_HOST']; if($remote_host==""){$remote_host = gethostbyaddr($remote_addr);} $refer=@$_SERVER['HTTP_REFERER']; Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524759 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2015 Author Share Posted October 30, 2015 You can't use ports 80 or 443 if you have a webserver installed. You can't bind to the same port. If you can't open ports then you can't use sockets. But websockets uses port 80/443 by default, no? How does it accomplish it? Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524762 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2015 Author Share Posted October 30, 2015 (edited) The router will need either port forwarding if its your own, else server B needs to be in the DMZ (basically same thing if I remember right). Is the router your home router or an actual internet gateway? The bit no-one has mentioned is that server B has a dynamically assigned IP, is that on the internal network, or is that the router itself or does that have a static IP? If so, then server B needs to start the communication to server A's static IP, or it needs to indicate somewhere what its IP is for server A to contact it. Don't want port forwarding or a DMZ. For arguments sake, lets say Server B is located behind my home router, and my router has a semi-static public IP as defined by my IP provider. If I am using a browser behind my home router, it is my understanding that I could initiate a websocket to a server, and later receive pushes from that server. If a browser could do it, why could it not be done without one? Edited October 30, 2015 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524763 Share on other sites More sharing options...
0x00 Posted October 30, 2015 Share Posted October 30, 2015 Don't want port forwarding or a DMZ. For arguments sake, lets say Server B is located behind my home router, and my router has a semi-static public IP as defined by my IP provider. If I am using a browser behind my home router, it is my understanding that I could initiate a websocket to a server, and later receive pushes from that server. If a browser could do it, why could it not be done without one? In this case then server B would have to initiate the connections. Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524766 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2015 Author Share Posted October 30, 2015 In this case then server B would have to initiate the connections. Yes, I totally agree. I tried to make that clear in the original post. Assuming that server B initiated the connection, will http://php.net/manual/en/book.sockets.php work? My initial review indicated that they are not sophisticated enough to pass header information to return to originating client. If not, how can this be accomplished? Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524768 Share on other sites More sharing options...
0x00 Posted October 30, 2015 Share Posted October 30, 2015 Yes, I totally agree. I tried to make that clear in the original post. Assuming that server B initiated the connection, will http://php.net/manual/en/book.sockets.php work? My initial review indicated that they are not sophisticated enough to pass header information to return to originating client. If not, how can this be accomplished? All networks are based on sockets, e.g. Berkley Sockets, WinSock, etc... IMHO sockets are too low level for you (but then again I have no idea at what you're doing). Personally when web programming I've never had to use / manage them, I only go that low when using C or C++, etc... My main point is, if you're communicating with a web server then all the sockets are already managed for you and you just use functions like file_get_contents() or maybe cURL, etc. Apache and NGINX, setup pools of threads that manage sockets, they then pass the data to the PHP parser (sort of), which is technically the application (even though the server can be thought of as the application when considering the OSI layers.) Soooo... what are you wanting to do, then someone can suggest the ideal solution, e.g. maybe things like SOAP or REST for different types of API demands, etc... 1 Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524773 Share on other sites More sharing options...
benanamen Posted October 30, 2015 Share Posted October 30, 2015 (edited) I have no idea at what you're doing That comment pretty much sums up this thread so far. Why don't you tell us exactly what you are trying to accomplish rather than asking about how to do what you think needs to be done Edited October 30, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524775 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2015 Author Share Posted October 30, 2015 Thanks 0X00, Sorry for being cryptic, but I am not allowed to communicate the application. What I heard you say is "yes, this is possible, but not really with PHP and will require some lower level language such as C, etc." If I misread you, please advise. PS. I like your comment "All networks are based on sockets, e.g. Berkley Sockets, WinSock, etc..." It confirms what I somewhat already suspected. Thanks again, Michael Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524780 Share on other sites More sharing options...
0x00 Posted October 30, 2015 Share Posted October 30, 2015 Thanks 0X00, Sorry for being cryptic, but I am not allowed to communicate the application. What I heard you say is "yes, this is possible, but not really with PHP and will require some lower level language such as C, etc." If I misread you, please advise. PS. I like your comment "All networks are based on sockets, e.g. Berkley Sockets, WinSock, etc..." It confirms what I somewhat already suspected. Thanks again, Michael I don't use raw sockets much anymore because there are so many libraries which handle them for you. Setting up sockets is very fiddly and thats before handling all the possible signals, threads and security concerns. That's why we now use web servers for many things, it works out the box, but it may not be right for your circumstance, but there is probably a library, framework or program which will be. I'm not asking for your million pound idea, but what it does and its constraints would help, well not me but you. Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524781 Share on other sites More sharing options...
scootstah Posted October 30, 2015 Share Posted October 30, 2015 But websockets uses port 80/443 by default, no? How does it accomplish it? No, they use whatever port the daemon is listening on. It's not possible to use sockets without an open port to listen on. Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524786 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2015 Author Share Posted October 30, 2015 You can't use ports 80 or 443 if you have a webserver installed. You can't bind to the same port. If you can't open ports then you can't use sockets. No, they use whatever port the daemon is listening on. It's not possible to use sockets without an open port to listen on. Agree a port needs to be open to hear through that port. Doesn't websockets by default use 80 and 443? I don't claim to know, and base this on https://en.wikipedia.org/wiki/WebSocket. If so, could a server that responds to websockets not act as a webserver? Needless to say, I am very new to sockets! Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524794 Share on other sites More sharing options...
0x00 Posted October 30, 2015 Share Posted October 30, 2015 Do you need a persistent connection? Most web servers weren't designed to handle persistent connections, they may even have a limited thread pool to work with (i.e. a max number of concurrent connections). Most realtime games use persistent tcp connections, and many games baulk out at 20 or so concurrent connections, yes there are ways to handle more, but, well... another story! Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524797 Share on other sites More sharing options...
scootstah Posted October 30, 2015 Share Posted October 30, 2015 Doesn't websockets by default use 80 and 443? I don't claim to know, and base this on https://en.wikipedia.org/wiki/WebSocket. No, websockets do not default to anything. The protocol does not mention implementation details such as port numbers. If so, could a server that responds to websockets not act as a webserver? Sure, you could bind your socket daemon to listen on port 80 or 443. But then you can't run a webserver like Apache. Quote Link to comment https://forums.phpfreaks.com/topic/298919-php-and-websockets/#findComment-1524816 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.