+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    75

    How to use 'TWinSocketStream'?

    Hi, I have this simple procedure:
    Code:
    procedure MyThread.ReceiveText(MySocket: TClientSocket);
    var
      TotalText: string;
      SockStream: TWinSocketStream;
      Buffer: array[0..1023] of Char;
    begin
    SockStream := TWinSocketStream.Create(MySocket.Socket, 5000);
        while (SockStream.Read(Buffer, SizeOf(Buffer)) <> 0) do
        TotalText:= TotalText + Buffer;
        ShowMessage(TotalText);
    I want to receive the text and convert into a simple string. But my buffer is with a lot of characters like
    'Ȁ'#0'良''л'... How can I receive the text from Socket in simple string?

  2. #2
    Member
    Join Date
    Nov 2009
    Posts
    75
    Code:
    procedure MyThread.ReceiveText(MySocket: TClientSocket);
    var
      ReceivedString: string;
      SockStream: TWinSocketStream;
      Buffer: array[0..1023] of Char;
    begin
     FillChar(Buffer, SizeOf(Buffer), #0);
      SockStream := TWinSocketStream.Create(MySocket.Socket, 2000);
      while (SockStream.Read(Buffer, SizeOf(Buffer)) <> 0) do
        ReceivedString:= ReceivedString + Buffer;
    
        if Length(ReceivedString) = 0 then
        Terminate
        else
        ReceivedString:= PAnsiChar(ReceivedString);
    nice and slow...

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    75
    Hey, I'm tryin to receive a socket response but no success... And don't know why. Look my code:
    Code:
    procedure MyThread.SendText(Command: string; MySocket: TClientSocket);
    var
      ReceivedString: string;
      SockStream: TWinSocketStream;
      Buffer: array[0..1023] of Char;
    begin
      FillChar(Buffer, SizeOf(Buffer), #0);
      SockStream := TWinSocketStream.Create(MySocket.Socket, 2000);
    
      if Command='Username' then
        begin
          MySocket.Socket.SendText('USER test'+#13+#10); //when I send this, I receive a response like '+OK Password required'
          while SockStream.Read(Buffer, SizeOf(Buffer)) <> 0 do //this should read the response and put in ReceivedString, but nothing happens...
            ReceivedString:= ReceivedString + Buffer;
    I'm sending the 'USER test' with success, and the server is answering with the message... But I can't receive this message... Why??

  4. #4
    Senior Member
    Join Date
    Nov 2009
    Posts
    102
    If I remember right, TClientsocket have a OnRead Event. Use that and try to get the answer there

  5. #5
    Member
    Join Date
    Nov 2009
    Posts
    75
    I'm using thread... Tried to set an OnRead event but this never get inside the event... I receive the text and the thread don't get inside the event... Don't know why, looks like events only work in the main thread.
    Last edited by SubGenius; 3 Weeks Ago at 11:30.

  6. #6
    Member
    Join Date
    Nov 2009
    Posts
    75
    If I do like this, work:
    Code:
    ...
      if Command='Username' then
        begin
          MySocket.Socket.SendText('USER test'+#13+#10);
          MySocket.Socket.SendText('PASS test'+#13+#10);
          while SockStream.Read(Buffer, SizeOf(Buffer)) <> 0 do
            ReceivedString:= ReceivedString + Buffer;
    But I'm getting the last response... I wanted to before send PASS, receive the response for USER... Why this shit don't work...

 

 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT +1. The time now is 11:53.
www.opensc.ws
Copyright ©2005 - 2012, OpenSC Forums



Search Engine Friendly URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.