Solved

Socket Programming

Posted on 2001-06-14
11
301 Views
Last Modified: 2010-04-06
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
Comment
Question by:mrk_raj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
11 Comments
 
LVL 2

Expert Comment

by:ysd
ID: 6190834
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
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6191336
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
 
LVL 1

Expert Comment

by:edsteele
ID: 6191528
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:ysd
ID: 6192276
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6193417
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
 
LVL 2

Accepted Solution

by:
joepezt earned 50 total points
ID: 6194773
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
 

Expert Comment

by:airhorse
ID: 6195220
hi:
   
     ^_^
0
 

Expert Comment

by:airhorse
ID: 6195259
hi:
   
     ^_^
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6466956
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
 
LVL 2

Expert Comment

by:joepezt
ID: 6467119
i have tested my example and that worked...
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6467404
In my opinion, most experts have contributed information which should have been useful to solve the problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question