我這樣怎么不行﹐要如何操作?
sockaddr_in addr;
int addrLen;
addr.sin_addr.s_addr=::htonl(INADDR_ANY);
addr.sin_family=AF_INET;
addr.sin_port=5000;
addrLen=sizeof(addr);CSocket s1,s2;
s1.Create();
s1.Bind((sockaddr *)&addr,addrLen);
s1.Listen();
s1.Accept(s2,(sockaddr *)&(pdlg->addr),&(pdlg->addrLen));......
sockaddr_in addr;
int addrLen;
addr.sin_addr.s_addr=::htonl(INADDR_ANY);
addr.sin_family=AF_INET;
addr.sin_port=5000;
addrLen=sizeof(addr);CSocket s1,s2;
s1.Create();
s1.Bind((sockaddr *)&addr,addrLen);
s1.Listen();
s1.Accept(s2,(sockaddr *)&(pdlg->addr),&(pdlg->addrLen));......
解决方案 »
- #include可以加宏路径吗?
- 如何实现vc++程序自动化编译?
- 为何有些操作系统的服务进程的全路径获取的不对
- 一个和树有关的编程
- 请问如何立即发送鼠标点击消息
- 如何改变Tab控件的背景颜色,希望进入各个不同的页颜色不同,用的是MFC工具栏上的控件
- 论坛使用问题:怎样设置已读帖子的标题颜色?
- 关于自适应滤波的问题请教
- 我自己新建了2个类,class A 和class B,我想在class B 中创建class A 的实体,应该怎样?
- 请问SelectObject()
- 关于带有键盘钩子的DLL远程注入系统进程的问题
- 111222 你的恐怖网站 ifree.533.net 上不去,请发一份给我好吗?
s1.Accept(s2,(sockaddr *)&addr,&addrLen);
BOOL Create( UINT nSocketPort = 0, int nSocketType = SOCK_STREAM, long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, LPCTSTR lpszSocketAddress = NULL );在Create的时候,就应该指定端口了
这里没指定端口,系统为你产生了一个随机端口但你的client不知道端口号,connect不到
so server段accept没反应
用wsaGetlasterror看看错误代码
我說的不行是程序運行沒錯誤﹐s1.Accept(s2)后程序應該進入等待連接的狀態﹐但是我用客戶端嘗試去connect卻沒有任何反應
它的原型是:
virtual BOOL Accept( CAsyncSocket& rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL );
参数:
rConnectedSocketA reference identifying a new socket that is available for connection.lpSockAddrA pointer to a SOCKADDR structure that receives the address of the connecting socket, as known on the network. The exact format of the lpSockAddr argument is determined by the address family established when the socket was created. If lpSockAddr and/or lpSockAddrLen are equal to NULL, then no information about the remote address of the accepted socket is returned.lpSockAddrLenA pointer to the length of the address in lpSockAddr in bytes. The lpSockAddrLen is a value-result parameter: it should initially contain the amount of space pointed to by lpSockAddr; on return it will contain the actual length (in bytes) of the address returned. 注解:
Call this member function to accept a connection on a socket. This routine extracts the first connection in the queue of pending connections, creates a new socket with the same properties as this socket, and attaches it to rConnectedSocket. If no pending connections are present on the queue, Accept returns zero and GetLastError returns an error. The accepted socket (rConnectedSocket) cannot be used to accept more connections. The original socket remains open and listening. The argument lpSockAddr is a result parameter that is filled in with the address of the connecting socket, as known to the communications layer. Accept is used with connection-based socket types such as SOCK_STREAM. accept过的socket不能被用来接受更多的连接,所以应该新new一个socket用来accept,而原始的socket是用来侦听的。例如我的程序
void CListenSocket::OnAccept(int nErrorCode)
{
CAcceptSocket *pSocket=new CAcceptSocket(m_pClientDlg);
……
if(Accept(*pSocket))
{
……我的后两个参数是省略的