[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Remove the thread from the connection

Posted on 2006-05-31
6
Medium Priority
?
210 Views
Last Modified: 2010-04-05
Hi, experts,

I have found a connection for my 3270-component
and it works perfectly, only this connection uses a thread
and I don’t know nothing about threads!

How can I remove the thread and still have the connection work?

This is the code:

procedure TForm1.Disconnect;
begin
 // if RecThreadRunning then
 //   RecThread.Terminate() ;
  if socOpen then
    begin
      closesocket(soc) ;
      socOpen := false ;
    end ;
  TcpCleanup() ;
end;
(*------------------------------------*)
procedure TForm1.DumpChar(ch: char);
var
  sf: string ;
  ic: integer ;
  i: integer ;
  e: char ;
begin
  DumpNum := DumpNum + 1 ;
  DumpStr[DumpNum] := ch ;
  if DumpNum = 16 then
    begin
      sf := format('%6.6x  ',[DumpBytes]) ;
      for i := 1 to 16 do
        begin
          ic := ord(DumpStr[i]) ;
          sf := sf+Format('%2.2x ',[ic]) ;
        end ;
      sf := sf+'  *' ;
      for i := 1 to 16 do
        begin
          if (ord(DumpStr[i]) >= 32) and (ord(DumpStr[i]) < 127) then
            sf := sf+DumpStr[i]
          else
            sf := sf+'.' ;
        end ;
      sf := sf+'*  *' ;
      for i := 1 to 16 do
        begin
          e := Char(eb2as(Byte(DumpStr[i]))) ;
          if (ord(e) >= 32) and (ord(e) < 127) then
            sf := sf+e
          else
            sf := sf+'.' ;
        end ;
      sf := sf+'*' ;
      DumpBytes := DumpBytes + DumpNum ;
      DumpNum := 0 ;
      for i := 1 to 16 do
        DumpStr[i] := chr(0) ;
    end ;
end ;
(*------------------------------------*)
procedure TForm1.DumpFlush;
var
  sf: string ;
  ic: integer ;
  i: integer ;
  e: char ;
begin
  if DumpNum > 0 then
    begin
      sf := format('%6.6x  ',[DumpBytes]) ;
      for i := 1 to 16 do
        begin
          ic := ord(DumpStr[i]) ;
          sf := sf+Format('%2.2x ',[ic]) ;
        end ;
      sf := sf+'  *' ;
      for i := 1 to 16 do
        begin
          if (ord(DumpStr[I]) >= 32) and (ord(DumpStr[i]) < 127) then
            sf := sf+DumpStr[i]
          else
            sf := sf+'.' ;
        end ;
      sf := sf+'*  *' ;
      for i := 1 to 16 do
        begin
          e := Char(eb2as(Byte(DumpStr[i]))) ;
          if (ord(e) >= 32) and (ord(e) < 127) then
            sf := sf+e
          else
            sf := sf+'.' ;
        end ;
      sf := sf+'*' ;
    end ;
  DumpNum := 0 ;
  DumpBytes := 0 ;
  for i := 1 to 16 do
    DumpStr[i] := chr(0) ;
end ;
(*------------------------------------*)
procedure TForm1.FormCreate(Sender: TObject);
begin
    DSP32701.LockKeyBoard('WAIT  ', NOALARM);
    DSP32701.OnSendData:=Self.OnSendData;
    ConnectHostName := ('145.70.16.2');
    MakeConnect;
end;
(*------------------------------------*)
procedure TForm1.MakeConnect;
type
  TPint = ^Longint ;
var
  err: integer ;
  phost: PHostEnt ;
  ina: TInAddr ;
  lina: TInAddr ;
  pint: TPint ;
  RemoteHostName: TPath ;
  PRemoteHostName: PChar ;
  LocalHostName: TPath ;
  PLocalHostName: PChar ;
  LocalAddr: TSockAddrIn ;
  psaddr: PSockAddr ;
  RemoteAddr: TSockAddrIn ;
  pServAddr: PServEnt ;
  usPort: u_short ;
  InitCmd: string ;
  NonBlocked: u_long ;
begin
  TcpStartup() ;
  soc := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
  if soc = INVALID_SOCKET then
    begin
      ShowMessage('Error socket invalid.') ;
      socOpen := False ;
    end
  else
    begin
      socOpen := True ;
    end ;
  { get foreign host name and addr }
  PRemoteHostName := @RemoteHostName ;
  StrPCopy(PRemoteHostName,ConnectHostName) ;
  phost := gethostbyname(PRemoteHostName) ;
  if phost = nil then
    begin
      ShowMessage('Error, failed to find host.') ;
      Disconnect ;
    end
  else
    begin
      pint := Pointer(phost^.h_addr_list^) ;
      ina.s_addr := pint^ ;
      { get local host name and addr }
      PLocalHostName := @LocalHostName ;
      if gethostname(PLocalHostName,MAX_PATH) <> SOCKET_ERROR then
        begin
          phost := gethostbyname(PLocalHostName) ;
          if phost = nil then
            begin
              SHowMessage('Error, failed to find local host.') ;
              Disconnect ;
            end
          else
            begin
              pint := Pointer(phost^.h_addr_list^) ;
              lina.s_addr := pint^ ;
              { bind socket to addr }
              LocalAddr.sin_family := AF_INET ;
              LocalAddr.sin_addr.s_addr := lina.s_addr ;
              LocalAddr.sin_port := htonl(INADDR_ANY) ;
              psaddr := Pointer(@LocalAddr) ;
              err := bind(soc,psaddr^,SizeOf(LocalAddr)) ;
              if err = SOCKET_ERROR then
                begin
                 showmessage('Bind error: '
                    +Format('%d',[WSAGetLastError()])) ;
                  Disconnect ;
                end
              else
                begin
                  { get service port }
                  pServAddr := getservbyname('telnet','tcp') ;
                  if pServAddr <> nil then
                    begin

                      usPort := pServAddr^.s_port ;

                      { connect to server }
                      RemoteAddr.sin_family := AF_INET ;
                      RemoteAddr.sin_addr.s_addr := ina.s_addr ;
                      RemoteAddr.sin_port := usPort ;
                      psaddr := Pointer(@RemoteAddr) ;
                      err := connect(soc,psaddr^,SizeOf(RemoteAddr)) ;
                      if err = SOCKET_ERROR then
                        begin
                          ShowMessage('Connect error: '
                            +Format('%d',[WSAGetLastError()])) ;
                          Disconnect ;
                        end
                      else
                        begin
                          RecvThrd.Create(soc);
                          { send CR LF to get server started }
                        //  SendData(InitCmd) ;
                        end ;
                    end
                  else
                    begin
                      Disconnect ;
                    end ;
                end ;
            end ;
        end
      else
        begin
          ShowMessage('Get local host error:'
            +Format('%d',[WSAGetLastError()])) ;
          Disconnect ;
        end ;
    end ;
end;
(*------------------------------------*)
procedure TForm1.OnSendData(Sender: TObject; sdata: String);
const
  C_CR    = $0A ; { carrage return }
  C_LF    = $0D ; { line feed }
type
  TOutBuf = array[0..4096] of char ;
var
  err: integer ;
  size: integer ;
  pc: PChar ;
  OutBuffer: TOutBuf ;
  i: integer ;
begin
  if EorFlag then
    sdata := sdata + Char(IAC) + Char(C_EOR) ;
  pc := @OutBuffer ;
  StrPCopy(pc,sdata) ;
  size := Length(sdata) ;
  err := send(soc,OutBuffer,size,0) ;
  if err = SOCKET_ERROR then
    ShowMessage('Send error: '
      +Format('%d',[WSAGetLastError()]))
  else
    begin
          for i:=0 to (size-1) do
            DumpChar(OutBuffer[i]) ;
          DumpFlush ;
    end ;
end;
(*------------------------------------*)
procedure TForm1.TcpCleanup;
var
  err: integer ;
begin
  err := WSACancelBlockingCall() ;
  err := WSACleanup() ;
end ;
(*------------------------------------*)
procedure TForm1.TcpStartup;
var
  err: integer ;
  VersionReq: word ;
  wsaData: TWSAData ;
begin
  VersionReq := $0101 ;
  err := WSAStartup(VersionReq,wsaData) ;
end;
(*------------------------------------*)

{ RecvThrd }

constructor RecvThrd.Create(ios: TSocket);
var
  i: integer ;
begin
  FreeOnTerminate := True ;
  IOSocket := ios ;
  numchars := 0 ;
  for i := 1 to 16 do
    s[i] := chr(0) ;
  inherited Create(False) ;  { dont run thread yet }
end ;
(*------------------------------------*)
procedure RecvThrd.Execute;
type
  PTelCmd = ^TTelCmd ;
  TTelCmd = packed record
    c_iac: Byte ;
    c_cmd: Byte ;
    c_func: Byte ;
  end ;
  PTelTermCmd = ^TTelTermCmd ;
  TTelTermCmd = packed record
    c_iac:  Byte ;
    c_sb:   Byte ;
    c_term: Byte ;
    c_is:   Byte ;
    c_type: array[1..10] of char ;
    c_iace: Byte ;
    c_se:   Byte ;
  end ;
var
  InDataLen: integer ;
  OutDataLen: integer ;
  err: integer ;
  size: integer ;
  pc: PChar ;
  i: integer ;
  pInTcmd: PTelCmd ;
  pOutTcmd: PTelCmd ;
  pOutTermTcmd: PTelTermCmd ;
  outstr: string ;
  InBuffer: TBuf ;
  OutBuffer: TBuf ;
  AmmountWaiting: u_long ;
  WaitMilliSecs: integer ;
begin
  outstr := '' ;
  RecThreadRunning := true ;
  AmmountWaiting := 0 ;
  while AmmountWaiting = 0 do
    begin
      err := ioctlsocket(IOSocket,FIONREAD,AmmountWaiting) ;

      if AmmountWaiting = 0 then
        Sleep(WaitMilliSecs) ;
      if Terminated then
        break ;
    end ;
  size := SizeOf(InBuffer) ;
  pc := @InBuffer ;
  err := recv(IOSocket,InBuffer,size,0) ;
  if err = SOCKET_ERROR then
    ShowMessage('Recv error: '+Format('%d',[WSAGetLastError()]))
  else
    begin
      InDataLen := err ;
      if deb then
        begin
          ShowMessage('Recv: '+Format(' (%d)',[InDataLen])) ;
          for i:=0 to (InDataLen-1) do
        end ;
     OutDataLen := 0 ;
      pInTcmd := Pointer(@InBuffer) ;
      pOutTcmd := Pointer(@OutBuffer) ;
      while InDataLen > 0 do
        begin
          if (pInTcmd^.c_iac = IAC) and (pInTcmd^.c_cmd <> $FF) then
            begin
              if pInTcmd^.c_cmd = C_EOR then
                begin
                  pInTcmd := Pointer(PChar(pInTcmd) + 2) ;
                  InDataLen := InDataLen - 2 ;
                  if length(outstr) > 0 then
                    begin
                      DataStreamBuf := outstr ;
                      Synchronize(ProcessDataStream) ;
                      outstr := '' ;
                    end ;
                end
              else if pInTcmd^.c_cmd = C_DO then
                begin
                  pOutTcmd^.c_iac := IAC ;
                  if (pInTcmd^.c_func = TOPT_TERM) or
                     (pInTcmd^.c_func = TOPT_BIN) or
                     (pInTcmd^.c_func = TOPT_EOR) then
                    begin
                      if (pInTcmd^.c_func = TOPT_EOR) then
                        EorFlag := True ;
                      pOutTcmd^.c_cmd := C_WILL ;
                    end
                  else
                    pOutTcmd^.c_cmd := C_WONT ;
                  pOutTcmd^.c_func := pInTcmd^.c_func ;
                  pOutTcmd := Pointer(PChar(pOutTcmd) + 3) ;
                  OutDataLen := OutDataLen  + 3 ;
                  pInTcmd := Pointer(PChar(pInTcmd) + 3) ;
                  InDataLen := InDataLen - 3 ;
                end
              else if pInTcmd^.c_cmd = C_DONT then
                begin
                  if (pInTcmd^.c_func = TOPT_EOR) then
                    EorFlag := False ;
                  pOutTcmd^.c_iac := IAC ;
                  pOutTcmd^.c_cmd := C_WONT ;
                  pOutTcmd^.c_func := pInTcmd^.c_func ;
                  pOutTcmd := Pointer(PChar(pOutTcmd) + 3) ;
                  OutDataLen := OutDataLen  + 3 ;
                  pInTcmd := Pointer(PChar(pInTcmd) + 3) ;
                  InDataLen := InDataLen - 3 ;
                end
              else if pInTcmd^.c_cmd = C_WILL then
                begin
                  pOutTcmd^.c_iac := IAC ;
                  if (pInTcmd^.c_func = TOPT_TERM) or
                     (pInTcmd^.c_func = TOPT_BIN) or
                     (pInTcmd^.c_func = TOPT_EOR) then
                    pOutTcmd^.c_cmd := C_DO
                  else
                    pOutTcmd^.c_cmd := C_DONT ;
                  pOutTcmd^.c_func := pInTcmd^.c_func ;
                  pOutTcmd := Pointer(PChar(pOutTcmd) + 3) ;
                  OutDataLen := OutDataLen  + 3 ;
                  pInTcmd := Pointer(PChar(pInTcmd) + 3) ;
                  InDataLen := InDataLen - 3 ;
                end
              else if pInTcmd^.c_cmd = C_WONT then
                begin
                  pInTcmd := Pointer(PChar(pInTcmd) + 3) ;
                  InDataLen := InDataLen - 3 ;
                end
              else if pInTcmd^.c_cmd = C_SB then
                begin
                  if (pInTcmd^.c_func = TOPT_TERM) then
                    begin
                      pOutTermTcmd := Pointer(pOutTcmd) ;
                      pOutTermTcmd^.c_iac := IAC ;
                      pOutTermTcmd^.c_sb := C_SB ;
                      pOutTermTcmd^.c_term := TOPT_TERM ;
                      pOutTermTcmd^.c_is := C_NULL ;
                      case Model of
                        ibm3278_2: pOutTermTcmd^.c_type := 'IBM-3278-2' ;
                        ibm3278_3: pOutTermTcmd^.c_type := 'IBM-3278-3' ;
                        ibm3278_4: pOutTermTcmd^.c_type := 'IBM-3278-4' ;
                      else
                        pOutTermTcmd^.c_type := 'IBM-3278-2' ;
                      end ;
                      pOutTermTcmd^.c_iace := IAC ;
                      pOutTermTcmd^.c_se := C_SE ;
                      pOutTcmd := Pointer(PChar(pOutTcmd) + 16) ;
                      OutDataLen := OutDataLen  + 16 ;
                      pInTcmd := Pointer(PChar(pInTcmd) + 4) ;
                      InDataLen := InDataLen - 4 ;
                    end
                  else
                    begin
                      pInTcmd := Pointer(PChar(pInTcmd) + 3) ;
                      InDataLen := InDataLen - 3 ;
                    end ;
                end
              else if pInTcmd^.c_cmd = C_SE then
                begin
                  pInTcmd := Pointer(PChar(pInTcmd) + 2) ;
                  InDataLen := InDataLen - 2 ;
                end
              else if pInTcmd^.c_cmd = C_NOP then
                begin
                  pInTcmd := Pointer(PChar(pInTcmd) + 2) ;
                  InDataLen := InDataLen - 2 ;
                end
              else if pInTcmd^.c_cmd = C_EL then
                begin
                  pInTcmd := Pointer(PChar(pInTcmd) + 2) ;
                  InDataLen := InDataLen - 2 ;
                end
              else if pInTcmd^.c_cmd = C_EC then
                begin
                  pInTcmd := Pointer(PChar(pInTcmd) + 2) ;
                  InDataLen := InDataLen - 2 ;
                end ;
            end
          else
            begin
              while InDataLen > 0 do
                begin
                  if (pInTcmd^.c_iac = IAC) and (pInTcmd^.c_cmd = $FF) then
                    begin
                      outstr := outstr + Char($FF) ;
                      InDataLen := InDataLen - 2 ;
                      pInTcmd := Pointer(PChar(pInTcmd) + 2) ;
                    end
                  else if (pInTcmd^.c_iac = IAC) then
                    break
                  else
                    begin
                      outstr := outstr + Char(pInTcmd^.c_iac) ;
                      InDataLen := InDataLen - 1 ;
                      pInTcmd := Pointer(PChar(pInTcmd) + 1) ;
                    end ;
                end ;
            end ;
          if InDataLen = 0 then
            begin
              err := ioctlsocket(IOSocket,FIONREAD,AmmountWaiting) ;
              if err = SOCKET_ERROR then
                SHowMessage('IOctlsocket error: '
                  +Format('%d',[WSAGetLastError()]))
              else
                if deb and (AmmountWaiting > 0) then
                  ShowMessage('Input queue length: '+Format('%d',[AmmountWaiting])) ;
              if OutDataLen > 0 then
                begin
                  pOutTcmd^.c_iac := $00 ;
                  err := send(IOSocket,OutBuffer,OutDataLen,0) ;
                  if err = SOCKET_ERROR then
                    showmessage('Send IAC error: '+Format('%d',[WSAGetLastError()])) ;
                  if deb then
                    begin
                      for i:=0 to (OutDataLen-1) do
                    end ;
                  pOutTcmd := Pointer(@OutBuffer) ;
                  OutDataLen := 0 ;
                end ;
              AmmountWaiting := 0 ;
              while AmmountWaiting = 0 do
                begin
                  err := ioctlsocket(IOSocket,FIONREAD,AmmountWaiting) ;
                  if err = SOCKET_ERROR then
                    Showmessage('IOctlsocket error: '
                      +Format('%d',[WSAGetLastError()]))
                  else
                    if deb and (AmmountWaiting > 0) then
                      ShowMessage('Input queue length: '+Format('%d',[AmmountWaiting])) ;
                  if AmmountWaiting = 0 then
                    Sleep(WaitMilliSecs) ;
                  if Terminated then
                    break ;
                end ;
              if not Terminated then
                begin
                  size := SizeOf(InBuffer) ;
                  err := recv(IOSocket,InBuffer,size,0) ;
                  if err = SOCKET_ERROR then
                    ShowMessage('Recv IAC error: '
                      +Format('%d',[WSAGetLastError()])) ;
                  pInTcmd := Pointer(@InBuffer) ;
                  InDataLen := err ;
                  if deb then
                    begin
                      showmessage('Recv: '+Format(' (%d)',[err])) ;
                      for i:=0 to (err-1) do
                    end ;
                end ;
            end ;

          if Terminated then
            break ;
        end ;
    end ;
  RecThreadRunning := false ;
end;
(*------------------------------------*)
procedure RecvThrd.ProcessDataStream;
begin
//  Ds3270.DataIn(DataStreamBuf) ;
end;
(*------------------------------------*)
procedure TForm1.DSP32701SendData(Sender: TObject; Data: String);
const
  C_CR    = $0A ; { carrage return }
  C_LF    = $0D ; { line feed }
type
  TOutBuf = array[0..4096] of char ;
var
  err: integer ;
  size: integer ;
  pc: PChar ;
  OutBuffer: TOutBuf ;
  i: integer ;
begin
  if EorFlag then
    data := data + Char(IAC) + Char(C_EOR) ;
  pc := @OutBuffer ;
  StrPCopy(pc,data) ;
  size := Length(data) ;
  err := send(soc,OutBuffer,size,0) ;
  if err = SOCKET_ERROR then
    ShowMessage('Send error: '
      +Format('%d',[WSAGetLastError()]))
  else
    begin
          for i:=0 to (size-1) do
            DumpChar(OutBuffer[i]) ;
          DumpFlush ;
    end ;
end;

Peter.
0
Comment
Question by:peterkiers
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 16798163
I must tell you that removing the thread will have a negative impact on your app, in the form of form freezing, hangups etc, depending on the "network connection" and how the other line is sending or not data. my advice is that you leave it in.

why do you want to take it out anyway? if it's because you don't know trheads, then I strongly recomment starting to learn and understand them, because windows applications are all about trheads (even if you don't see them explicitly)
0
 
LVL 1

Author Comment

by:peterkiers
ID: 16798483
Oke, your whright, I will follow your advice, and leave it this way.
But because I use a thread I still have another problem!

As you can see I have 2 classes on the main form:
1. TForm-class
2. TThread-class

My problem is:
When I call a procedure of my component
in the TForm-class, it works.

For example in the OnCreate-event:

    DSP32701.LockKeyBoard('WAIT  ', NOALARM);
    DSP32701.OnSendData:=Self.OnSendData;
    ConnectHostName := ('145.70.16.2');
    MakeConnect;

But when I want to call a procedure of my component
in the TThread-class, and I type DSP3270, I get nothing.

procedure RecvThrd.ProcessDataStream;
begin
//  DSP3270.DataIn(DataStreamBuf) ; //this doesn't work
end;

Peter
0
 
LVL 28

Expert Comment

by:2266180
ID: 16798522
:) of course, since that componet belongs to the form. it's perfectly natural. if you want to access it from the trhead, then add e property to the thread for your component, modify the constructor to take as parameter the component and initialize the property, and modify the code so that when the thread is created, it is passed the component. (that will be in the MakeConnect; procedure)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:peterkiers
ID: 16798545
Can you help me with it, because I can't follow?
0
 
LVL 28

Accepted Solution

by:
2266180 earned 2000 total points
ID: 16798975
I'll try to define the thread as you needed, but you will have to adapt to your code (since you didn't provide the type definitions :P)
there are only 3 or 4 lines changed, so it should be easy to follow the changes

Type RecvThrd = class(TThread)
        private
          DSP32701:yourcomponenttypehere;
        public
          constructor Create(ios: TSocket; dsp:yourcomponenttypehere);
        end;


procedure TForm1.MakeConnect;
type
  TPint = ^Longint ;
var
  err: integer ;
  phost: PHostEnt ;
  ina: TInAddr ;
  lina: TInAddr ;
  pint: TPint ;
  RemoteHostName: TPath ;
  PRemoteHostName: PChar ;
  LocalHostName: TPath ;
  PLocalHostName: PChar ;
  LocalAddr: TSockAddrIn ;
  psaddr: PSockAddr ;
  RemoteAddr: TSockAddrIn ;
  pServAddr: PServEnt ;
  usPort: u_short ;
  InitCmd: string ;
  NonBlocked: u_long ;
begin
  TcpStartup() ;
  soc := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
  if soc = INVALID_SOCKET then
    begin
      ShowMessage('Error socket invalid.') ;
      socOpen := False ;
    end
  else
    begin
      socOpen := True ;
    end ;
  { get foreign host name and addr }
  PRemoteHostName := @RemoteHostName ;
  StrPCopy(PRemoteHostName,ConnectHostName) ;
  phost := gethostbyname(PRemoteHostName) ;
  if phost = nil then
    begin
      ShowMessage('Error, failed to find host.') ;
      Disconnect ;
    end
  else
    begin
      pint := Pointer(phost^.h_addr_list^) ;
      ina.s_addr := pint^ ;
      { get local host name and addr }
      PLocalHostName := @LocalHostName ;
      if gethostname(PLocalHostName,MAX_PATH) <> SOCKET_ERROR then
        begin
          phost := gethostbyname(PLocalHostName) ;
          if phost = nil then
            begin
              SHowMessage('Error, failed to find local host.') ;
              Disconnect ;
            end
          else
            begin
              pint := Pointer(phost^.h_addr_list^) ;
              lina.s_addr := pint^ ;
              { bind socket to addr }
              LocalAddr.sin_family := AF_INET ;
              LocalAddr.sin_addr.s_addr := lina.s_addr ;
              LocalAddr.sin_port := htonl(INADDR_ANY) ;
              psaddr := Pointer(@LocalAddr) ;
              err := bind(soc,psaddr^,SizeOf(LocalAddr)) ;
              if err = SOCKET_ERROR then
                begin
                 showmessage('Bind error: '
                    +Format('%d',[WSAGetLastError()])) ;
                  Disconnect ;
                end
              else
                begin
                  { get service port }
                  pServAddr := getservbyname('telnet','tcp') ;
                  if pServAddr <> nil then
                    begin

                      usPort := pServAddr^.s_port ;

                      { connect to server }
                      RemoteAddr.sin_family := AF_INET ;
                      RemoteAddr.sin_addr.s_addr := ina.s_addr ;
                      RemoteAddr.sin_port := usPort ;
                      psaddr := Pointer(@RemoteAddr) ;
                      err := connect(soc,psaddr^,SizeOf(RemoteAddr)) ;
                      if err = SOCKET_ERROR then
                        begin
                          ShowMessage('Connect error: '
                            +Format('%d',[WSAGetLastError()])) ;
                          Disconnect ;
                        end
                      else
                        begin
                          RecvThrd.Create(soc, DSP32701);
                          { send CR LF to get server started }
                        //  SendData(InitCmd) ;
                        end ;
                    end
                  else
                    begin
                      Disconnect ;
                    end ;
                end ;
            end ;
        end
      else
        begin
          ShowMessage('Get local host error:'
            +Format('%d',[WSAGetLastError()])) ;
          Disconnect ;
        end ;
    end ;
end;

constructor RecvThrd.Create(ios: TSocket; dsp:yourcomponenttypehere);
var
  i: integer ;
begin
  FreeOnTerminate := True ;
  DSP32701:=dsp;
  IOSocket := ios ;
  numchars := 0 ;
  for i := 1 to 16 do
    s[i] := chr(0) ;
  inherited Create(False) ;  { dont run thread yet }
end ;

then you can access DSP32701 as it was in your form.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 16799076
YES, it works.

Peter
0

Featured Post

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.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

831 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