• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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.
 
0
mrk_raj
Asked:
mrk_raj
  • 2
  • 2
  • 2
  • +4
1 Solution
 
ysdCommented:
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
   
0
 
FrodoBegginsCommented:
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
0
 
edsteeleCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ysdCommented:
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
0
 
AvonWyssCommented:
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;

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

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
var
i : integer;

begin
for i := 0 to memo1.lines.count -1 do
    clientsocket1.socket.sendtext(memo1.lines[i]);

end;


------- Remote computer

procedure TForm1.ServerSocket1ClientWrite(Sender: TObject;
  Socket: TCustomWinSocket);
begin

memo1.lines.add(socket.receivetext);
end;


- joepezt -
0
 
airhorseCommented:
hi:
   
     ^_^
0
 
airhorseCommented:
hi:
   
     ^_^
0
 
MoondancerCommented:
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:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  
0
 
joepeztCommented:
i have tested my example and that worked...
0
 
AvonWyssCommented:
In my opinion, most experts have contributed information which should have been useful to solve the problem.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now