Socket multi-user connection Help

Source: Internet
Author: User
Tags php write try catch
Little brother is doing a flash for the client, PHP for the server socket applet

Now there is a problem with flash successfully connected to the server after the end! Everything can communicate normally.

But not many users to connect to the server (such as I open two flash, the second will not connect)

Can I ask if the PHP socket will solve the problem of multi-user connection


Looking for a master to help


Reply to discussion (solution)

No one has ever developed anything like that?

Do you have this PHP program, which is the normal processing response HTTP request? Or is it really a socket server?

If it is the latter, how is your PHP program started?

You have to make this clear before you talk about it.

is to be true. Socket response
Flash keeps the connection with PHP

Php
can be used
Set_time_limit (0);
Ignore_user_abort ();
To be executed forever

But it's good to write in Java.
But I'm not java.

You answered only the first of my two questions. The second question, how does your PHP start?

If you start by manually sending an HTTP request, then you need to use a fixed port in PHP to receive and process multiple socket connection requests, which requires a certain skill.

If this HTTP request is initiated by FLASH, you can use a different port in PHP to receive the socket connection.

Anyway, this way of running the PHP program is not suitable for the socket server.

Good advice

Use Flash to start the socket server

But I'm not good at it. Run time after startup (if running permanently, the server will be expensive until it is finally down)
And when a new user comes in, I need to proactively send messages to other previously logged-in users, but not very well.

To answer your previous questions.
PHP boot is to enter the game before the first use of PHP connection to the server if you can not connect on the request (I thought before)

Indeed, each flash itself requests to launch a PHP to do the socket server, only for this flash alone service, it is difficult to achieve the interoperability of multiple sessions.

Do not understand your actual application background, so it is unclear why you must use the socket, if your communication can return to the normal HTTP, it should be the simplest.

Be sure to use the socket, it is recommended that you consider a different way. Not to use Java, that is, to use Java, if it is a web-style deployment, the problem is the same. PHP can also be done in the CLI way.

Through the HTTP connection, is through the iis,apache, he door has very good processing multi-user's ability,

So it is possible to use WebService, that is, to use Iis,apache,

So the textbook on the internet says that Flash is all about using WebService,

Or if you do a bad server, you're going to be upside down.

The main thing is to do a flash game, based on the web, to be able to play several people in a room

To be able to instantly notify individual users of the situation of other users, it is necessary to use a socket to achieve the purpose of instant communication

The CLI has not been used, can you give me an example?
I want to know how he works, what he can do.

Your application scenario, the key is to "instant notification", right?

Using a socket can, of course, accomplish the goal, but it's not easy to implement, at least in a web container.

In fact, with an HTTP long connection can also achieve the same effect, such as you visit the net, there will always be an incomplete HTTP request, up to 30 seconds it would end, but will immediately initiate a request, which can achieve "instant notification." Related data can be searched for "Comet long Connection".

(Please note that the above 30 seconds, not to say that each notification to wait 30 seconds.) If there is no notification, a new connection will be used for more than 30 seconds, but will be returned immediately if notified)

CLI is just another way to start a PHP program, the equivalent of running an EXE on the server, but this exe is written in PHP. Because there is no web container restrictions, for multi-threaded, network communications and other functions used more freely.

Php
can be used
Set_time_limit (0);
Ignore_user_abort ();
To be executed forever

But it's good to write in Java.
But I'm not java.
Why is there Ignore_user_abort (), the PHP socket server to execute under the command line, which is the CLI above, not set as an HTTP response end.
http://devzone.zend.com/article/1086

Thanks, Maquan.

Does PHP write an. exe suffix executable file?

Or is the file also. php just started to change the hairstyle?

The http://devzone.zend.com/article/1086 above is the same as the Socket_select of the handbook.

I'll try again.

Thank you for your help!

My little brother learned a lot of knowledge and solutions that I didn't know before!

Thanks again

CLI is just another way to start a PHP program, the equivalent of running an EXE on the server, but this exe is written in PHP. Because there is no web container restrictions, for multi-threaded, network communications and other functions used more freely.


Can I use CLI to run without a thread limit?
Is it equal to the code I wrote before running directly on it?

Thank you very much for being able to reach multiplayer online.

It feels real good, too.

Thank you, Maquan and Boojs.

Can I continue chatting after the knot is pasted?

Still want to talk to you guys?

The knot is not anxious, as long as it is related to this topic, although continue to talk, hehe

However, if it is other problems, it is best to open another post, so it is convenient for everyone to read. CSDN is a public place:)

Uploaded to space discovery yesterday with the CLI running with a limit of 30 seconds to timeout!

Then we have to write a program that guards him. Re-run the CLI once the discovery times out

Is it possible to run the catch when the try catch is monitored to timeout?
try{
My socket Code
}
catch () {
Re-request again
}

How do I write in the catch brackets?

Do not understand the space, do not know what management restrictions they have. PHP's own run time limit can be set by Set_time_limit (), which is 30s by default.

The way to reload with the daemon is not necessarily a good solution to your problem, it will make your server performance is very unstable, client development more difficult.

If you are renting space, there will be a lot of restrictions on the ultra-conventional program, it is strongly recommended that you carefully understand the long connection.

Okay thank you

Space is really set for 30 seconds timeout
I'll look at the long connection first!

This is a much smaller requirement for the environment.

A day of understanding about what is a comet long connection!

It's mostly written on the PHP side.
Flush ();
Flush buffers to let the foreground Ajax receive messages

And the PHP side doesn't stop working.

When the 30-second timeout occurs, the client then re-connects again

Do not know if I understand the correct?

You're right, that's basically what it means.

But you may be too concerned with the details of the implementation. Conceptually, the so-called long connection, that is, after the server receives an HTTP request, does not complete it immediately, but to maintain a period of time (such as 30 seconds), if there is a need for instant delivery of the notification, immediately output the response content and close the connection. If it is safe, it is time to return (preferably not to be killed by the container), which gives the client a complete response, which allows the client to continue the work below.

One of the difficulties in implementing long connections is how to be aware of "notifications requiring immediate delivery" in the process of waiting, especially in web containers where space is rented, which can be very limited, simple polling and delay is very inefficient, preferably some kind of blocking mode of IO.


????????????????????????????????
Based on the plugin extension function provided by CSDN forum, I have made a signature file tool to share with you, welcome technical exchange:)

It's a bit of a hassle to know what new content needs to be returned in the process of waiting.
Bad things can affect immediacy.

I think so, if the content needs to be returned by reading the XML or PHP array created by another page (that is, reading the write file), when the connection is initiated, record the current modification time of the file, and then return the contents of the file when the modification time changes.
Do not know if this will not occur some problems, such as the modification and connection at the same time (I connected the first second, the file was modified, so that only the next time the file changes will return information, the probability of such a situation does not know how much)

I don't quite understand the IO operation you're referring to.



I have written a small example of a socket. In my blog, you can go and see.
http://blog.csdn.net/a82168506/article/details/6307461

Thank you for the brother upstairs, the socket I wrote the code and your similar

The problem is the space force set 30 seconds timeout (set_time_limit (0) No use), so there is no way to see if the long connection can be solved!

Learned a bit about IO
Discover that my long connection seems to be blocking IO!

If you want to do non-blocking IO, where is the technique?

Tests have found that Ajax-initiated long connections are non-blocking IO

Only the browser is limited to long connections, when the same browsing from the two long connection, there is one is unable to connect!

Notify customers immediately if new messages are implemented

I think of the following methods
Or a way to read a file.
The first time the page is opened, the connection is initiated by JS and the current modification times of the file to be read are transmitted.

Notifies the client of new messages when it finds that the modification time and the transmitted time are inconsistent when the PHP page is reached
PHP page run to 25 seconds to return the last modification time of a file to JS, this long connection ends

JS received PHP back to the file modification time to re-initiate the connection and transfer of PHP back to the file modification time

I'm sure that's not going to miss the information that needs to be notified to the client.

That's one of my personal thoughts.

Please Maquan and everybody help me again, share your handling method

Thank you

......
Notifies the client of new messages when it finds that the modification time and the transmitted time are inconsistent when the PHP page is reached
PHP page run to 25 seconds to return the last modification time of a file to JS, this long connection ends
......
In the time between red and blue two actions, what is the program doing? If the "event has occurred" at the time of the first 10 seconds, can it be notified in time?

Two actions between the PHP page sleep (1) (return the new message after the connection does not end), and then check the file modification time until the run time of more than 25 seconds to end the connection, and then by JS to initiate a new connection

That should be fine.

If it was you, what would you do? How can I tell if a new message needs to be returned?

When I have time, I'll get the flash.

First, "The connection does not end after returning the new message" This is probably problematic, only if the end of the connection, the client will think "has been fully accepted an HTTP Response", will be further processed. If you do not end the connection, even if you do flush and so on, most of the data is only passed to the client, but the client has not finished receiving, start processing, that is, you this notification is not "instant."

As for the "Check for notification" method, you should use a "poll + delay" method, generally available, but "instant" and "running efficiency" a little bit worse. I always feel that the "block IO" way may be better, you can refer to Socket_recvfrom (), the disadvantage is that the resource occupies a bit more.

Rented space, no way, subject to the Web container. If it is their own independent server, make a deamon, with multiple asynchronous IO, want to play on how to play, hehe


????????????????????????????????
Based on the plugin extension function provided by CSDN forum, I have made a signature file tool to share with you, welcome technical exchange:)

Http://www.zeitoun.net/articles/comet_and_php/start
I found this example.

He uses a JS library to achieve a long connection and the content will return normally, the connection does not stop!

Everybody, look.

I'm not familiar with this library, do you have any familiar friends?

Is this the real comet?

Find data in multiple parties
Found to be a very small reference

My current level may be the only way to write.

JS initiates a long connection

PHP page If there is new information immediately return to JS and end the connection, JS received new information (processing), immediately initiate a new connection (before data processing)

If there is no new information for more than 25 seconds, PHP ends the connection, JS also initiates a new connection again!

With Socket_recvfrom () I have to use the CLI to run a service side, so that is blocking IO?
It seems that JS can also be used as a socket client connection server, if more than one client to connect at the same time, there is a blocking, do not know the client's originating connection will time out? Will it automatically connect when the socket server disconnects the previous connection?

In theory, if the client is a lot of situations, real-time is probably not guaranteed

I seem to understand the mistake above, and found a most popular explanation.

For example, process A to receive a packet from the network, if there is no package to wait, this is blocking.
Do not wait, to do other things, wait for the package to deal with is not blocked.

If you look at this,

JS initiates a long connection
PHP page If there is new information immediately return to JS and end the connection, JS received new information (processing), immediately initiate a new connection (before data processing)
If there is no new information for more than 25 seconds, PHP ends the connection, JS also initiates a new connection again!
is actually a blocking IO, when there is no new information, JS is always processing wait state, until there is new information

In a communication of two paragraphs, read operations and write operations, they are used respectively is "blocking" or "non-blocking", are said each, and do not affect each other.

For example, your server code: Wait for notifications if you use Socket_recvfrom (), which is "blocking", and output HTTP Response is generally "blocked". And in the JS side to receive HTTP Response, since you use AJAX, that must be "non-blocking", this is the value of Ajax, otherwise the page will become blank screen.
With Socket_recvfrom () I have to use the CLI to run a service side, so that is blocking IO?
That's not true. The PHP that is responsible for the long connection uses socket_recvfrom () to wait for the notification data to be in the "blocking state", and the notification data must be another regular PHP program, such as "a user login", which is responsible for socket_sendto ().

In theory, if the client is a lot of situations, real-time is probably not guaranteed
What you need to worry about is not "real-time", and if the communication model described above succeeds, real-time is always guaranteed (depending on the speed of the network, of course). The problem is that when the number of clients is large, the resource usage is larger, including:

• Each client will continue to consume a PHP long connection process resource;
• Each long connection process consumes a socket resource;

In general, confined to web containers, this pattern is not suitable for large-scale applications. For example, the space service provider limits the number of HTTP concurrent connections to your website, and that long connection may bring down your entire site. It's better to return to the "client-side periodic polling" mode, and lose a bit of "real-time", in exchange for "online users."

I used your method to try and find a problem

Only one socket can be created per port
If I use a random port, Socket_sendto won't know what ports are being sent.

Do I need to maintain a current online port table? Is it better to save to a database, or save it as a file mode?

Use UDP broadcast, the specific use you check the information.

Well, thank you so much for your patience.
I'll check the information first.

Maquan
Broadcast I've been tested successfully

But the broadcast still needs to set the port

I put my code on it.

JS Request for PHP

Header ("Cache-control:no-cache, Must-revalidate"), Header ("Expires:mon, Jul 1997 05:00:00 GMT"), Include (' Config.php ');//This is $add (IP) and $port$socket = Socket_create (Af_inet, Sock_dgram, SOL_UDP); Socket_bind ($socket, $add, $ Port);//I try to write $port as 0 but if this creation fails $port = 0;socket_recvfrom ($socket, $buf, 10240, 0, $from, $port); echo "Received $buf from Remote address $from and remote port $port ". Php_eol;socket_close ($socket);


Broadcast of PHP
Include (' config.php '); $sock = Socket_create (Af_inet, Sock_dgram, SOL_UDP); Socket_set_option ($sock, Sol_socket, So_ Broadcast, 1); $msg = rand (1000,9999), $len = strlen ($msg), Socket_sendto ($sock, $msg, $len, 0, ' 255.255.255.255 ', $port);//Here's $ The port will be the same as the above file $port the above file to receive the broadcast, but different customers connect after the ports must not be the same to successfully create the socket connection, so how to write to notify all the ports under the IP users? This is to notify all users of Port $port socket_close ($sock);


How do I write to notify all ports?

Write the socket.accept in the while (1).

Accept is to listen to whether the user connected it

I don't need a user connection. sendto based on IP and Port sent to online users

Try again socket_set_option ($sock, Sol_socket, SO_REUSEADDR, 1);

Or not?
If the port parameter of the Socket_sendto function cannot be empty, the receiving end cannot receive the information, that is, it cannot send the message to all the ports on the line.

Socket_set_option ($sock, Sol_socket, SO_REUSEADDR, 1); It's supposed to be written on the receiving end, but it doesn't work.

Can you write an example for me?

Last night, a whim, wrote a UDP broadcast communication program, just for your reference:)

recvfrom.php

   

sendto.php
    

The above two PHP programs, recvfrom.php can use multiple browser windows to send the request, will be in a waiting state, and then send a sendto.php request in a browser window, the previous several browser windows to get the results at the same time.


????????????????????????????????
Based on the plugin extension function provided by CSDN forum, I have made a signature file tool to share with you, welcome technical exchange:)

Thank you so much
has been tested successfully
Now go back to see SO_REUSEADDR's explanation reports whether local addresses can be reused.
It dawned, "can I reuse local addresses"
After setting this option, you will no longer have bind when you remind an IP that a port can only be created with a connection

Just test found
RECVFROM.PHPL, there's no need to add this to success.
Socket_set_option ($socket, Sol_socket, So_broadcast, 1);

I think it can be deleted because the broadcast page is sendto.php,recvfrom.php only need to re-use the local address and port on the line

Is that right?

I'm not quite sure about that either.

The obvious difference between a UDP broadcast packet and a normal UDP packet is that the destination IP address is different. As a "Listen UDP packet" socket, it is physically possible to "perceive" the broadcast and non-broadcast UDP packets, but it will not "receive" broadcast packets it? If you've set this socket to "broadcast", it's sure to be fine, but what if it's not set? Will it refuse?

I think this is not good to say, may be related to the implementation of the specific system. Although the actual use can not write, but I always feel that it is more safe to write.

It makes sense.
Calculate the broadcast address of the outside network test

The broadcast address must have a subnet mask to count.

You have to ask the service provider, only IP seems to count.

I don't know if I understand right, broadcast (broadcast) is limited to LAN, which is a network segment. To go out of the network, that is, multicast (multicast), the need for router coordination.

I do this is actually a different IP port to broadcast it

It's supposed to be possible.

If you can't do that, you can only maintain a user-used port table.

Tried it, no.
Help me see if the broadcast address is wrong.


IP Address: 123.183.215.45
Subnet Mask: 255.255.255.128
Default gateway: 123.183.215.1

I figure it out.
123.183.215.127

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.