有关socket编程的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 土拨鼠
  • 开始时间 开始时间

土拨鼠

Unregistered / Unconfirmed
GUEST, unregistred user!
在C环境下有这样一段代码:
unsigned long packlen, packlen1, lentmp;
.....
int netint = htonl(packlen);
write( sock, &netint, 4 );
lentmp=htonl(0x00000001);
write( sock, &lentmp, 4 );
在dephi环境下怎么写这段代码?请高手指教!谢谢!
 
var
packlen, packlen1, lentmp: DWORD;
netint: Integer;
begin
netint:= htonl(packlen);
write( sock, netint, 4 );
lentmp:= htonl($00000001);
write( sock, lentmp, 4 );
end;
 
楼上的,谢谢!不过,sock是下边定义的.用write( sock, netint, 4 );不能通过编译啊!还请指教.qq:444078
int sock;
....
sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
 
下边才定义的话你这个 c 程序就是错误的。
 
int sock;
.......
unsigned long packlen, packlen1, lentmp;

sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
.....
int netint = htonl(packlen);
write( sock, &netint, 4 );
lentmp=htonl(0x00000001);
write( sock, &lentmp, 4 );

程序是这样的!目的是程序通过socket连接,发送消息,每个消息头包括2个4字节的Integer,上边的程序就是发送这两个4字节整型的.现在要在delphi里实现
 
var
int sock;
packlen, packlen1, lentmp: DWORD;
netint: Integer;
begin
sock:= socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
.......
netint:= htonl(packlen);
write( sock, netint, 4 );
lentmp:= htonl($00000001);
write( sock, lentmp, 4 );
end;
 
接受答案了.
 
后退
顶部