初学winsocket,谁能给我简单讲一下,出200分!要求详细!(200分)

  • 主题发起人 主题发起人 ICEsting
  • 开始时间 开始时间
I

ICEsting

Unregistered / Unconfirmed
GUEST, unregistred user!
关于winsocket api的函数及其用法,我比较笨,最好那代码贴出来,再注释一下:)
谢谢,分不够还可以加!
看了很多,不知道用法!帮一下,让我早点入门!
 
不想用控件,我的目标是向网络上的服务器发送请求.
200分,我觉的够了[:)]
 
winsocket api的资料我有,不过太长了,贴出来怕有人说我乱倒垃圾
 
去买本书吧!
 
可以直接用socket控件来实现,
如果是用标准http或ftp协议的话,直接用这些协议控件来发请求是不错的选择。
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure RecvMessage(var Msg: TMessage); message WM_USER + 1;
public
{ Public declarations }
end;

var
Form1: TForm1;
RemoteHost: string;
RemotePort: Integer;
ClientSocket: Integer;
implementation

{$R *.DFM}

{线程处理函数}
function RecvProc(P: Pointer): DWORD;
var
Buffer: PChar;
ret: Integer;
begin
while True do
begin
ret := recv(ClientSocket, Buffer, sizeof(Buffer), 0);
if ret = 0 then
begin
ShowMessage('Socket closed.');
Break;
end
else
if ret = SOCKET_ERROR then
begin
ShowMessage('Read Error.');
Break;
end;
GetMem(Buffer, ret);
SendMessage(HWND(P), WM_USER + 1, 0, Integer(Buffer));
end;
closesocket(ClientSocket);
ClientSocket := INVALID_SOCKET;
Result := WSAGetLastError;
end;

procedure ShowError;
var
ErrorCode: Integer;
begin
ErrorCode := WSAGetLastError;
ShowMessage('Error: ' + IntToStr(ErrorCode));
end;


procedure TForm1.Button1Click(Sender: TObject);
var
sa_in_svr: sockaddr_in;
tid: Cardinal;
begin
sa_in_svr.sin_family := AF_INET;
sa_in_svr.sin_port := htons(RemotePort);
sa_in_svr.sin_addr.S_addr := htonl(inet_addr(PChar(RemoteHost)));

{连接到服务器}
if connect(ClientSocket, sa_in_svr, sizeof(sockaddr_in)) = SOCKET_ERROR then
begin
closesocket(ClientSocket);
ClientSocket := INVALID_SOCKET;
ShowMessage('Failed to connect to server.');
end
else
begin
{创建接收数据线程}
CreateThread(nil, 0, @RecvProc, Pointer(Handle), 0, tid);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
WSData: WSAData;
ErrorCode: Integer;
sa_in: sockaddr_in;

begin
RemoteHost := '127.0.0.1';
RemotePort := 2000;

{初始化WinSock}
if WSAStartup(MakeWord(1, 1), WSData) <> 0 then
begin
ShowMessage('WinSock initialization fialed.');
Exit;
end;

ClientSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ClientSocket = INVALID_SOCKET then
begin
ShowError;
Exit;
end;

sa_in.sin_family := AF_INET;
sa_in.sin_port := htons(0);
sa_in.sin_addr.S_addr := INADDR_ANY;

if bind(ClientSocket, sa_in, sizeof(sockaddr_in)) = SOCKET_ERROR then
begin
closesocket(ClientSocket);
ClientSocket := INVALID_SOCKET;
ShowError;
end;
end;

procedure TForm1.RecvMessage(var Msg: TMessage);
var
P: PChar;
begin
P := PChar(Msg.LParam);
Memo2.Text := String(P);
FreeMem(P);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
buf: PChar;
len: Integer;
begin
buf := PChar(Memo1.Text);
len := Length(Memo1.Text);
send(ClientSocket, buf, len, 0);
end;

end.
程序退出时别忘调用WSACleanup
这是别人的你参考一下吧。
 
to ego:
我想要,发到我的邮箱吧,谢谢!e-mail:ypengfei@163.com.

to 寻找:
我不想买书,没钱:(

to willrain:
不想用控件,觉的不是太好.

to zhuoyuesky:
我看看,先谢谢!
 
我已经给你发了详尽的编程资料,你慢慢看吧,有什么不懂的,再来这里问。
 
后退
顶部