appfirst来此接分 ( 积分: 200 )

  • 主题发起人 主题发起人 hhero
  • 开始时间 开始时间
H

hhero

Unregistered / Unconfirmed
GUEST, unregistred user!
晕,要再开4贴
 
晕,要再开4贴
 
你是要从服务端发信息到客户端?
 
先从C发消息到S,S收到并处理后再发还C,C接到后进行处理
还有你昨天的代码编译不通
 
winsock2.pas 的下载地址http://jungla.dit.upm.es/~bti/files/winsock2.pas
我给你调试通过了昨天没有DELPHI:).
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock2, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private

{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

Type
TSocketThread=class(TThread)
private
Memo:TMemo;
tBuff:Array[0..8196] of Byte;
procedure WriteMemo;
protected
procedure Execute;override;
public
Constructor Create(vMemo:TMemo);
end;

Var
SocketThread:TSocketThread;
//客户端
procedure ClientOpen(Var Buff; Len:Integer);
var
buf:TWSABUF;
SendBytes:DWORD;
tBuff:Array[0..8196] of Byte;
hRet:Integer;
Data: TWSAData;
Addr: TSockaddrIn;
m_Socket:TSocket;
begin
WSAStartup(MakeWord(2,2), Data);
m_Socket := WSASocket(AF_INET, SOCK_STREAM ,0,nil,0,0);
if m_Socket=INVALID_SOCKET then
Exit;
Addr.sin_family := AF_INET;
Addr.sin_addr.s_addr := inet_addr('127.0.0.1');
Addr.sin_port := htons(8000);
connect(m_Socket,@Addr,Sizeof(Addr));
Buf.Buf:=@tBuff;
Buf.Len:=8196;
{做你的处理部分}
CopyMemory(@tBuff,PChar(@Buff),Len);
hRet:=WSASend(m_Socket,@Buf,1,@SendBytes,0,nil,nil);
if hRet=Socket_Error then
Exit;
{做你的处理部分}
{从服务端接收数据使用WSARecv使用方法参考服务端
WSARecv(m_Socket,@Buf,1,@RecvBytes,Flag,nil,nil);
}
CloseSocket(m_Socket);
WSACleanup();
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SocketThread:=TSocketThread.Create(Memo1);
end;

procedure TForm1.Button2Click(Sender: TObject);
Var
Str:String;
begin
Str:=Edit1.Text;
ClientOpen(PChar(Str)^,Length(Str));
end;

{ TSocketThread }

constructor TSocketThread.Create(vMemo:TMemo);
begin
Inherited Create(true);
Self.FreeOnTerminate:=true;
Memo:=vMemo;
Self.Resume;
end;

procedure TSocketThread.Execute;
var
buf:TWSABUF;
RecvBytes,Flag:DWORD;
hRet:Integer;
Data: TWSAData;
Addr: TSockaddrIn;
AcceptSocket,m_Socket:TSocket;
begin
WSAStartup(MakeWord(2,2), Data);
m_Socket := WSASocket(AF_INET, SOCK_STREAM ,0,nil,0,0);
if m_Socket=INVALID_SOCKET then
Exit;
Addr.sin_family := AF_INET;
Addr.sin_addr.s_addr := htonl(INADDR_ANY);
Addr.sin_port := htons(8000);
bind(m_Socket,@Addr,Sizeof(Addr));
Listen(m_Socket,5);
While Not Terminated do
begin
AcceptSocket:=WSAAccept(m_Socket,nil,nil,nil,0);
if AcceptSocket=INVALID_SOCKET then
Exit;
Buf.Buf:=@tBuff;
Buf.Len:=8196;
Flag:=0;
hRet:=WSARecv(AcceptSocket,@Buf,1,@RecvBytes,@Flag,nil,nil);

{做你的处理部分}
Synchronize(WriteMemo);
{发送数据到客户段使用WSASend,Socket参数使用AcceptSocket
如:WSASend(AcceptSocket,Buf,1,SendOfBytes,0,nil,nil);
}
end;
CloseSocket(m_Socket);
WSACleanup();
end;

procedure TSocketThread.WriteMemo;
begin
Memo.Lines.Add(StrPas(PChar(@tBuff)));
end;

end.
 
继续请教:
{发送数据到客户段使用WSASend,Socket参数使用AcceptSocket
如:WSASend(AcceptSocket,Buf,1,SendOfBytes,0,nil,nil);
}
这里不用指定客户端的IP和端口吗?
我实际测试:客户端发给服务端正常,服务端按照这样返回的客户端无法接收到。
我QQ:247089。可能的话加我好友
 
服务端是用acceptsocket,当然不用指定客户端的IP和端口,因为这个套按字的结构里已经有了。1000分的贴子上他的客户端写错了,写成acceptsocket,这个贴子改过来了。请仔细看。你有1000分就给我200分吧。呵呵
 
分我有,可是服务端按照这样返回的客户端无法接收到啊,你有QQ吗,我把我写的发你,你帮我改下,OK?
分我一共还有1200,解决了剩下的全给你
 
你使用服务端发了,必须在客户端使用WSARecv接收你不接怎么能收到可以使用WSAEventSelect 注册FD_WRITE获取事件然后开一个线程等待其事件就可以收信息了非常简单我就不做了最近比较忙:).
 
对了我的分呢?:)可是分哦!
 
对不起,我出差刚回来
马上开帖给分
 
appfirst来此接分

http://www.delphibbs.com/delphibbs/dispq.asp?lid=3012632
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3012633
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3012634
 
哎一个都打不开
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3018743
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3018742
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3018740
 
后退
顶部