Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CString.Format anomaly Pin
Richard MacCutchan17-Jun-15 8:07
mveRichard MacCutchan17-Jun-15 8:07 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 8:26
ForNow17-Jun-15 8:26 
AnswerRe: CString.Format anomaly Pin
Jochen Arndt17-Jun-15 3:27
professionalJochen Arndt17-Jun-15 3:27 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 7:46
ForNow17-Jun-15 7:46 
GeneralRe: CString.Format anomaly Pin
jeron117-Jun-15 8:00
jeron117-Jun-15 8:00 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 8:11
ForNow17-Jun-15 8:11 
GeneralRe: CString.Format anomaly Pin
jeron117-Jun-15 8:19
jeron117-Jun-15 8:19 
QuestionMultithread c Windows Pin
mosine16-Jun-15 2:22
mosine16-Jun-15 2:22 
Hi,
I'm developing a client-server C language in Windows.
The client connects to server and every 5 seconds send a message. The communication is OK and server receives messages.
I try to develop multi-client (.bat file starts client.exe) - server but I have problem: A segmentation fault appears after n messages.
This is my code:
main
//INIT WINSOCK
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
    //CREATE SOCKET
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET){
        printf("Could not create socket : %d" , WSAGetLastError());
    }
    //sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );
    //BIND
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR){
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }
    //LISTEN
    listen(s , 10000);
    //WAIT CONNECTION
    puts("Waiting for incoming connections...");
    while( TRUE ) {
        new_socket = accept(s , (struct sockaddr *)&client, &c);
        if (new_socket == INVALID_SOCKET){
            printf("accept failed with error code : %d" , WSAGetLastError());
        }else{
            clientInfo[cont].clientAddr = client ;
            clientInfo[cont].hClientSocket = new_socket;
            hClientThread[cont] = CreateThread(NULL,0,( LPTHREAD_START_ROUTINE ) cliTh1,( LPVOID ) &clientInfo[cont],0,&dwThreadId ) ;

            if ( hClientThread[cont] == NULL ){
                printf("Unable to create client thread");
            } else {
                printf("thread OK\n");
                CloseHandle( hClientThread[cont] ) ;
                cont++;
            }
        }
    }

Thread function (cliTh1)
DWORD  WINAPI cliTh1( LPVOID lpData ){
    struct CLIENT_INFO *pClientInfo;
    char szClientMsg[250];
    static int j = 0;
    int i = 0;
    char *message;

    pClientInfo = (struct CLIENT_INFO *)lpData ;
    printf("SOCKET:%d - THREAD_ID:%ld\n", pClientInfo->hClientSocket, GetCurrentThreadId());
    while ( 1 ){
        if(WSAGetLastError()){
            printf("CURRENT:%ld\n", GetCurrentThreadId());
            closesocket(pClientInfo->hClientSocket);
            ExitThread(GetCurrentThreadId());
        }
        if(recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) > 0){
            if(j>5000){j=0;};
            strcpy(bufferRx[j].packet, szClientMsg);
            for(i=0; i < sizeof( szClientMsg ); i++){
                szClientMsg[i] = 0;
            }
            printf("RECEIVE %d: %s",j, bufferRx[j].packet);
            j++;
        }
    }
    return(TRUE);
}


I read on Internet, but I don't know where is Error.
Thanks.
AnswerRe: Multithread c Windows Pin
Jochen Arndt16-Jun-15 2:33
professionalJochen Arndt16-Jun-15 2:33 
GeneralRe: Multithread c Windows Pin
mosine16-Jun-15 2:47
mosine16-Jun-15 2:47 
GeneralRe: Multithread c Windows Pin
mosine16-Jun-15 3:01
mosine16-Jun-15 3:01 
GeneralRe: Multithread c Windows Pin
Jochen Arndt16-Jun-15 3:02
professionalJochen Arndt16-Jun-15 3:02 
GeneralRe: Multithread c Windows Pin
mosine16-Jun-15 3:21
mosine16-Jun-15 3:21 
QuestionFailure to remove folders after using CFileDialog DoModal Pin
Still learning how to code15-Jun-15 21:28
Still learning how to code15-Jun-15 21:28 
AnswerRe: Failure to remove folders after using CFileDialog DoModal Pin
Freak3016-Jun-15 0:21
Freak3016-Jun-15 0:21 
GeneralRe: Failure to remove folders after using CFileDialog DoModal Pin
Jochen Arndt16-Jun-15 0:24
professionalJochen Arndt16-Jun-15 0:24 
GeneralRe: Failure to remove folders after using CFileDialog DoModal Pin
Still learning how to code16-Jun-15 2:08
Still learning how to code16-Jun-15 2:08 
AnswerRe: Failure to remove folders after using CFileDialog DoModal Pin
Jochen Arndt16-Jun-15 0:22
professionalJochen Arndt16-Jun-15 0:22 
GeneralRe: Failure to remove folders after using CFileDialog DoModal Pin
Still learning how to code16-Jun-15 2:12
Still learning how to code16-Jun-15 2:12 
GeneralRe: Failure to remove folders after using CFileDialog DoModal Pin
Jochen Arndt16-Jun-15 2:24
professionalJochen Arndt16-Jun-15 2:24 
GeneralRe: Failure to remove folders after using CFileDialog DoModal - **** FIXED (I think !) **** Pin
Still learning how to code16-Jun-15 2:54
Still learning how to code16-Jun-15 2:54 
GeneralRe: Failure to remove folders after using CFileDialog DoModal - **** FIXED (I think !) **** Pin
Jochen Arndt16-Jun-15 3:19
professionalJochen Arndt16-Jun-15 3:19 
QuestionRe: Failure to remove folders after using CFileDialog DoModal Pin
David Crow16-Jun-15 4:21
David Crow16-Jun-15 4:21 
QuestionExisting VR Frame Renderers Pin
Trevor Johansen15-Jun-15 15:25
Trevor Johansen15-Jun-15 15:25 
QuestionLoad CSV file to a database in Windows 64bit Pin
aks.14-Jun-15 22:36
aks.14-Jun-15 22:36 

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.