Link to home
Start Free TrialLog in
Avatar of mrk_raj
mrk_raj

asked on

Socket Programming

Hai experts,

I have a small socket application in which ,when I try to send some text to the other end of the connection using the following  code, fails to reach the destination..

procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientSocket1.Active:=True;
  ClientSocket1.Socket.SendText(Memo1.Text);
end;

I have set the ClientSocket1.Active as False at design time and the ClientType as ctNonBlocking.If the socket is already active (Open) then there will not be any problem.When we click the button for the first time the the contents of the Memo does not reach the other end of the connection.If  we try agian then there will not be any problem.
 
Avatar of Y SD
Y SD
Flag of Greece image

Hi,

I assume that you can establish a connectioin between the clientsocket and the serversocket.  If you do, then try sending the memotext line by line, i.e.
  ClientSocket1.Socket.SendText(Memo1.lines[memo1.lines.count-1]);

that will send the last line entered in the memo.  Or have a for loop to send all the lines in the memo one by one.

I hope this helps.
ysd
   
Avatar of FrodoBeggins
FrodoBeggins

The problem is that you use non-blocking (acynchrouious) socket. That means, that you send some command and imidiately receive back the control. But the command ("connect", in your case) needs some time to execute. So that's why you can't send the strings - you're not connected yet. You should wait until the connection is established. The second time you try, the connection is already established and you send the strings successfully
FrodoBeggins is correct that you need to wait for the connection to complete.  You can tell when you have a good connection by using the OnConnect event in your TClientSocket component.  If this is the only thing you are sending, put the ClientSocket1.Socket.SendText command in that event.  Otherwise wait until the ClientSocket1.Socket.Connected property is true.

Eric
have a look at the "chat" demo under delphi\demos\internet\chat. Its a good example for the use of client and serversocket, and it works with a memo as well.
ysd
SendText is a function, look it up in the help file. It will return 0 if it succeeded, so that you have to retry until your text got out if you don't get 0 as result. Actually, what's happening is that the connection is still being made when you're already sending text, making the send fail.

An easy fix could be this:

procedure TForm1.Button1Click(Sender: TObject);
begin
        ClientSocket1.Active:=True;

        while ClientSocket1.Socket.SendText(Memo1.Text)<>0 do
                Sleep(100); //wait some to let connection establish
end;

ASKER CERTIFIED SOLUTION
Avatar of joepezt
joepezt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi:
   
     ^_^
hi:
   
     ^_^
Greetings, mrk_raj:  

Please return to all your open questions here and update them with comments if more is needed, or award the expert(s) who helped you to close/finalize them.  I will be posting this in all your open questions shortly so that you are advised of the links and can navigate to them more easily:




It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
 
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
 
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
 
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
 
PLEASE DO NOT AWARD THE POINTS TO ME.
 
____________________________________________
 
 
 
Hi Experts:
 
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
 
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
 
Thank you everyone.
 
Moondancer :)
Community Support Moderator @ Experts Exchange
 
P.S.  Engineering has been advised about the error in the comment date/time sort order for year 2000 questions, they're in userID order instead.  REGARDING POINTS, Guidelines and more:  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  
i have tested my example and that worked...
In my opinion, most experts have contributed information which should have been useful to solve the problem.