Sockets VS web request (80)

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

cualquiera
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Aug 22, 2013 6:52 am

Sockets VS web request (80)

Postby cualquiera » Fri Aug 23, 2013 12:36 am

Hello forum members!

First post here.

I am quite sure there are great members with years of knowledge that will laugh about my questions, but I didn't find anything about it. Because I have started with PHP just a few months ago, and I will develop probably for really long time, I have decided to be part of this community.

Now, about the question itself.
Note: Forum member, you probably can answer the question without reading the methodology.

Client in Java request an operation to a LAMP server.

The really sort version of the methodology: users connect to the webpage 'A', which determines which IP's are blocked, with operations can be done for every specific user, and then, if there are sockets available (ports that can be open,etc..) the user receives the port to proceed (and in the server side it is opened a socket there), in case no ports can be opened (or maybe because we don't want) I thought as alternative method, the user receives a second url (webpage 'B') to proceed with the same operations as in the socket, but thru the tcp80, and meanwhile in the socket is a normal "speech" workflow (user->socket, socket->user, user->socket,...) with the alternative method I need send new parameters to the webpage B (user->B and B answers by echo (the server stores the info exchanges in db until it receives the next step of the process from user,...).

I thought sockets were faster (no headers like HTTP GET...), but I am really surprised that it isn't. So, I have been fighting against sockets for 1 week, to understand that the alternative method is in fact the best method.

I have done some benchmarks with different types of operations from users, and as average:

All tests are done in localhost.

test A:
5 users. Every 4 seconds a different user request an operation type 1.
Time in finish the operation (since user request til server finishes the operation)
Sockets: 8 s. (and some sockets are blocked, TIME_WAIT states,... but this is maybe because of the localhost)
Webpage: 1.6 s.

Now with operation type 2.
Sockets: 4 s.
Webpage: 0.9 s.

test B:
5 users. All users request at the same time an operation type 1.
Sockets: 11 s.
Webpage: 4.5 s.

Now with operation type 2.
Sockets: 7 s.
Webpage: 3 s.


Now, the question: Why? Why I thought that sockets would be better?. Looking to the results, why I should consider in the future to try with sockets if it is so slow? What do you recommend me?

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Sockets VS web request (80)

Postby johnj » Tue Aug 27, 2013 7:58 pm

By comparing web browser with sockets you are actually comparing apples with oranges. A browser itself uses multiple socket connections to complete a task.

cualquiera
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Aug 22, 2013 6:52 am

Re: Sockets VS web request (80)

Postby cualquiera » Wed Aug 28, 2013 8:33 am

Ok, but in this case the "browser" is the usual java HTTPConnection, not really a browser.

And if I am comparing apples with oranges, when it is useful the oranges (Sockets)? I don't know if it is the best example. I don't see the advantage in this case.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Sockets VS web request (80)

Postby johnj » Thu Aug 29, 2013 10:02 pm

If you have something that is already built-in and if it is faster and useful to you then use that. In this case, you say that the so called "browser" is better. In that case, use the browser.

But, there will cases or situations where you do not have anything "ready made" and the only solution to implement it (your networked application problem) will be to write your own method from scratch. In this scenario go for socket programming. You can use any programming language for this as long as it supports the Socket API.

cualquiera
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Aug 22, 2013 6:52 am

Re: Sockets VS web request (80)

Postby cualquiera » Thu Aug 29, 2013 10:59 pm

Ok, perfect.

So, as summary, if your OS and Programming language that will be the source code of the app to communicate with from the server supports those "built-in functionality like a browser", use common http 80 requests. So, almost in every PC nowadays doesn't make sense to use sockets instead of http request if we are using php.

In other cases, probably embedded systems and so, sockets.

I remember to program Java to Java with sockets, because it was the only solution if I wanted to avoid communication through a server with other language.

Thank you.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Sockets VS web request (80)

Postby johnj » Sat Aug 31, 2013 1:03 am

Please read this book
http://www.amazon.com/Unix-Network-Prog ... 0131411551

This book may not tell you when to use socket programming but will give you a feel (trend) of what type of applications programs use sockets. But, again there are no strict rules, it is you who will decide based on the situation. For example: Even if I have a built in method that will satisfy my need but if I find that it (the method) is not reliable (as it is buggy), then I will go ahead and write my own method. In another instance I might go ahead and use an open source library even if I find that it is not reliable. So, everything depends on the situation


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron