The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Delphi Programming
|
Delphi 2010 WINSOCK ACCEPT
Discuss Delphi 2010 WINSOCK ACCEPT in the Delphi Programming forum on Dev Shed. Delphi 2010 WINSOCK ACCEPT Delphi Programming forum discussing Delphi related topics including Kylix, C++ Builder, and more. Delphi is a high-performance language, originally based on the PASCAL language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 24th, 2012, 04:04 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
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.
|

September 24th, 2012, 04:51 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
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
|

September 25th, 2012, 12:04 AM
|
|
Contributing User
|
|
Join Date: May 2012
Posts: 134
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|