Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [c++]

    Ciao a tutti...ho un problema del cavolo ma non riesco a venirne a capo e ci sto perdendo troppo tempo...
    codice:
    // ServerTCP.cpp : definisce il punto di ingresso dell'applicazione console.
    //
    
    #include "stdafx.h"
    #include "ServerTCP.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // L'unico oggetto applicazione
    
    CWinApp theApp;
    
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        int nRetCode = 0;	
    	
        cout << "Press ESCAPE to terminate program\r\n";
        AfxBeginThread(ServerThread,0);
    	while(_getch()!=27);
    	
        return nRetCode;
    }
    
    UINT ServerThread(LPVOID pParam)
    {	
        cout << "Starting up TCP server\r\n";
    
        //A SOCKET is simply a typedef for an unsigned int.
        //In Unix, socket handles were just about same as file 
        //handles which were again unsigned ints.
        //Since this cannot be entirely true under Windows
        //a new data type called SOCKET was defined.
        SOCKET server;
    
        //WSADATA is a struct that is filled up by the call 
        //to WSAStartup
        WSADATA wsaData;
    
        //The sockaddr_in specifies the address of the socket
        //for TCP/IP sockets. Other protocols use similar structures.
        sockaddr_in local;
    
        //WSAStartup initializes the program for calling WinSock.
        //The first parameter specifies the highest version of the 
        //WinSock specification, the program is allowed to use.
        int wsaret=WSAStartup(0x101,&wsaData);
    
        //WSAStartup returns zero on success.
        //If it fails we exit.
        if(wsaret!=0)
        {
            return 0;
        }
    
        //Now we populate the sockaddr_in structure
        local.sin_family=AF_INET; //Address family
        local.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
        local.sin_port=htons((u_short)20248); //port to use
    
        //the socket function creates our SOCKET
        server=socket(AF_INET,SOCK_STREAM,0);
    
        //If the socket() function fails we exit
        if(server==INVALID_SOCKET)
        {
            return 0;
        }
    
        //bind links the socket we just created with the sockaddr_in 
        //structure. Basically it connects the socket with 
        //the local address and a specified port.
        //If it returns non-zero quit, as this indicates error
        if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
        {
            return 0;
        }
    
        //listen instructs the socket to listen for incoming 
        //connections from clients. The second arg is the backlog
        if(listen(server,10)!=0)
        {
            return 0;
        }
    
        //we will need variables to hold the client socket.
        //thus we declare them here.
        SOCKET client;
        sockaddr_in from;
        int fromlen=sizeof(from);
    
        while(true)//we are looping endlessly
        {
            char temp[512];
    
            //accept() will accept an incoming
            //client connection
            client=accept(server,
                (struct sockaddr*)&from,&fromlen);
    		
            sprintf(temp,"Your IP is %s\r\n",inet_ntoa(from.sin_addr));
    
            //we simply send this string to the client
            send(client,temp,strlen(temp),0);
            cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
    		
            //close the client socket
            closesocket(client);
    
        }
    
        //closesocket() closes the socket and releases the socket descriptor
        closesocket(server);
    
        //originally this function probably had some use
        //currently this is just for backward compatibility
        //but it is safer to call it as I still believe some
        //implementations use this to terminate use of WS2_32.DLL 
        WSACleanup();
    
        return 0;
    }
    Simpaticamente il compilatore Visual Studio 2010 non mi da errori (non mi segna in rosso cosa che non sono state dichiare per intenderci) tuttavia nel momento in cui compilo mi dice

    Errore 1 error C2065: 'ServerThread': identificatore non dichiarato

    e mi da errore sulla chiamata
    codice:
     AfxBeginThread(ServerThread,0);
    Che posso fare?
    Qualcuno ha qualche idea?

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Il compilatore ha ragione dato che non sa cosa sia ServerThread quando lo incontra nel main.

    Puoi semplicemente spostare il main dopo la funzione ServerThread ...


    P.S. Dovresti usare titoli più significativi ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Ehmmm si scusatemi...

    Nella fretta ho messo solamente il linguaggio...

    Chiedo perdono

    Ciao a tutti

    ps: ho risolto grazie mille :-D

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.