服务端
var
buff:WSABUF;
RecvBytes
WORD;
tBuff:Array[0..8196] of Byte;
hRet:Integer;
Data: WSAData;
Addr: sockaddr_in;
AcceptSocket:Socket;
begin
WSAStartup(MakeWord(2,2), WSAData);
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(INET_ANY);
Addr.sin_port := htons(8000);
bind(m_Socket,@Addr,Sizeof(Addr));
Listen(m_Socket,5);
While true do
begin
AcceptSocket:=WSAAccept(m_Socket,nil,nil,nil,0);
if AcceptSocket=INVALID_SOCKET then
Exit;
Buf.Buf:=tBuf;
Buf.Len:=8196;
hRet:=WSARecv(AcceptSocket,@Buf,1,RecvBytes,0,nil,nil);
{做你的处理部分}
{发送数据到客户段使用WSASend,Socket参数使用AcceptSocket}
end;
closesocket(m_Socket);
WSACleanup();
end;
客户端
var
buff:WSABUF;
SendBytes,Flag
WORD;
tBuff:Array[0..8196] of Byte;
hRet:Integer;
Data: WSAData;
Addr: sockaddr_in;
AcceptSocket:Socket;
begin
WSAStartup(MakeWord(2,2), WSAData);
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:=tBuf;
Buf.Len:=8196;
{做你的处理部分}
hRet:=WSASend(AcceptSocket,@Buf,1,SendBytes,Flag,nil,nil);
if hRet=Socket_Error then
Exit;
{做你的处理部分}
{从服务端接收数据使用WSARecv使用方法参考服务端}
end;
closesocket(m_Socket);
WSACleanup();
end;
以上代码没有测试,不过应该不会有大的问题
.