Recent

Author Topic: winsock Send Data from cmd to Listview  (Read 1107 times)

BlackBat

  • New Member
  • *
  • Posts: 10
winsock Send Data from cmd to Listview
« on: June 24, 2019, 02:17:55 pm »
Good Morning , its me again.
On command line this sends the variables from across the network (client / server) Now i want to have the same application send from a command line to say a a form with a listview and display on the Listview.

I got very lost along the line as i have not done this sending from a command line to GUI , so this would be my first trial for something like that

Client Works fine , likewise Server .. But on cmd.exe only but how can i send it now from the command line and have the values shown in the Listview. My code looks somewhat like this :

Client (Which gets the information for Display looks like this : ):

Code: Pascal  [Select][+][-]
  1. unit myclientUi;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls , Windows , Winsock;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     ListView1: TListView;
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28. procedure ReceiveBuf();
  29. var
  30.       _wsdata: WSAData;
  31.       serverSocket, S: TSocket;
  32.       _addrIn, _addr: sockaddr_in;
  33.       addrSize: Integer;
  34.       SendBuf:Array[0..31] of AnsiChar;
  35.       data : Integer;
  36. begin
  37.        if WSAStartup(MakeWord(2, 2), _wsdata) <> 0 then
  38.     Exit;
  39.       serverSocket := socket(AF_INET, SOCK_STREAM, 0);
  40.         if serverSocket = INVALID_SOCKET then
  41.     Exit;
  42.           _addrIn.sin_family := AF_INET;
  43.           _addrIn.sin_addr.S_addr := INADDR_ANY;
  44.           _addrIn.sin_port := htons(8080);
  45.  
  46.           if bind(serverSocket, _addrIn, SizeOf(_addrIn)) = SOCKET_ERROR then
  47.           Exit;
  48.  
  49.           if listen(serverSocket, SOMAXCONN) = SOCKET_ERROR then
  50.             Exit;
  51.  
  52.             addrSize := SizeOf(_addrIn);
  53.             getsockname(serverSocket, _addrIn, addrSize);
  54.             //Writeln(Format('Listening on port %d' + #13#10, [ntohs(_addrIn.sin_port)]));
  55.             while True do
  56.             begin
  57.                  S := accept(serverSocket, @_addr, @addrSize);
  58.                 if S <> INVALID_SOCKET then
  59.                   begin
  60.                     repeat
  61.                     data := recv(s, SendBuf, SizeOf(SendBuf), 0);
  62.                     if data > 0 then Writeln('Bytes Received: ' + IntToStr(data) + ' Text: ' +SendBuf); // I believe it should save into the Listview From here
  63.                   until data <= 0;
  64.                   closesocket(S);
  65.                   end;
  66.               end;
  67.  
  68. end;
  69.  
  70. { TForm1 }
  71.  
  72.  
  73. end.
  74.  


Now Server which sends the information looks like this (Works 100% Fine)

Code: Pascal  [Select][+][-]
  1. program server;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes , SysUtils , Windows , Winsock
  10.   { you can add units after this };
  11.  
  12. procedure SendBuf(Log: Array of AnsiChar);
  13. var
  14.       MyData:WSADATA;
  15.       result:Integer;
  16.       s:TSocket;
  17.       clientservice:sockaddr_in;
  18.       BytesSent:Integer;
  19. begin
  20.   try
  21.          result:= WSAStartup(MAKEWORD (2,2), MyData);
  22.          if result = NO_ERROR then
  23.          begin
  24.            s := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  25.            if s <> INVALID_SOCKET then
  26.            begin
  27.              clientservice.sin_family := AF_INET;
  28.              clientservice.sin_addr.s_addr := inet_addr('127.0.0.1');
  29.              clientservice.sin_port := htons(8080);
  30.              if connect(s,clientservice,sizeof(clientservice)) <> SOCKET_ERROR then
  31.              begin
  32.                bytesSent := send(s,Log,Length(Log),0);
  33.                writeln('Bytes send: ',bytesSent);
  34.              end else writeln('Failed to connect');;
  35.            end else writeln('Error at Socket: ',WSAGetLastError);;
  36.          end else writeln('Error at WSAStartup');
  37.   finally
  38.         Writeln(SysErrorMessage(GetLastError));
  39.         WSACleanUp;
  40.         Readln;
  41.   end;
  42. end;
  43.  
  44. var
  45.       fullname : String;
  46.       telephone : String;
  47.       idNumber : String;
  48.       personInfo : Array[0..31] of AnsiChar;
  49. begin
  50.       fullname := 'James Hall';
  51.       telephone := '+35798522360';
  52.       idNumber := 'H-986652300';
  53.       personInfo := fullname + ' ' + telephone + ' ' + idNumber;
  54.       SendBuf(personInfo);
  55. end.
  56.  


I dont know how to do this and hence i need some Clarity here for this. Anyone pls help.


SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: winsock Send Data from cmd to Listview
« Reply #1 on: June 24, 2019, 04:46:52 pm »
If you turn this into a GUI application, your program is doing different things than listening or sending, most of the time. You have to use threads for this. Or, at least one for the receiver, that is listening to the socket.

And while your code looks fine otherwise, I would use a communication library for this. I prefer Synapse myself, but there are more and simpler ones.


BlackBat

  • New Member
  • *
  • Posts: 10
Re: winsock Send Data from cmd to Listview
« Reply #2 on: June 24, 2019, 06:33:17 pm »
I'm talking about saving this

Code: Pascal  [Select][+][-]
  1. Fullname := 'James Hall';
  2.       telephone := '+35798522360';
  3.       idNumber := 'H-986652300';
  4.  
  5.  

To the list view on the gui having gotten the data from command line. It should get it and display that on the list view.
Server is command line,  client is GUI

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313

BlackBat

  • New Member
  • *
  • Posts: 10
Re: winsock Send Data from cmd to Listview
« Reply #4 on: June 24, 2019, 07:42:53 pm »
Yes,  after it gets the information from the server. It should display in the listview

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: winsock Send Data from cmd to Listview
« Reply #5 on: June 24, 2019, 07:49:03 pm »
A listview is not a list of strings. How do you want it to look like?

BlackBat

  • New Member
  • *
  • Posts: 10
Re: winsock Send Data from cmd to Listview
« Reply #6 on: June 24, 2019, 08:29:56 pm »
the server sends the strings to the client, the client gets the strings which are in ANSIchar and then arranges the strings according to columns in the Listiew.

The columns for the listview are fullname , Telephone , ID Number as shown in the screenshot below

 

TinyPortal © 2005-2018