程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 用API做的ServerSocket例子

用API做的ServerSocket例子

編輯:Delphi

program Server;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Winsock,
  Windows;

type
  TParam=record
    TrdId:integer;
    Sckt:TSocket;
  end;

//子線程函數
function ChildThrd(p:Pointer):LongInt;stdcall;
var
  PkLen:integer;
  ThreadId:integer;
  ASocket:TSocket;
  buf:array[0..63] of char; 
begin
  Result:=0;
  ThreadId:=TParam(p^).TrdId;
  ASocket:=TParam(p^).Sckt;
  Writeln(Thread +IntToStr(ThreadId)+ Create); 
  PkLen:=1;
  while PkLen>0 do
  begin
    FillChar(buf,SizeOf(buf),0);
    PkLen:=Recv(ASocket,buf,SizeOf(buf),0);  //接收數據
    Write(buf);
    if PkLen>0 then
    begin
      Send(ASocket,buf,PkLen,0);//發送數據
    end;
  end;
  CloseSocket(ASocket); //關閉socket
  Dispose(p);
  Writeln(Thread +IntToStr(ThreadId)+ End);
  Writeln(Socket +IntToStr(ASocket)+ DisConnect);
end;

//主程序
var
  MyWSA: WSAData;
  Svr,Clt: TSockAddr;
  NameLen:Integer;
  hSocket,s: TSocket;
  TimeOut:integer;
  ThreadCount:integer;
  hThread:Thandle;
  ThreadID:DWord;
  Param:^TParam;  
begin
  If WSAStartup(MAKEWORD(2,2), MyWSA) <> 0 Then  //初始化
  Begin
    WSACleanup;
    Writeln(WSAStartup() Fail);
    Exit;
  end;
  hSocket := Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //創建socket
  If hSocket = INVALID_SOCKET Then
  Begin
    WSACleanup;
    Writeln(Socket() Fail);
    Exit;
  End;
  Svr.sin_family := AF_INET;
  Svr.sin_port := htons(176); //server端口號為176
  Svr.sin_addr.S_addr := inet_addr(PChar(127.0.0.1));//server的ip地址為127.0.0.1
  If Bind(hSocket, Svr, SizeOf(Svr)) = SOCKET_ERROR Then  //綁定socket
  Begin
    CloseSocket(hSocket);
    WSACleanup;
    Writeln(Bind() Fail);
    Exit;
  end;
  if Listen(hSocket,256) = SOCKET_ERROR Then  //socket開始偵聽,最大同時連接數為256個
  begin
    CloseSocket(hSocket);
    WSACleanup;
    Writeln(Listen() Fail);
    Exit;
  end;
  TimeOut:=3000;
  ThreadCount:=0;
  Writeln(Listening......);
  while true do
  begin
    NameLen:=SizeOf(Clt);
    s:=Accept(hSocket,@Clt,@NameLen);//接受一個連接
    if s = SOCKET_ERROR Then
    begin
      CloseSocket(hSocket);
      WSACleanup;
      Writeln(Accept() Fail);
      Exit;
    end
    else
    begin
      Writeln(Socket +IntToStr(s)+ Connect);
      if SetSockOpt(s,SOL_SOCKET,SO_RCVTIMEO,PChar(@TimeOut),SizeOf(TimeOut))=SOCKET_ERROR then //設置接收超時為3秒
      begin
        CloseSocket(s);
        CloseSocket(hSocket);
        WSACleanup;
        Writeln(SetSockOpt() Fail);
        Exit;
      end;
      if SetSockOpt(s,SOL_SOCKET,SO_SNDTIMEO,PChar(@TimeOut),SizeOf(TimeOut))=SOCKET_ERROR then //設置發送超時為3秒
      begin
        CloseSocket(s);
        CloseSocket(hSocket);
        WSACleanup;
        Writeln(SetSockOpt() Fail);
        Exit;
      end;
      new(Param);
      Param^.TrdId:=ThreadCount;
      Param^.Sckt:=s;
      hThread:=CreateThread(nil,0,@ChildThrd,Param,0,ThreadID); //創建childthrd子線程
      if hThread=0 then
      begin
        Writeln(CreateThread() Fail);
      end
      else
      begin
        Inc(ThreadCount);
      end;
    end;
  end;
  CloseSocket(hSocket); //關閉socket
  WSACleanup;
end.

//源碼下載 http://ono.3322.org/mysoft/serversocket.zip

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved