Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 24th, 2012, 04:04 AM
crossmark crossmark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 crossmark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 24 sec
Reputation Power: 0
Delphi 2010 WINSOCK ACCEPT

Hello! Help! I beg you.
I do not know where to look and why do not work.
I sketched a small server and client code.
A socket is created, the server is listening on the port.
Client also connect.
This shows TCPView (client ESTABLISHED. Also tried through telnet.
Connection occurs.
But why is the server does not work function ACCEPT.
This code program go to END. And it should stay on ACCEPT. Passes ACCEPT and outputs the result -1.
Tried ACCEPT place in the cycle. constantly gives -1 and does not send the message HELLO. What's wrong? Everywhere such examples. Help.
Code:
procedure TForm1.Button2Click(Sender: TObject);
var sock_client,sock_server:integer;
    wsa_Data:WSADATA;
    err:integer;
    addrClient,addrServ:sockaddr_in;
    buf:array[1..1024] of pansichar;
begin
   //WSA
   err:=WSAStartup($101,wsa_Data);
   if err=SOCKET_ERROR then
   showmessage('WSA_START_UP '+IntToStr(GetlastError));
   //create socket
   sock_server:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   if sock_server<0 then
   begin
       showmessage('socket not create');
       exit;
   end;
   //set
   addrServ.sin_family:=AF_INET;
   addrServ.sin_addr.S_addr:=htonl(INADDR_ANY);
   addrServ.sin_port:=htons(5060);
   // link bind
   err:=bind(sock_server,&addrServ,sizeof(addrServ)) ;
   if err=SOCKET_ERROR then
      begin
       showmessage('bind error');
       exit;
   end;
   //listen socket
   listen(sock_server,5);

  buf[1]:='HELLO';
  sock_Client:=accept(sock_server,pointer(@addrClient),pointer(sizeof(addrClient)));
  send(sock_Client,buf,sizeOf(buf),0);
procedure TForm1.Button2Click(Sender: TObject);
var sock_client,sock_server:integer;
    wsa_Data:WSADATA;
    err:integer;
    addrClient,addrServ:sockaddr_in;
    buf:array[1..1024] of pansichar;
begin
   //WSA
   err:=WSAStartup($101,wsa_Data);
   if err=SOCKET_ERROR then
   showmessage('WSA_START_UP '+IntToStr(GetlastError));
   //create socket
   sock_server:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   if sock_server<0 then
   begin
       showmessage('socket not create');
       exit;
   end;
   //set
   addrServ.sin_family:=AF_INET;
   addrServ.sin_addr.S_addr:=htonl(INADDR_ANY);
   addrServ.sin_port:=htons(5060);
   // link bind
   err:=bind(sock_server,&addrServ,sizeof(addrServ)) ;
   if err=SOCKET_ERROR then
      begin
       showmessage('bind error');
       exit;
   end;
   //listen socket
   listen(sock_server,5);

  buf[1]:='HELLO';
  sock_Client:=accept(sock_server,pointer(@addrClient),pointer(sizeof(addrClient)));
  send(sock_Client,buf,sizeOf(buf),0);
end;


and another question, how to use buf: array [1 .. 255] of char. lot function are essential to take or give result BUF. I only issued the first character.

Reply With Quote
  #2  
Old September 24th, 2012, 04:51 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
I have not done Winsock code for a while but I believe the buffers should be declared as
ansichar NOT pansichar.

Given the declaration as pansichar, I think that buf[1] contains 'HELLO' not 'H' as I suspect you imagine.

In addition I do not see where you initialize the buffer, so all other elements will contain random data.

Also, I recommend (although I do not think it affects whether the code works) that you declare any such buffers as zero based.

Code:
buf:array[0..1023] of ansichar;


HTH - Clive

Reply With Quote
  #3  
Old September 25th, 2012, 12:04 AM
Luthfi Luthfi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 134 Luthfi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 26 m
Reputation Power: 2
Variable buf was never been initialized. And the declaration is wrong, for your purpose. You should declare buf either as:
Code:
var buf: PAnsiChar;


or

Code:
var buf: array [1..1024] of AnsiChar;


or
Code:
var buf: RawByteString;


Code:
var buf: AnsiString;




Initialize the first approach by using GetMem(Buf, 1024), which later you must finalized with FreeMem.

The second approach does not require initialization of finalization since Delphi will allocate and deallocate the required memory upon entering and leaving the routine.

The third and fourth approach only need initialization, i.e. setting the length of the string. I.e.: SetLength(Buffer, 1024).

Of course there will be small differences in using the buffer between the first approach and the rest.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Delphi 2010 WINSOCK ACCEPT

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap