Recent

Author Topic: winsock in pascal  (Read 10803 times)

shonay

  • Full Member
  • ***
  • Posts: 169
winsock in pascal
« on: June 29, 2015, 08:33:02 pm »
Good evening,
I been practising Winsock (Windows Socket) I been able to successfully do this in c++ now I wonna try the same in pascal, using lazarus. From there try to connect to it room a php server side app

I been looking for a way to do this in pascal, source code of c++ looks like this
Code: [Select]
#pragma comment (lib,"ws2_32")

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <Winsock.h>

void main()
{
   WSADATA wsaData;
   int result = WSAStartup(MAKEWORD (2,2), &wsaData);
   if(result !=NO_ERROR)
{
  printf("Error at WSAStartup()");
}
   SOCKET s;
   s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   if(s == INVALID_SOCKET)
{
  printf("Error at Socket (): %ld\n",WSAGetLastError());
  WSACleanup();
return;
}
  sockaddr_in clientservice;
  clientservice.sin_family = AF_INET;
  clientservice.sin_addr.s_addr = inet_addr("127.0.0.1");
  clientservice.sin_port = htons(80);
 
  if(connect(s,(SOCKADDR*)&clientservice,sizeof(clientservice))==SOCKET_ERROR)
{
  printf("Failed To Connect\n");
  WSACleanup();
  return;
}
  int bytesSent;
  char sendbuf[32] = "la la la la la la la :)";

  bytesSent = send(s,sendbuf,strlen(sendbuf),0);
  printf("Bytes Sent: %ld\n",bytesSent);

  return;
  system("PAUSE");
  }
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

Dr.Theopolis

  • Jr. Member
  • **
  • Posts: 69
Re: winsock in pascal
« Reply #1 on: June 30, 2015, 11:26:02 pm »
My preference is to use Synapse (http://wiki.freepascal.org/Synapse). It includes demos which will show you how to do what you want to do.

Other options are listed at http://wiki.freepascal.org/Networking_libraries

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: winsock in pascal
« Reply #2 on: July 01, 2015, 06:11:07 am »
I been looking for a way to do this in pascal, source code of c++ looks like this
Apart from fp prefix to the BSD sockets functions name when calling, everything should be identical for ANY language to access BSD sockets. So you just need to convert C++ to Pascal syntax. And no, no one sane here is gonna do it for you.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: winsock in pascal
« Reply #3 on: July 01, 2015, 10:04:20 am »
(fp prefix is sockets unit, he uses winsock directly, so probably no fp-. Note there are two, winsock and winsock2)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: winsock in pascal
« Reply #4 on: July 01, 2015, 10:12:28 am »
[EDIT] With hindsight remove ugly exits:

Ok, I bite:

program sendtest;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  windows,
  winsock;
var
  MyData:WSADATA;
  Rslt:Integer;
  s:TSocket;
  SendBuf:Array[0..31] of AnsiChar;
  clientservice:sockaddr_in;
  BytesSent:Integer;
begin
 try
   Rslt := WSAStartup(MAKEWORD (2,2), MyData);
   if Rslt = NO_ERROR then
   begin
    s := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if s <> INVALID_SOCKET then
    begin
      clientservice.sin_family := AF_INET;
      clientservice.sin_addr.s_addr := inet_addr('127.0.0.1');
      clientservice.sin_port := htons(8080);
      if connect(s,clientservice,sizeof(clientservice)) <> SOCKET_ERROR then
       begin
        sendbuf := 'la la la la la la la :)';
        bytesSent := send(s,sendbuf,Length(sendbuf),0);
        writeln('Bytes send: ',bytesSent);
      end else writeln('Failed to connect');
    end else writeln('Error at Socket: ',WSAGetLastError);
   end else writeln('Error at WSAStartup');
 finally
  Writeln(SysErrorMessage(GetLastError));
  WSACleanUp;
  Readln;
 end;
end.

Note in this example my webserver is running on port 8080.
« Last Edit: July 01, 2015, 10:56:30 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: winsock in pascal
« Reply #5 on: July 01, 2015, 01:52:29 pm »
(fp prefix is sockets unit, he uses winsock directly, so probably no fp-. Note there are two, winsock and winsock2)
Ah, sorry, missed that.

Michaela Joy

  • New Member
  • *
  • Posts: 24
Re: winsock in pascal
« Reply #6 on: July 08, 2015, 02:32:57 am »
I have used this with Delphi, so it stands a chance of working with FPC.

http://www.overbyte.be/frame_index.html

It uses WinSock as well.

Hope this helps.

:MJ

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: winsock in pascal
« Reply #7 on: July 08, 2015, 09:51:36 am »
Older versions of ICS worked fine with FPC.  It actually was the first socket suite that worked on Windows I guess.

Michaela Joy

  • New Member
  • *
  • Posts: 24
Re: winsock in pascal
« Reply #8 on: July 11, 2015, 05:33:31 pm »
@marcov: I believe you're correct. I'm sure that it's a matter of preference, but personally, I prefer ICS's non-blocking socket handling over Indys' blocking implementation.

@shonay: You'll need to set up a worker thread and handle inet communications there. If for reference only, grab a copy of ICS and take a look at the examples.
If you want to use a specific function, look in the unit itself, find the function you need, and see how He wraps it.

Please let us know how you fare.

:MJ

sam707

  • Guest
Re: winsock in pascal
« Reply #9 on: July 12, 2015, 04:00:42 pm »
https://lnet.wordpress.com

i do use lNet components library for Lazarus.
there is à choice of events handlers that are easy workarounds to blocking and nonblocking sockets use.

unlike synapse and indy (that are oldschool), nonblocking sockets are available by default, and winsock headers have been patched to work with HUGE amout of open connections.

to me, lNet is the best library ever for free pascal basic sockets programming.
simple to understand, efficient, well structured, thread safe, fast, works on linux/windows, IPv4/IPv6... supports SSL sessions also

give it a try!
« Last Edit: July 12, 2015, 07:16:32 pm by sam707 »

 

TinyPortal © 2005-2018