ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
<?php
function giveMeAnArray($delimiter, $string)
{
return explode($delimiter, $string);
}
$read_write = "read";
if($read_write == "read")
{
//Initialize the socket
set_time_limit(50000);
$address = "127.0.0.1";
$port = 10119;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $address, $port) or die ('Could not bind to address');
//Start listening on the socket
//There could be a 'while true' or something here to make it infinite
//While(true)
// {
socket_listen($sock);
//Accept an incoming connection
$client = socket_accept($sock);
While (true) {
//Read whatever was just sent, 1024 bytes' worth. Make this however long you need.
$global_string = socket_read($client, 1024);
I was under the impression the the commented out while loop would allow me to recive multiple messages from my server but instead it sends my program into a not responding state. My client is setup to send a message every second in the form of a string. I need to be able to accept and print out these strings every second replacing the previously sent strings. Any suggestions on what I am doing wrong here and what I can do to fix it?
What does the client do with the socket connection after it sends the first string? Does it close out the socket and reopen a new one the next time it needs to send something? If so, then your server needs to be able to handle disconnects and reconnects.
'set the server to connect to
'Winsock1(0).RemoteHost = Winsock1(0).LocalIP
' 127.0.0.1 is always your computer
Winsock1(0).RemoteHost = "127.0.0.1"
'set the remote port
Winsock1(0).RemotePort = "10119"
'initiate the connection request
Winsock1(0).Connect
End Sub
Private Sub Winsock1_Connect(Index As Integer)
'show the connection in the label
Label3.Caption = "Connected to server!"
End Sub
Private Sub Winsock1_Close(Index As Integer)
'When the other end has closed the connection,
' close my end too
Winsock1(Index).Close
'show the connection in the label
Label3.Caption = "Dis-Connected from server!"
End Sub
Private Sub cmdSend_Click(Index As Integer)
Dim strTemp As String
Dim strTemp2 As String
Dim strTemp3 As String
Dim strTemp4 As String
'check to make ure the connection is there
If Winsock1(0).State <> sckConnected Then
'notify user they are not connected
Label3.Caption = "Not Connected!"
'exit the sub before attempting to send
Exit Sub
End If
'get the data from the text boxes on the exitbox form
strTemp = currentcoil_txt.Text + " , "
strTemp2 = currentinwidth_txt.Text + " , "
strTemp3 = processspeed_txt.Text + " , "
strTemp4 = trlength_txt.Text + " , "
'send the data to the other end
Winsock1(0).SendData strTemp
Winsock1(0).SendData strTemp2
Winsock1(0).SendData strTemp3
Winsock1(0).SendData strTemp4
'allow winsock to complete the send
DoEvents
'sets the focus on the textboxes of interest
currentcoil_txt.SetFocus
currentinwidth_txt.SetFocus
processspeed_txt.SetFocus
trlength_txt.SetFocus
End Sub
I connect when the form loads and then close the socket before the message is sent. I have the rest of the code on a timer which is the code that generates my values and I call my Send function. If I need to handle connects and disconnects.....how would I handle that .......if not what seems to look like the problem and how can I fix it?....Thanks a bunch for your willingness to help
I see your client closing the socket if the server has already closed it, but I don't see where you close the socket before the message is sent. (BTW: doing that would probably result in nothing being sent )
To handle disconnects/reconnects in your server simple encapsulate your listen call, socket accept, data I/O, result printing, close accepted socket in a while(true) loop.
You also may need to play around with using multiple .php scripts or with reinvoking your server using the php "header" command in order to get the new text message updates to occur correctly.
If you look at my server code, I do have listen call, socket accept, data I/O, result printing, close accepted socket in a while(true) loop. When I tried to run the server....it went to a not responding state. What would cause this....also how could I fix whatever is wrong?
I know that you had these statements. What I think could be happening when you use the while (true) loop is that the web server you are using does not get control back from your web page and presumes that it is busy waiting for something from a client. Consequently, any screen updates that you may do will be ignored by the web server as it hasen't had a chance to see that the current task has completed and a screen update is needed.
How you can fix this is to rearange your code a bit....
First off, presume that you are going to manually need to reload your web page in order to get screen updates to work correctly. This means that you will be using the web server to run your outer loop for you. It also means that all of your code will be executed starting at the first statement of your code each time the page is reloaded. You will need to do something like this: (in pseudo-code, you will need to implement the algorithm yourself in php, sorry but I don't have the time to do that for you right now, plus it is good experience and fun to do yourself.)
if ( listen socket is not opened )
open listen socket
Listen on listen socket
if ( client connects with server)
accept connection
while ( true )
read data
if ( read error )
process error
break
end if
process data
write data to screen
end while
close socket from accept call
reload web page to start over again
This algorithm requires that you preserve the listen socket between page reloads. php allows the saveing and retreiving of global variables that you can use for this. If you don't want to go with global variable, then you can simply close all sockets before doing the reload and don't bother with the initial "if ( listen socket..." statement and always open everything. The drawback to always opening the socket is the overhead involved, using a global will mean that subsequent web page loads will start up faster than the first time.
Thanks for your help. I just have one last question.....How exactly and what do you mean by reloading the web page....at the end of the pseudocode. How would you implement "starting over". Also you dont think it could be something wrong with my client code. I was thinking that it could be because I connect on a form load. After the one string is passed it will then go into the not responding state. Could this be a problem or do you think it lies in the server code?
Sorry for the delay in getting back you. php has the ability of sending raw http headers. Using the php command "header" you can do some pretty interesting things. One of them is to cause the web server to "branch", or load and execute another .php file - even if the file is the same one that is currently being executed. By doing this you are effectively resursively calling yourself, thus setting up your outer infinite loop.
To do this add this php statement at the end of your algorithm:
Sorry to keep bothering you.....I really do not understand about the header and web server. I have never coded php before and dont really know much about it. Could you go into a little more detail in explaining to me what is going on? To fill you in on my progress. I no longer have a not responding program. I have a server coded that will take an infinite amount of messages. The problem that I am having now is the format of the output . Currently my output is printed as follows:
(first message)
Coil Id is123456
Coil width is 376
Coil length is 786
Footage count is 989
(second message)
Coil Id is654321
Coil width is 8970
Coil length is 675
Footage count is 5432
As you can see it keeps printing the sentences over and over. I need the sentences to be printed only once with the values as the only changing text. I dont want any new lines of text. 4 lines with changing numbers. The values will change as each message somes in. is this possible and if so how would I implement it. Here is my code:
<?php
function giveMeAnArray($delimiter, $string) {
return explode($delimiter, $string);
}
4) Add the line "header( "Location: http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] );" at the end of your source after the last printf and before the last }.
It really didnt do anything. I got an error on line 37 where I have:
break 2; Fatal error: Cannot break/continue 2 levels in U:\phpProgram2 on line 37
I wanted to let you know that I am currently coding a debugging this from a php debugger/interpreter called tulip. I do not have it on a linux box yet. Will this make a difference in trying to implement that header line. Also was I supposed to put something in place of server_name and php_self?
That output does not make sense to me. You have the data printed out before you start seeing "Serving Client!" messages. I do not see how that can happen. Did you uncomment the output print statements inside your read loop?
I would have expected to see something like the following:
Like I said before......I am not using a linux operating system...I am using a php editor/interpretor(called "Tulip) that is installed on a windows machine. It prints from the bottom up. I am new to using this software but this is how it has output things all along. What do you think I should do now?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.