Board index » delphi » My Client Socket not receiving data unless i send data

My Client Socket not receiving data unless i send data

Hello Winsock group,

I am developing an app that wants to send and received data to a
"server" over a LAN.
I have it working using both Indy components and the Standard Delphi
Client Socket component.
Neither of the applications receive data unless I send first, then they
receive fine.

I thought that if I establish a connecition to a server, i.e.
instantiate a socket class object, and let the connection listen, it
would receive data sent to it, without me sending data to the server
first.

Supposedly the "server" is sending packets every 3 minutes, but I am not
sure if I have the properties and methods correctly set up for my client
socket.  I am also not sure (at this time) it the guy who wrote the
server app is sending packets specifically to my computer's IP address.

Do I need to do anything special to my ClientSocket to have it receive
data without me sending???  Or do I need to set up a server socket to
capture the data from the other "server" , thus, treating it as a
client?  I have not yet tried that one. I could turn off the client, and
connect the server, and visa versa.

This is my first attempt at using a custom protocol (or any protocol)
and then communitcating using the Winsock components, etc.

Thanks for any info,

Eric W. Holzapfel

 

Re:My Client Socket not receiving data unless i send data


You have the client listening?
That should not work.
The server listens for new connections, the client connected to a listening
server.
Once a client is connected, it doesn't matter who sends data first.
So there must be some other problem in your code, maybe you could show some
of it?

Andy

--
Kyoto Now !

Re:My Client Socket not receiving data unless i send data


Here is a sample of some code I use to "try to read and write".  I have turned
off the write part, to see if it will receive without writing.  I am a true
beginner, so I have stolen the code here from one of the Delphi 5 help
examples.  One procedure i do not understand how to properly implement is the
"GetNextRequest" idea.

any comments will be helpful, I am in a learning mode for sure,

thanks

Eric/Ollie

Listing below:  Note the client socket is set for blocking for the stream.

unit sockthread;

interface

uses
  Classes, Dialogs, ScktComp, SysUtils;

type
  TMyClientThread = class(TThread)
  private
    { Private declarations  RecvBuf : Array[0..31] of Byte;}
    procedure GetNextRequest(buffer: Array of Byte);
    procedure HandleThreadException;
  protected
    procedure Execute; override;
  end;
 var ByteCount: Integer;
implementation
      uses main;
{ Important: Methods and properties of objects in VCL can only be used in a
  method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TMyClientThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TMyClientThread }

procedure TMyClientThread.Execute;
 const rbuffer: Array[0..11] of byte = ($0, $0C, $0E, $00, $30, $30, $30, $30,
                                    $30, $31, $31, $33);
 var
  TheStream: TWinSocketStream;
  Buf : Array[0..4095] of Byte;
  count, i,  message_count : Integer;
  ProcessBuffer: Array of Byte;
begin
  { create a TWinSocketStream for reading and writing }
  TheStream := TWinSocketStream.Create(ChatForm.ClientSocket.Socket, 60000);
  message_count := 1;
  for i := 0 to High(buf) do
    buf[i] := 0;
  try
    { fetch and process commands until the connection or thread is terminated }

//    while (not Terminated) and (Chatform.ClientSocket.Active) do
  while (not Terminated) and (Chatform.ClientSocket.Socket.Connected) do
    begin
      try
        if not Chatform.ClientSocket.Active then
          begin
           Terminate;
           break;
          end;
        GetNextRequest(Buf); { GetNextRequest must be a thread-safe method }
        { write the request to the server }
       // TheStream.Write(rbuffer, Length(rbuffer));
       { continue the communication (eg read a response from the server) }
           if TheStream.Read(Buf, sizeof(Buf)) = 0 then
              ChatForm.ClientSocket.Close
            else
              begin
               count := Buf[0] + Buf[1];
               SetLength(ProcessBuffer, count);
               for i := 0 to count - 1 do
                 ProcessBuffer[i] := Buf[i];
               ChatForm.ProcessReceivedData(ProcessBuffer, count);
               SetLength(ProcessBuffer, 1);
               ProcessBuffer[0] := 0;
              end;
         //  INC(message_count);
        // if message_count = 20 then
        //  begin
        //    Chatform.ClientSocket.Active := False;
        //    Terminate;
        //    ShowMessage('done!');
         // end;
      except
        if not(ExceptObject is EAbort) then
          Synchronize(HandleThreadException); { you must write
HandleThreadException }
      end;
    end;
  finally
   TheStream.Free;
  end;
end;

procedure TMyClientThread.GetNextRequest(buffer: array of Byte);
var
  i : Integer;
begin
 for i := 0 to High(buffer) do
    buffer[i] := 0;
//ByteCount := 15;//(buffer[0] + buffer[1]);
// ShowMessage('getting a byte?');
//ChatForm.ClientSocketRead(ChatForm, ChatForm.ClientSocket.Socket);
end;

procedure TMyClientThread.HandleThreadException;
begin
 showMessage('Thread Error');
end;

end.

Quote
"Andy M." wrote:
> You have the client listening?
> That should not work.
> The server listens for new connections, the client connected to a listening
> server.
> Once a client is connected, it doesn't matter who sends data first.
> So there must be some other problem in your code, maybe you could show some
> of it?

> Andy

> --
> Kyoto Now !

Other Threads