用pascal写的最简单的winsock程序

时间:2010-02-01 13:19:06  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:70" cellspacing="0" bordercolordark="#ffffff" cellpadding="5" width="99%" bgcolor="#ffffc0" border="1">server1.pas:program server1;uses windows,winsock;const myport=1414;//预定义口{type sockaddr_in = record  case Integer of   0: (sin_family: u_short;    ...
  正文:我把socket编程教程的第一个例子改写了一下
服务器接受客户信息,再反送回去

实际上是直接调用win api
Delphi4下用命令行编译
dcc32 -cc server1.pas
用windows自带的telnet测试


server1.pas:
program server1;

uses windows,winsock;

const myport=1414;//预定义口

{type
 sockaddr_in = record
  case Integer of
   0: (sin_family: u_short;
     sin_port: u_short;
     sin_addr: TInAddr;
     sin_zero: array[0..7] of Char);
   1: (sa_family: u_short;
     sa_data: array[0..13] of Char)
 end;
 TSockAddrIn = sockaddr_in;
 photoshop/ target=_blank class=infotextkey>psOCKADDR = ^TSockAddr;
 TSockAddr = sockaddr_in;

 WSAData = record // !!! also WSDATA
  wVersion: word;
  wHighVersion: Word;
  szDescription: array[0..WSADESCRIPTION_LEN] of Char;
  szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
  iMaxSockets: Word;
  iMaxUdpDg: Word;
  lpVendorInfo: PChar;
 end;
 TWSAData = WSAData;

end
}


var str:string;
  s,ns:TSocket;//type:u_int|integer;
  ver:word;
  rece:TWSAData;//type:WSAData;
  wsstatus:integer;//winsocket_status;
  i,pktlen:integer;
  szd:array[0..WSADESCRIPTION_LEN] of Char;
  szs:array[0..WSASYS_STATUS_LEN] of Char;
  server:TSockAddr;//tpye:sockaddr_in
  client:PSockAddr;//type:^sockaddr_in
  namelen:PInteger;//type:^Integer;
  buf:array[0..1023] of char;

begin
  ver:=514;
  write('WSAStartup = ');
  try
    wsstatus:=WSAStartup(ver,rece);
    //初始化winsock;在windows下是必须的过程
writeln(wsstatus);//winsock状态,0代表成功
if wsstatus=0 then
    begin
      writeln;
      writeln('*********************************************');
      //以下显示winsock状态
write('wVersion = ');
      writeln(rece.wVersion);
      write('wHighVersion = ');
      writeln(rece.wHighVersion);
      write('iMaxSockets = ');
      writeln(rece.iMaxSockets);
      write('iMaxUdpDg = ');
      writeln(rece.iMaxUdpDg);
      writeln('szDescription is ');
      for i:=0 to WSADESCRIPTION_LEN do write(szd[i]);
      writeln;
      writeln('szSystemStatus is ');
      for i:=0 to WSASYS_STATUS_LEN do write(szs[i]);
      writeln;
      writeln('***********************************');
    end;
    
    write('socket = ');
    s:=socket(AF_INET,SOCK_STREAM,0);
    //第一步,建立服务器的socket,0表示成功
writeln(s);
    if s<0 then exit;
    
    write('bind = ');
    server.sin_family := AF_INET;
    server.sin_port := htons(myport);
    server.sin_addr.s_addr := INADDR_ANY;
    wsstatus:=bind(s,server,sizeof(server));
    //第二步,绑定,0表示成功

"用pascal写的最简单的winsock程序"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/12725.html


关键字:

关于《用pascal写的最简单的winsock程序》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td