Solved

Socket Programming

Posted on 2001-06-14
11
296 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Thread safe  opinion 7 126
Wininet read php file from internet issue 6 81
Communication Between RC4 Delphi <-> PHP 3 100
Unique identifier on a terminal server (rdp) 4 46
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now