zoukankan      html  css  js  c++  java
  • 通过ClientSocket 与 ServerSocket实现简单的聊天功能.

    代码清单:

    以下程序同时扮演了客户端与服务端.

    添加一个button,两个Edit,一个ClientSocket,一个ServerSocket.

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    unit Unit1;

    interface

    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Win.ScktComp,Winapi.WinSock,
      Vcl.OleCtrls, SHDocVw, Vcl.StdCtrls;

    type
      TForm1 = class(TForm)
        ClientSocket1: TClientSocket;
        Edit1: TEdit;
        Button1: TButton;
        ServerSocket1: TServerSocket;
        Edit2: TEdit;
        Button2: TButton;
        procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
        procedure ServerSocket1ClientRead(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    procedure TForm1.Button1Click(Sender: TObject); //OnClick事件

    begin
      ClientSocket1.Open; //打开ClientSocket,并连接服务端.
    end;

    procedure TForm1.ClientSocket1Connect(Sender: TObject;Socket: TCustomWinSocket); //OnConnect事件
    begin
        ClientSocket1.Socket.SendText(Edit1.Text);  //把Edit1中的消息发送给服务端.
      ShowMessage('成功连接.');
        ClientSocket1.Close;   //完成后关闭连接.
    end;

    procedure TForm1.ServerSocket1ClientRead(Sender: TObject;Socket: TCustomWinSocket); //OnClientRead事件
    begin
      Edit2.Text := Socket.ReceiveText;  //服务端接收到由客户端发来的消息,并且显示在Edit2上.
    end;

    end.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------

    现在说说ClientSocket和ServerSocket的几个重要属性.

      ClientSocket :

                         1. Active   Boolean型属性,为真时便是打开端口与服务器连接(等同于ClientSocket.Open),为假时就断开与服务器的连接(等同于ClientSocket.Close).

                         2.Address String型属性,你要连接的IP地址.

                         3.ClientType  连接方式, ctNonBlocking表示无阻断方式.ctBlocking表示以阻断方式连接.

                         4.Host  String型属性,你要连接的域名,如果Host与Address同时设置了,那么默认选择Host的参数.

                         5.Prot 整型属性, 你要使用的端口号,不能使用已经被占用的端口号.

                         6.SerVice String类型属性,设置常用的端口号,例如http ftp 等等. 如果同时设置了Prot属性,那么默认选择Service的参数.

    ServerSocket :

                          1.  Active 等于ClientSocket的属性意思.

                          2. Prot 等于ClientSocket的属性意思.

                          3. ServerType 等于ClientSocket的属性意思.

                          4. Prot 等于ClientSocket的属性意思.

                          5. Service 等于ClientSocket的属性意思.

  • 相关阅读:
    Educational Codeforces Round 85 D. Minimum Euler Cycle(模拟/数学/图)
    Educational Codeforces Round 85 C. Circle of Monsters(贪心)
    NOIP 2017 提高组 DAY1 T1小凯的疑惑(二元一次不定方程)
    Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)
    Educational Codeforces Round 85 A. Level Statistics(水题)
    IOS中的三大事件
    用Quartz 2D画小黄人
    strong、weak、copy、assign 在命名属性时候怎么用
    用代码生成UINavigationController 与UITabBarController相结合的简单QQ框架(部分)
    Attempting to badge the application icon but haven't received permission from the user to badge the application错误解决办法
  • 原文地址:https://www.cnblogs.com/mdnx/p/2592891.html
Copyright © 2011-2022 走看看