在服务中
CSocket *lp=new CSocket();
if(lp!=NULL)
{
lp->Create(2000); 报异常
}然后我就在 前加了
AfxSocketInit();//这个是成功的
CSocket *lp=new CSocket();
if(lp!=NULL)
{
lp->Create(2000); 不报异常了 ,但是代码不往下了
}
CSocket *lp=new CSocket();
if(lp!=NULL)
{
lp->Create(2000); 报异常
}然后我就在 前加了
AfxSocketInit();//这个是成功的
CSocket *lp=new CSocket();
if(lp!=NULL)
{
lp->Create(2000); 不报异常了 ,但是代码不往下了
}
服务程序中对于窗口是很麻烦的,而不少的MFC中都隐含的各种窗口,消息框的使用。基本上用MFC写服务是不大现实的。
WSAEADDRINUSE
(10048)
Address already in use.
Only one usage of each socket address (protocol/IP address/port) is normally permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that wasn't closed properly, or one that is still in the process of closing. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt(SO_REUSEADDR). Client applications usually need not call bind at all - connect will choose an unused port automatically. When bind is called with a wild-card address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is "committed." This could happen with a call to other function later, including connect, listen, WSAConnect or WSAJoinLeaf.
HWINSTA hwinstaSave;
HDESK hdeskSave;
HWINSTA hwinstaUser;
HDESK hdeskUser; // Ensure connection to service window station and desktop, and
// save their handles. GetDesktopWindow();
hwinstaSave = GetProcessWindowStation();
dwThreadId = GetCurrentThreadId();
hdeskSave = GetThreadDesktop(dwThreadId); // Impersonate the client and connect to the User's
// window station and desktop. hwinstaUser = OpenWindowStation("WinSta0", FALSE, MAXIMUM_ALLOWED);
if(hwinstaUser == NULL){
return ;
}
SetProcessWindowStation(hwinstaUser);
hdeskUser = OpenDesktop("Default", 0, FALSE, MAXIMUM_ALLOWED);
if(hdeskUser == NULL){
SetProcessWindowStation(hwinstaSave);
CloseWindowStation(hwinstaUser);
return ;
}
SetThreadDesktop(hdeskUser); dwGuiThreadId = dwThreadId; ///////////////////
if (!AfxSocketInit ()) {
return FALSE;
}
try
{
CSocket *lp=new CSocket();
if (lp->Create(2100))
{
if(lp->Listen())
return FALSE;
}
else
{
delete lp;
return FALSE;
}
}catch (...) {
delete lp;
return FALSE;
}
///////////////////
dwGuiThreadId = 0; // Restore window station and desktop. SetThreadDesktop(hdeskSave);
SetProcessWindowStation(hwinstaSave);
CloseDesktop(hdeskUser);
CloseWindowStation(hwinstaUser);
return TRUE;