Click here to Skip to main content
15,905,325 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionImage Processing Question Pin
sherinsabry15-Sep-08 4:29
sherinsabry15-Sep-08 4:29 
AnswerRe: Image Processing Question Pin
Saurabh.Garg15-Sep-08 5:10
Saurabh.Garg15-Sep-08 5:10 
GeneralRe: Image Processing Question Pin
sherinsabry22-Sep-08 7:21
sherinsabry22-Sep-08 7:21 
QuestionImage Processing Question Pin
sherinsabry15-Sep-08 4:29
sherinsabry15-Sep-08 4:29 
AnswerRe: Image Processing Question Pin
Mark Salsbery15-Sep-08 5:44
Mark Salsbery15-Sep-08 5:44 
GeneralRe: Image Processing Question Pin
sherinsabry22-Sep-08 7:22
sherinsabry22-Sep-08 7:22 
GeneralRe: Image Processing Question Pin
Mark Salsbery22-Sep-08 7:28
Mark Salsbery22-Sep-08 7:28 
QuestionStrange problem in accept() function of winsock... Pin
oppstp15-Sep-08 3:33
oppstp15-Sep-08 3:33 
Hello, I am writing an ActiveX plug-in now. One of the feature is that it can let the user download a file from the remote server.
The way of downloading files is sequentially now. It means no two files are downloaded at the same time.
I implement this feature by winsock and create another thread for transferring the file. (close thread when complete download)
The procedure is like : user query -> server return corresponding files -> user choose the file -> click download icon -> server and client create the socket -> start to transfer file.

I use the virtual PC to connect the html on my machine to test the download functionality.
But there is a strange problem now. The problem is that the program might be hanged in the accept() sometimes. (like waiting for a client connection, but the result of client connection is success...)
If this happens, the file can't be downloaded. If this doesn't happen, the file can be downloaded.
And the probability of this error is random...
The function socket(), bind(), listen(), connect() of winsock all return success and the closesocket() returns 0 too.
So I have no idea how to solve this problem now. Can someone give me a help or suggestion ?

The server transfer code is
BOOL CServer::SocketForDownload(int port, CString DirectoryPath)
{
   m_iPort = port;	
   m_sDirectoryPath = DirectoryPath;
   m_pDLThread = AfxBeginThread(DLThreadProc, this);
   return SOCKET_OK;
}

UINT DLThreadProc(LPVOID pParam)
{
   CServer *pNW = (CServer*)pParam;
   SOCKET DLSocket;
   SOCKADDR_IN	DLAddr;  

   DLSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   if(DLSocket == INVALID_SOCKET)
   {
      // error handling code
   }
   int optval, rc;
   optval = 1;
   rc = setsockopt(DLSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval));
	
   DLAddr.sin_family = AF_INET;
   DLAddr.sin_port = htons(pNW->m_iPort);
   DLAddr.sin_addr.s_addr = htonl(INADDR_ANY);

   if(bind(DLSocket, (SOCKADDR*) &DLAddr, sizeof(DLAddr)) == SOCKET_ERROR)
   {
      // error handling code
   }

   if (listen(DLSocket, 5) == SOCKET_ERROR)
   {
      // error handling code
   }
   int len;
   SOCKET AcceptSocket;
   while(TRUE)
   {
        len = sizeof(DLAddr);
        AcceptSocket = SOCKET_ERROR;
        while (AcceptSocket == SOCKET_ERROR)
        {
 //        AcceptSocket = accept(DLSocket, NULL, NULL );
           AcceptSocket = accept(DLSocket, (SOCKADDR*) &DLAddr, &len);
	// the above two are all the same result, hang there sometimes     
           if(AcceptSocket == INVALID_SOCKET)
           {
	     // error handling code
           }
        }
        DLSocket = AcceptSocket; 
        break;
    }
  
    // transfer part start
    // transfer code
    // transfer part end	
	
    shutdown(AcceptSocket, SD_SEND);
//  shutdown(DLSocket, SD_SEND);
	
    int returncode;	
    returncode = closesocket(AcceptSocket);
//  returncode = closesocket(DLSocket);
    
    AfxEndThread(SOCKET_OK);
    return SOCKET_OK;
}


The client transfer code is
BOOL CClient::SocketForUpload(char *ip, char *filepath)
{
    m_sFilePath = filepath;
    m_sIP = ip;
    m_pULThread = AfxBeginThread(ULThreadProc, this);
    return SOCKET_OK;
}
UINT ULThreadProc(LPVOID pParam)
{
    CClient *pNW = (CClient *)pParam;
    SOCKET DLSocket;
    SOCKADDR_IN	DLAddr;

    if((DLSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
    {
        // error handling code	
    }
    Sleep(500);

    DLAddr.sin_family = AF_INET;
    DLAddr.sin_port = htons(pNW->m_iPort - 1);
    DLAddr.sin_addr.s_addr = inet_addr(pNW->m_sIP);

    if(connect(DLSocket, (SOCKADDR *) &DLAddr, sizeof(DLAddr)) == SOCKET_ERROR)
    {
       // error handling code
    }
     
    // transfer part start
    // transfer code
    // transfer part end

    shutdown(DLSocket, SD_SEND);
    int returncode = closesocket(DLSocket);
    
    AfxEndThread(SOCKET_CLOSE);
    return SOCKET_OK;
}

The value of SOCKET_OK is an enum value

My environment is : Vista SP1 + VS2005 SP1, The virtual PC is XP SP3 + VS2005 SP1
The http server is a simple http server implemented by our member.

Thank you very much.
AnswerRe: Strange problem in accept() function of winsock... Pin
Malli_S15-Sep-08 4:44
Malli_S15-Sep-08 4:44 
GeneralRe: Strange problem in accept() function of winsock... Pin
oppstp15-Sep-08 16:36
oppstp15-Sep-08 16:36 
AnswerRe: Strange problem in accept() function of winsock... Pin
Mark Salsbery16-Sep-08 8:29
Mark Salsbery16-Sep-08 8:29 
QuestionIXMLHTTPRequest Problem Pin
alireza_shokoie15-Sep-08 3:14
alireza_shokoie15-Sep-08 3:14 
QuestionMAPI question Pin
josip cagalj15-Sep-08 2:55
josip cagalj15-Sep-08 2:55 
QuestionTranslateMessage in NON-GUI threads ... Pin
Joseph Marzbani15-Sep-08 2:16
Joseph Marzbani15-Sep-08 2:16 
AnswerRe: TranslateMessage in NON-GUI threads ... Pin
SandipG 15-Sep-08 2:21
SandipG 15-Sep-08 2:21 
AnswerRe: TranslateMessage in NON-GUI threads ... Pin
Joseph Marzbani15-Sep-08 2:36
Joseph Marzbani15-Sep-08 2:36 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
SandipG 15-Sep-08 2:47
SandipG 15-Sep-08 2:47 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
Joseph Marzbani15-Sep-08 3:07
Joseph Marzbani15-Sep-08 3:07 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
SandipG 15-Sep-08 3:23
SandipG 15-Sep-08 3:23 
GeneralRe: TranslateMessage in NON-GUI threads ... [modified] Pin
Joseph Marzbani15-Sep-08 3:36
Joseph Marzbani15-Sep-08 3:36 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
SandipG 15-Sep-08 3:44
SandipG 15-Sep-08 3:44 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
Joseph Marzbani15-Sep-08 3:50
Joseph Marzbani15-Sep-08 3:50 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
SandipG 15-Sep-08 3:58
SandipG 15-Sep-08 3:58 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
Joseph Marzbani15-Sep-08 4:06
Joseph Marzbani15-Sep-08 4:06 
GeneralRe: TranslateMessage in NON-GUI threads ... Pin
SandipG 15-Sep-08 5:03
SandipG 15-Sep-08 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.