simple tcp echo program not working when SDL included?

我们两清 提交于 2019-12-24 10:56:48

问题


I have this weird problem where whenever I #include "SDL/SDL.h", my windows socket program doesn't execute. It compiles but it doesn't do anything when run. When I remove the #include "SDL/SDL.h" header, compile and run, it starts working again??.

I'm trying to make both SDL and my original socket program work but I don't understand whats wrong this.

//#include "SDL/SDL.h"
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
#include <ws2tcpip.h>
#define MAXLEN 80
using namespace std;

const int winsockVersion = 2;

int main( int argc, char* args[] ) { 

    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(2,0),&wsadata)) == 0){
        cout<<"-[ WSAStartup Initialized. ]" << endl;

        char PORT[MAXLEN];
        char SERVER[MAXLEN];
        cout <<"Server: ";
        cin>>SERVER;
        cout <<"Port: ";
        cin>>PORT;

        struct addrinfo hints, *res;
        int sockfd;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if (getaddrinfo(SERVER,PORT,&hints,&res) != 0){
            cout<<"-Getaddrinfo unsuccessful." << endl;
        }

        if ( (sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1 ){
            cout<<"-Unable to create socket." << endl;
        }

        if ( (connect(sockfd,res->ai_addr,res->ai_addrlen)) != -1 ){
            cout<<"-[ Connection Established. ]" << endl;
        }

        cout<<"-[ Client connecting to: ]" << res->ai_addr << endl;

        while(true){
            string text_buff;
            cout<<"Send: ";
            getline(cin,text_buff);
            if( (send(sockfd,text_buff.c_str(),text_buff.length()+1,0)) != -1 ){
                cout<<"-----------------------------------> Data sent!." << endl;
            }

            if ( text_buff == "quit" ){
                break;
            }

        }

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
        if(WSACleanup()!=0){
            cout<<"-WSACleanup Successful." << endl;
        }else{
            cout<<"-WSACleanup Failed." << endl;
        }
    }

    return 0;
}

compiling with

g++ -o draft.exe draft.cpp -I"C:\compilers and libs\Libs\SDL\SDL devep\SDL-1.2.15\include" -L"C:\compilers and libs\Libs\SDL\SDL devep\SDL-1.2.15\lib" -lmingw32 -lSDLmain -lSDL -lws2_32

回答1:


As far as I know SDL rewrites the main function with some silly #define main trick, it intercepts program main like that.

Therefore, main might not be getting called at all.

If I remember right, your main function (arguments) should be exactly int main(int argc, char *argv[]).



来源:https://stackoverflow.com/questions/10877885/simple-tcp-echo-program-not-working-when-sdl-included

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!