// MyFirst.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Winsock2.h"
#pragma comment(lib,"ws2_32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    WORD wVersionRequested;
 WSADATA wsaData;
 int err;

wVersionRequested = MAKEWORD(1,1);

err = WSAStartup(wVersionRequested,&wsaData);
 if (err!=0)
 {
  return 0;
 }

if (LOBYTE(wsaData.wVersion)!=1||
  HIBYTE(wsaData.wVersion)!=1)
 {
  WSACleanup();
  return 0;
 }
 SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;
 addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
 addrSrv.sin_family=AF_INET;
 addrSrv.sin_port=htons(5101);

bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

listen(sockSrv,5);

SOCKADDR_IN addrClient;
 int len=sizeof(SOCKADDR);
 while(1)
 {
  SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
  char sendBuf[100];
    // sprintf_s(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
  /*send(sockConn,sendBuf,strlen(sendBuf)+1,0);*send函数是将指定长度的数据复制到TCP的系统缓冲区,然后系统缓冲区负责发送流数据到接收端的缓冲区
               recv负责从缓冲区复制数据到指定存储地址字节不一样又没事的,recv每次不一定接收固定的数据的*/
  char recvBuf[100];
  memset(recvBuf,0,sizeof(recvBuf));
  recv(sockConn,recvBuf,sizeof(recvBuf),0);
  printf("%s\n",recvBuf);
  memset(recvBuf,0,sizeof(recvBuf));
  recv(sockConn,recvBuf,sizeof(recvBuf),0);
  printf("%s\n",recvBuf);
  closesocket(sockConn);}
   
 return 0;
}

网上找的一段代码用C++重写了一遍然后用pascal写的客户端发送;

发现总是无法接受到正常的数据,在经过N边检查之后,才明白。

问题出现在客户端发送哪里有一个数据大小验证。

unit uSendMsg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,IdTCPClient,WinSock, IdBaseComponent, IdComponent,
  IdTCPConnection;

type
  TFSendMsg = class(TForm)
    btnsend: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Memo1: TMemo;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Button2: TButton;
    dlg: TOpenDialog;
    procedure btnsendClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Memo1Click(Sender: TObject);
    procedure Edit3Click(Sender: TObject);
  private
    { Private declarations }
    client: TIdTCPClient;
    procedure sendSplitText(client: TIdTCPClient; s: String);
    Procedure SendFile(IFileName:string);
  public
    { Public declarations }
  end;

var
  FSendMsg: TFSendMsg;

implementation
uses IdGlobal,IniFiles;
{$R *.dfm}

procedure TFSendMsg.btnsendClick(Sender: TObject);
var
  iLength: Integer;
  s,s1,sSendText: String;
  flag: Boolean;
  aText: string;
begin
  try
    if client.Connected() then
      client.Disconnect();
    if not client.Connected() then
    begin
      client.Host := trim(edit1.Text);
      client.Port := strtoint(trim(edit2.Text));
      client.Connect();
     // showmessage('连接成功');
    end;
  except on E: Exception do
  begin
      showMessage('连接主机失败,错误描述:'+ E.Message);
      exit;
  end;
  end;
  if not memo1.ReadOnly then
  begin
    try
       aText := trim(memo1.Text);
       if atext = '' then begin
          showmessage('No message send');
          exit;
       end;
       iLength := Length(aText);
       sSendText := intToStr(100000000 + iLength);
       Delete(sSendText, 1, 1);
       sSendText := sSendText + aText;
       if client.Connected() then
         sendSplitText(client,sSendText);
       showmessage('发送成功');
    except on E: Exception do
      ShowMessage('发送失败,错误描述:'+ E.Message);
    end;
  end else
  begin
  //  edit3.Text :=  'E:\华腾项目软件\sendmsg\a.txt';
    s1 := trim(edit3.Text);
    if s1 = '' then begin
      showmessage('请选择文件');
      exit;
    end;
    try
      if client.Connected() then
         sendfile(s1);
      showmessage('发送成功');
    except on E: Exception do
      ShowMessage('发送失败,错误描述:'+ E.Message);
    end;
  end;
end;

procedure TFSendMsg.Button1Click(Sender: TObject);
begin
  close;
end;

procedure TFSendMsg.Button2Click(Sender: TObject);
begin
  btnsend.Enabled := true;
  edit3.ReadOnly := false;
  memo1.ReadOnly := true;
  if dlg.Execute then
   edit3.Text := dlg.FileName;
end;

procedure TFSendMsg.Edit3Click(Sender: TObject);
begin
  btnsend.Enabled := true;
  edit3.ReadOnly := false;
  memo1.ReadOnly := true;
end;

procedure TFSendMsg.FormShow(Sender: TObject);
var
  s: string;
  ini: TIniFile;
begin
  client := TIdTCPClient.Create(nil);
  s := ExtractFilePath(Application.ExeName)+'info.ini';
  if not fileExists(s) then
  begin
    ShowMessage('未找到配置文件'+ s);
    Application.Terminate;
    exit;
  end;
  ini := TIniFile.Create(s);
  Edit1.Text := ini.ReadString('info','ip','');
  Edit2.Text := IntToStr(ini.ReadInteger('info','port',0));
  Edit3.Text := ini.ReadString('info','path','');
  ini.Free;
end;

procedure TFSendMsg.Memo1Click(Sender: TObject);
begin
  btnsend.Enabled := true;
  edit3.ReadOnly := true;
  memo1.ReadOnly := false;
end;

procedure TFSendMsg.SendFile(IFileName: string);
var
  iFileHandle:integer;
  iFileLen,cnt:integer;
  buf:array[0..4096] of byte;
  mdata: TBytes;
  cc: array[0..3000] of char;
  bb: array[0..3000] of char;
  aa: array[0..3000] of char;
  sText,sSendText: string;
  i,iLength,isum: integer;
begin
  iFileHandle:=FileOpen(IFileName,fmOpenRead);
  iFileLen:=FileSeek(iFileHandle,0,2);   //返回文件共多少个字符
  FileSeek(iFileHandle,0,0);  //指针指向文件开头

FillChar(cc, sizeof(cc),0);
  cnt := FileRead(iFileHandle,cc,iFileLen);

sSendText := intToStr(100000000 + iFileLen);
  Delete(sSendText, 1, 1);
  FillChar(aa, sizeof(aa),0);
  for i := 1 to 8 do
      aa[i-1] := sSendText[i];
  mdata := rawtobytes(aa, 8);
  client.Socket.Write(mdata);

isum := 0;
  while iFileLen > 1000 do
  begin
    FillChar(bb, sizeof(bb),0);
    for i := 0 to 1000 - 1 do
      bb[i] := cc[i+isum];
    mdata := rawtobytes(bb, 1000);
    client.Socket.Write(mdata);
    isum := isum + 1000;
    iFileLen := iFileLen - 1000;
  end;

if iFileLen > 0 then
  begin
    FillChar(bb, sizeof(bb),0);
    for i := 0 to iFileLen - 1 do
      bb[i] := cc[i+isum];
    mdata := rawtobytes(bb, iFileLen);
    client.Socket.Write(mdata);
  end;

FileClose(iFileHandle);
end;

procedure TFSendMsg.sendSplitText(client: TIdTCPClient; s: String);
var
  strStream:TStringStream;
  sText, s1: String;
  mdata: TBytes;
  cc: array[0..3000] of char;
  i: Integer;
begin
  sText := s;
  FillChar(cc, sizeof(cc),0);
  for i := 1 to 18 do
      cc[i-1] := sText[i];
  mdata := rawtobytes(cc, 18);
  client.Socket.Write(mdata);
  Delete(sText,1,18);
  while Length(sText) > 1000 do
  begin
    s1 := Copy(sText,1,1000);
    FillChar(cc, sizeof(cc),0);
    for i := 1 to Length(s1) do
      cc[i-1] := s1[i];
    mdata := rawtobytes(cc, 1000);
    client.Socket.Write(mdata);
    Delete(sText,1,1000);
  end;
  if Length(sText) > 0 then
  begin
    FillChar(cc, sizeof(cc),0);
    for i := 1 to Length(sText) do
      cc[i-1] := sText[i];
    mdata := rawtobytes(cc, Length(sText));
    client.Socket.Write(mdata);
  end;
end;

C++和pascal之间的通信的更多相关文章

  1. android中四大组件之间相互通信

    好久没有写有关android有关的博客了,今天主要来谈一谈android中四大组件.首先,接触android的人,都应该知道android中有四大组件,activity,service,broadca ...

  2. angularJS中directive与directive 之间的通信

    上一篇讲了directive与controller之间的通信:但是我们directive与directive之间的通信呢? 当我们两个directive嵌套使用的时候怎么保证子directive不会被 ...

  3. 使用reflux进行react组件之间的通信

    前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...

  4. Fragment之间的通信(四)

    自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...

  5. react native 之子组件和父组件之间的通信

    react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...

  6. js组件之间的通信

    应用场景: 1.在刷微博的时候,滑到某个头像上,会出现一张usercard(用户名片), 微博列表区有这个usercard, 推荐列表也有这个usercard,评论区也有. 2.在网上购物时,购物车安 ...

  7. react8 组件之间的通信

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  8. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  9. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...

随机推荐

  1. iOS人机界面指南(翻译)

    本文源自于苹果开发者网站的文章iOS Human Interface Guidelines,内容比较多,此处仅仅是部分笔记.

  2. Less 导入命令 @import

    在这个less文件上想导入另一个less文件, 连在同级的文件里直接可用文件名 @import url('css.less')或@import rul(css) 连下级的文件 @import url( ...

  3. 【转】CSS实现兼容性的渐变背景(gradient)效果

    一.有点俗态的开场白 要是两年前,实现“兼容性的渐变效果”这个说法估计不会被提出来的,那个时候,说起渐变背景,想到的多半是IE的渐变滤镜,其他浏览器尚未支持,但是,在对CSS3支持日趋完善的今天,实现 ...

  4. jQuery ui datepicker 日历转中文

    做个笔记,以后详解 jQuery(function($){ $.datepicker.regional['zh-CN'] = { closeText: '关闭', prevText: '<上月' ...

  5. MongoDB与php的配合使用 【windows版】

    通过学习了如何使用和部署MongoDB,尝试了一下如何将mongodb应用到php的程式中去. 1.预备工作 首先得准备好mongodb,并按照相关方法部署以及服务能正常运行中. 对于初学者,可以参考 ...

  6. 一个好用的PHP验证码类

    分享一个好用的php验证码类,包括调用示例. 说明: 如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数.字体的位置在C盘下 ...

  7. 当年的笔记_apache配置虚拟主机

    下午需要,在网上找了一堆,没找到合适的,翻出来自己当年的笔记,还是自己记的容易理解. 解决方案1:通过端口来区分 1>添加一个虚拟主机1.在d盘下新建www目录,如:d:/www. 2.修改ht ...

  8. mysql中log

    mysql的主从模式配置 1.改主库配置文件:D:\Program Files\MySQL\MySQL Server 5.5(my.ini/my.cnf)在下面加入 [mysqld] log=c:/a ...

  9. 原始套接字的简单tcp包嗅探

    原始套接字 sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_TCP); while(1) { data_size = recvfrom(sock_raw ...

  10. 22 高级SQL特性

    1.约束 为正确地进行关系数据库设计,需要一种方法来保证只在表中插入合法的数据.例如,如果Orders表存储订单信息,OrderItems表存储订单详细内容,应该保证Orderitems中引用的任何订 ...