K
kofbee
Unregistered / Unconfirmed
GUEST, unregistred user!
近段我想用VC重写以前用delphi写的SOCKET服务程序,但是发现我用vc打开端口以后,delphi写的客户端却死活连接不上,用VC写的客户端却可以正常连接。下面是2个主要代码:VC的:
//取得端口号
UpdateData(true);
CString sPort=m_vPort;
int iPort=atoi(sPort.GetBuffer(0));
//建立服务器SOCKET
m_SCKServer=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_SCKServer==INVALID_SOCKET)
{
AddMsgToMemo("程序主SOCKET创建失败!/r/n"
return;
}
else
{
AddMsgToMemo("程序主SOCKET创建成功!/r/n"
}
sockaddr_in saddr;
saddr.sin_addr.s_addr=INADDR_ANY;
saddr.sin_family=AF_INET;
saddr.sin_port=iPort;
if (bind(m_SCKServer,(sockaddr *)&saddr,sizeof(saddr))== SOCKET_ERROR)
{
AddMsgToMemo("程序主SOCKET绑定失败!/r/n"
return;
}
int ret=WSAAsyncSelect(m_SCKServer,this->m_hWnd,WM_SOCKET,FD_ACCEPT);
if (ret==SOCKET_ERROR)
{
AddMsgToMemo("服务器主Socket启动WSASsyncSelect失败!/r/n"
return;
}
else
{
CString sMsg="";
sMsg.Format("服务器主Socket启动WSASsyncSelect成功!%d /r/n",ret);
AddMsgToMemo(sMsg);
}
if (listen(m_SCKServer,5)==SOCKET_ERROR)
{
AddMsgToMemo("服务器主Socket端口监听失败!/r/n"
return;
}
CString sMsg;
sMsg.Format("服务器正在监听端口:%d /r/n",iPort);
AddMsgToMemo(LPCTSTR(sMsg));
GetDlgItem(IDC_PortState)->SetWindowText("端口状态:开启"
Delphi连接的:
procedure TF_Client.btnLINKClick(Sender: TObject);
var
addr : SockAddr;
sAddr : string;
iret,i : integer;
begin
for i:=0 to (strtoint(trim(edt_Num.Text))-1) do
begin
m_sock:=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if m_sock = INVALID_SOCKET then
begin
MessageBox( 0, 'Call socket() failed.', 'Error', MB_ICONERROR );
Exit;
end;
sAddr:=trim(edt_Addr.Text);
addr.sin_family := AF_INET;
addr.sin_port := htons(LISTEN_PORT);
addr.sin_addr.S_addr := inet_addr(PChar(sAddr));
iRet:=Connect(m_sock,@addr,sizeof(SockAddr));
if iRet<>0 then
begin
showmessage(format('Connect Error in %d',[WSAGetLasterror()]));
exit;
end;
// MM1.Lines.Add(format('socket %d Connect OK。 Connect value is %d!',[m_sock,iRet]));
Application.ProcessMessages;
G_CList.Add(Pointer(m_sock));
if WSAAsyncSelect(m_sock,handle,WM_SOCKET,FD_CONNECT or FD_CLOSE or FD_READ or FD_WRITE)<>0 then
begin
showmessage(format('Connect Error in %d',[WSAGetLasterror()]));
exit;
end;
StatusBar1.Panels.Items[0].Text:=inttostr(G_CList.Count);
end;
btnSend.Enabled:=true;
end;
哪位兄弟可以帮忙指教一下啊??
//取得端口号
UpdateData(true);
CString sPort=m_vPort;
int iPort=atoi(sPort.GetBuffer(0));
//建立服务器SOCKET
m_SCKServer=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_SCKServer==INVALID_SOCKET)
{
AddMsgToMemo("程序主SOCKET创建失败!/r/n"
return;
}
else
{
AddMsgToMemo("程序主SOCKET创建成功!/r/n"
}
sockaddr_in saddr;
saddr.sin_addr.s_addr=INADDR_ANY;
saddr.sin_family=AF_INET;
saddr.sin_port=iPort;
if (bind(m_SCKServer,(sockaddr *)&saddr,sizeof(saddr))== SOCKET_ERROR)
{
AddMsgToMemo("程序主SOCKET绑定失败!/r/n"
return;
}
int ret=WSAAsyncSelect(m_SCKServer,this->m_hWnd,WM_SOCKET,FD_ACCEPT);
if (ret==SOCKET_ERROR)
{
AddMsgToMemo("服务器主Socket启动WSASsyncSelect失败!/r/n"
return;
}
else
{
CString sMsg="";
sMsg.Format("服务器主Socket启动WSASsyncSelect成功!%d /r/n",ret);
AddMsgToMemo(sMsg);
}
if (listen(m_SCKServer,5)==SOCKET_ERROR)
{
AddMsgToMemo("服务器主Socket端口监听失败!/r/n"
return;
}
CString sMsg;
sMsg.Format("服务器正在监听端口:%d /r/n",iPort);
AddMsgToMemo(LPCTSTR(sMsg));
GetDlgItem(IDC_PortState)->SetWindowText("端口状态:开启"
Delphi连接的:
procedure TF_Client.btnLINKClick(Sender: TObject);
var
addr : SockAddr;
sAddr : string;
iret,i : integer;
begin
for i:=0 to (strtoint(trim(edt_Num.Text))-1) do
begin
m_sock:=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if m_sock = INVALID_SOCKET then
begin
MessageBox( 0, 'Call socket() failed.', 'Error', MB_ICONERROR );
Exit;
end;
sAddr:=trim(edt_Addr.Text);
addr.sin_family := AF_INET;
addr.sin_port := htons(LISTEN_PORT);
addr.sin_addr.S_addr := inet_addr(PChar(sAddr));
iRet:=Connect(m_sock,@addr,sizeof(SockAddr));
if iRet<>0 then
begin
showmessage(format('Connect Error in %d',[WSAGetLasterror()]));
exit;
end;
// MM1.Lines.Add(format('socket %d Connect OK。 Connect value is %d!',[m_sock,iRet]));
Application.ProcessMessages;
G_CList.Add(Pointer(m_sock));
if WSAAsyncSelect(m_sock,handle,WM_SOCKET,FD_CONNECT or FD_CLOSE or FD_READ or FD_WRITE)<>0 then
begin
showmessage(format('Connect Error in %d',[WSAGetLasterror()]));
exit;
end;
StatusBar1.Panels.Items[0].Text:=inttostr(G_CList.Count);
end;
btnSend.Enabled:=true;
end;
哪位兄弟可以帮忙指教一下啊??