Avatar billede dr.truti Nybegynder
10. november 2003 - 16:01 Der er 1 løsning

Problemer med AfxBeginThread

Jeg sidder og roder lidt rundt med noget "simpelt" socket programmering, jeg kan bare ikke få "AfxBeginThread" til at fungere optimalt!

VC 6.0 siger:
C:\Program Files\Microsoft Visual Studio\MyProjects\Simple TCP Server\Simple TCP Server.cpp(18) : error C2065: 'ServerThread' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Simple TCP Server\Simple TCP Server.cpp(26) : error C2373: 'ServerThread' : redefinition; different type modifiers
---------------------------------------------------------
Koden ser således ud:


#include "stdafx.h"
#include "Simple TCP Server.h"
#include <winsock2.h>
#include <conio.h>
#include <iostream.h>


//Sets the port we use!
#define PORT 20248


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;   
   
    cout << "Press ESCAPE to terminate program\n";
   
    AfxBeginThread(ServerThread, 0);
 
    while(_getch()!=27);
   
    return nRetCode;
}

UINT ServerThread(LPVOID pParam)
{   
    cout << "Starting up TCP server\r\n";

    //Define the type!
    SOCKET server;

    //Stores initialization for Microsoft Windows Sockets
    WSADATA wsaData;

    //The address of the socket
    sockaddr_in local;

    //WSAStartup initializes the program for calling WinSock.
    //First parameter specifies the highest version of winsock the program is allowed to use
    int wsaret = WSAStartup(0x101,&wsaData);

    //If a updated winsock is found wasret will be 0 if not 1, and then we exit!
    if(wsaret!=0)
    {
        return 0;
    }

    //Now we populate the sockaddr_in structure
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = INADDR_ANY;
    local.sin_port = htons((u_short)PORT);

    //the socket function creates our SOCKET
    server = socket(AF_INET,SOCK_STREAM,0);

    //If the socket() function fails we exit
    if(server == INVALID_SOCKET)
    {
        cout << "An error have rised in the socket session!\n";
        cin.get();
        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(bind(server,(sockaddr*)&local,sizeof(local))!=0)
    {
        return 0;
    }

    //listen for incoming clients
    if(listen(server,10)!=0)
    {
        return 0;
    }

    // Variabels for holding the client socket!
    SOCKET client;
    sockaddr_in from;
    int fromlen = sizeof(from);

    //we are looping forever!
    while(true)
    {
        char temp[512];

        //accept incoming clints
        client = accept(server,(struct sockaddr*)&from,&fromlen);
       

        sprintf(temp,"Your IP is %s\r\n",inet_ntoa(from.sin_addr));

        //Send this string to the client
        send(client,temp,strlen(temp),0);
       
        cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
       
        //close client socket
        closesocket(client);

    }

    //closes the socket and releases the socket descriptor
    closesocket(server);


    WSACleanup();

    return 0;
}

---------------------------------------------------------

Hele projektet kan hentes:
http://www.truti.com/tcp_server.zip
Avatar billede segmose Nybegynder
10. november 2003 - 18:21 #1
flyt din _tmain ned i bunden eller lav forward declaration på dine functioner.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester