贴子贴了好几天,难道没有人知道?!请哪位指点:delphi编的程序怎样与vb编的程序通过ip来通讯? (200分)

  • 主题发起人 leijh001
  • 开始时间
L

leijh001

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi程序中有clientsocket,nmmsg,nmmsgserv等控件,vb程序中有winsock,各项参数都设置好了,两个程序分别在不同的电脑上运行,vb发送数据给delphi时,delphi提示接收的数据不是一个有效的整数,而delphi发送给vb进却没反应,请问怎样做才能使两者正常通讯?
若回答有理,定当重谢。
 
没有问题,你发送的数据的格式有问题,只要都是ANSI字符就没有问题,即使是UNIX
也可以正常和DELPHI做的程序通讯,仔细检查一下你发送数据的格式
 
先测试收发字符串
-----
http://www.8421.org
 
把你觉得有问题的源程序贴出来(Dephi &
VB)
 
delphi源程序:
unit msgdem;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Psock, NMMSG, ComCtrls, ScktComp, IdBaseComponent,
IdComponent, IdTCPServer, IdHostnameServer, IdTCPConnection, IdTCPClient,
IdWhois, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
NMMsg1: TNMMsg;
NMMSGServ1: TNMMSGServ;
Label1: TLabel;
Label2: TLabel;
Edit3: TEdit;
StatusBar1: TStatusBar;
Label3: TLabel;
ClientSocket1: TClientSocket;
Edit2: TEdit;
Panel2: TPanel;
Panel3: TPanel;
ListBox2: TListBox;
ListBox1: TListBox;
Splitter1: TSplitter;
SaveDialog1: TSaveDialog;
BitBtn1: TBitBtn;
procedure Panel1Resize(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject;
var Key: Char);
procedure NMMSGServ1MSG(Sender: TComponent;
const sFrom, sMsg: String);
procedure NMMsg1MessageSent(Sender: TObject);
procedure NMMsg1Connect(Sender: TObject);
procedure NMMsg1ConnectionFailed(Sender: TObject);
procedure NMMsg1Disconnect(Sender: TObject);
procedure NMMsg1HostResolved(Sender: TComponent);
procedure NMMsg1Status(Sender: TComponent;
Status: String);
procedure NMMsg1InvalidHost(var handled: Boolean);
procedure NMMSGServ1ClientContact(Sender: TObject);
procedure NMMSGServ1Status(Sender: TComponent;
Status: String);
procedure FormActivate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Panel1Resize(Sender: TObject);
begin
Edit1.Left := 8;
Edit1.Width := Panel1.Width - 16;
Label1.Left := 8;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject;
var Key: Char);
begin
If Key = #13 then
begin
NMMsg1.Host := Edit3.Text;
NMMsg1.FromName := Edit2.Text;
NMMsg1.PostIt(Edit1.Text);
end;
end;

procedure TForm1.NMMSGServ1MSG(Sender: TComponent;
const sFrom,
sMsg: String);
begin
if ListBox1.Items.IndexOf(sFrom)=-1 then
ListBox1.Items.Add(sFrom);
ListBox2.Items.Add (sMsg);
end;

procedure TForm1.NMMsg1MessageSent(Sender: TObject);
begin
ShowMessage('Message sent!');
end;

procedure TForm1.NMMsg1Connect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connected';
end;

procedure TForm1.NMMsg1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;

procedure TForm1.NMMsg1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'Disconnected';
end;

procedure TForm1.NMMsg1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'Host Resolved';
end;

procedure TForm1.NMMsg1Status(Sender: TComponent;
Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;

procedure TForm1.NMMsg1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
begin
NMMsg1.Host := TmpStr;
Handled := TRUE;
end;
end;

procedure TForm1.NMMSGServ1ClientContact(Sender: TObject);
begin
NMMsgServ1.ReportLevel := Status_Basic;
NMMsgServ1.TimeOut := 90000;
StatusBar1.SimpleText := 'Client connected';
end;

procedure TForm1.NMMSGServ1Status(Sender: TComponent;
Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;

procedure TForm1.FormActivate(Sender: TObject);
var
ComputerName :array[0..MAX_COMPUTERNAME_LENGTH + 1] of char;
sComputerName :string;
lpSize :DWORD;
begin
lpSize := MAX_COMPUTERNAME_LENGTH + 1;
if GetComputerName(@ComputerName ,lpSize) then
begin
sComputerName := ComputerName;
end
else
begin
sComputerName := '计算机名非法!';
end;
Edit2.Text :=sComputerName;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
if savedialog1.FileName<>'' then
begin
SaveDialog1.Title := '保存日记到文本文件 ';
ListBox2.Items.SaveToFile(savedialog1.filename+'.doc');
listbox2.Items.Clear;
end;
end;
end.

vb源程序:
Option Explicit
Private Sub cmdConnect_Click()
lblStatus.Caption = "Connection to: " &amp;
txtRemoteIP.Text &amp;
":" &amp;
txtRemotePort.Text ' Sets the status
Winsock1.Connect txtRemoteIP.Text, Val(txtRemotePort.Text) ' Connects the client to ip, val(port)
End Sub
Private Sub cmdDisconnect_Click()
Winsock1.Close ' Closes the connection
cmdConnect.Enabled = True ' Changes the buttons status
cmdDisconnect.Enabled = False
cmdListen.Enabled = True
cmdSend.
Enabled = False
lblStatus.Caption = "Disconnected" ' Set the new status message
End Sub
Private Sub cmdExit_Click()
Winsock1.Close ' Closes the connection
End ' Exits
End Sub
Private Sub cmdListen_Click()
Winsock1.LocalPort = Val(txtLocalPort.Text) ' Sets the port to listen on
Winsock1.Listen ' Opens port
lblStatus.Caption = "Listening on port: " &amp;
txtLocalPort.Text ' Updates status
cmdConnect.Enabled = False ' Changes the buttons status
cmdDisconnect.Enabled = True
cmdListen.Enabled = False
cmdSend.
Enabled = False
End Sub
Private Sub cmdSend_Click()
Winsock1.SendData txtOut.Text ' Sends the data to the other end
End Sub
Private Sub Form_Load()
cmdDisconnect.Enabled = False ' Sets the buttons
cmdSend.
Enabled = False
End Sub
Private Sub txtIn_Change()
txtIn.SelStart = Len(txtIn.Text) ' Makes it so it scrollsdo
wn when more text is added
End Sub
Private Sub Winsock1_Connect()
lblStatus.Caption = "Connected: " &amp;
txtRemoteIP.Text &amp;
":" &amp;
txtRemotePort.Text ' Sets status
cmdConnect.Enabled = False ' Updates buttons
cmdDisconnect.Enabled = True
cmdListen.Enabled = False
cmdSend.
Enabled = True
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If (Winsock1.State <> sckClosed) then
Winsock1.Close ' If not closed then
call close method to cleanup socket status
Winsock1.LocalPort = 0 ' Clear the localport
Winsock1.Accept requestID ' Accept the incoming connection
txtRemoteIP.Text = Winsock1.RemoteHostIP ' Tells you their ip
txtRemotePort.Text = Winsock1.RemotePort ' Tells you their port
lblStatus.Caption = "Connected: " &amp;
txtRemoteIP.Text &amp;
":" &amp;
txtRemotePort.Text ' Updates status
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim INCOMING ' Sets the varible
Winsock1.GetData INCOMING, vbString ' Puts the incoming data into the varible
txtIn.Text = txtIn.Text &amp;
INCOMING ' Adds the varible to the text box
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
lblStatus.Caption = "Error: " &amp;
Description ' Descibes the error in the status box
End Sub
 
我前段时间刚做过的,不过我做的是delphi部分,其他人做的是VB部分。
delphi里用serversocket,VB里用socket就可以了,两者的用法差不多,
没有什么需要特别设置的啊
 
当然要用 delphi的 socket通讯
非常好用, 我以前用的是 vc 和 delphi 通讯
还是多看看书把, 搞通最简单的socket通讯, 劝你先分别在每个系统都搞通程序
再试vb和delphi的通讯, 有是两种语言数据类型试有区别的, 程序需要静心思考
 
不要用 NMMSGServ 和 NMMSG 和vb通信。
NMMSGServ 只能和 NMMSG 通信,因为他们消息传递中间使用了一些自定义的信息交互的协议,
传递一些信息,你的vb并没有用这种格式通信
你要使用纯的socket控件,比如 TClientSocket(delphi发起) 或者 TServerSocket(vb发起)
 
不知道。
顶顶。。。。。。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部