Socket编程问题(10分)

  • 主题发起人 主题发起人 smallbee
  • 开始时间 开始时间
S

smallbee

Unregistered / Unconfirmed
GUEST, unregistred user!
错误为:
[Error] Unit1.pas(56): Incompatible types: 'Unit1.Integer' and 'System.Integer'
[Error] Unit1.pas(57): '(' expected but ',' found

请问如何修改啊?
var
s,newsock:TSocket;
ser_addr:TSockAddrIn;
remote_addr:PSockAddr;
leng:^Integer;
wsa:WSAData;
wVersionRequested:WORD;
begin
new(leng);
wVersionRequested := MAKEWORD( 2, 0 );
WSAStartup(wVersionRequested,wsa);
s := socket(AF_INET,SOCK_STREAM,0);
ser_addr.sin_family := AF_INET;
ser_addr.sin_addr.S_addr := htonl(INADDR_ANY);
ser_addr.sin_port := htons(1234);

bind(s,ser_addr,sizeof(ser_addr));

listen(s,0);
while 1>0 do
begin
leng^ := sizeof(remote_addr);
newsock := accept(s,remote_addr,leng);
recv(newsock,buf,leng^,0);
closesocket(newsock);
end;
closesocket(s);
WSACleanup();
Dispose(leng);
end;
 
1: 你用到Delphi的关键字了
2: 检查一下语法
 
var
s,newsock:TSocket;
ser_addr:TSockAddrIn;
remote_addr:PSockAddr;
leng:Integer;
wsa:WSAData;
wVersionRequested:WORD;
buf:array[0..1024]of char;
begin
//new(leng);
wVersionRequested := MAKEWORD( 2, 0 );
WSAStartup(wVersionRequested,wsa);
s := socket(AF_INET,SOCK_STREAM,0);
ser_addr.sin_family := AF_INET;
ser_addr.sin_addr.S_addr := htonl(INADDR_ANY);
ser_addr.sin_port := htons(1234);

bind(s,ser_addr,sizeof(ser_addr));

listen(s,0);
while 1>0 do
begin
leng:= sizeof(remote_addr);
newsock := accept(s,remote_addr,@leng);
recv(newsock,buf,leng,0);
closesocket(newsock);
end;
closesocket(s);
WSACleanup();
//Dispose(leng);
end;
 
谢谢tang1234,你的答案我很喜欢。这里将我的调试好了的服务器程序贴出来,

对这个问题做一个结束。谢谢各位。smallbee

program tcpserver;

{$APPTYPE CONSOLE}

uses
SysUtils, Windows,
winsock;

var
s,newsock:TSocket;
ser_addr:TSockAddrIn;
remote_addr:PSockAddr;
leng:Integer;
wsa:WSAData;
wVersionRequested:WORD;
buf:array[0..128]of char;
buf2:array[0..128]of char;
begin
//new(leng);
wVersionRequested := MAKEWORD( 2, 0 );
WSAStartup(wVersionRequested,wsa);
s := socket(AF_INET,SOCK_STREAM,0);
ser_addr.sin_family := AF_INET;
ser_addr.sin_addr.S_addr := htonl(INADDR_ANY);
ser_addr.sin_port := htons(4000);

bind(s,ser_addr,sizeof(ser_addr));

listen(s,0);
while 1>0 do
leng:= sizeof(remote_addr);
newsock := accept(s,remote_addr,@leng);
begin
if recv(newsock,buf,sizeof(buf),0)>0 then
begin
writeln('Client Data:',buf);
Readln(buf2);
send(newsock,buf2,sizeof(buf2),0);
end;
closesocket(newsock);
end;

closesocket(s);
WSACleanup();
sleep(2000);
end.
 
多人接受答案了。
 

Similar threads

后退
顶部