Hi!

I have a problem with a winsock code. I want it to connect to a irc server, but it won't recieve any readable data... Just alot of "jibberish"...

#include <string>
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

using namespace std;
//class lam0rZ { ~lam0rZ() { system("pause"); /*sleep(5000)*/ } };
class IRC {
public:
    struct Server {
        char *sServer, *cNick, *cUser;
        char nBuffer[1000];
        short int nPort;
    };
    void recieve();
    void nonBlock();
    void sendI(char* cMsg);
    Server innerIRC;
    WSAData WsaDat;
    SOCKET Socket;
    IRC();
    ~IRC();
};
IRC::IRC() {
    Server conInfo;
    conInfo.sServer = "irc.mznation.com"; //Host
    conInfo.nPort = 6667; //Port to host
    WSAStartup(MAKEWORD(2,2), &WsaDat);
    Socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    struct hostent *host = gethostbyname(conInfo.sServer);
    //Setup for IP and port that we'll be communicating with.
    SOCKADDR_IN SockAddr;
    SockAddr.sin_addr;
    SockAddr.sin_port = htons(conInfo.nPort);
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    //Now... connect.
    connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr));
}
IRC::~IRC() {
    shutdown(Socket, SD_SEND);
    closesocket(Socket);
}
void IRC::recieve() {
    Server conInfo;
    int nData = recv(Socket, conInfo.nBuffer, 1000, 0);
    cout << conInfo.nBuffer;
}
void IRC::nonBlock() {
    u_long iMode=0;
    ioctlsocket(Socket, FIONBIO, &iMode);
}
void IRC::sendI(char *cMsg) {
    send(Socket, cMsg, strlen(cMsg), 0);
}
int main () {
    IRC connect;
    typedef char* sInfo;
    connect.nonBlock();
    connect.recieve();
    sInfo cNick = connect.innerIRC.cNick = "NICK ExBot\n\r";
    sInfo cUser = connect.innerIRC.cUser = "USER XXX 8 * :AKLSKAL\n\r";
    connect.sendI(cNick);
    connect.sendI(cUser);
    for (;;) {
        connect.recieve();
    /*
    string x = "123";
    int n = atoi(x.c_str());
    cout << n << endl;
    */
    }
    return 0;
}

Could anyone help me out? Thanks!

You're not receiving anything, it prints uninitialized data. You set the socket to 'nonblocking' and then continuously call recv. So even if there is no data coming from there server, recv will return.
You do not check the return value of recv before printing it, the return value is probably SOCKET_ERROR, and WsaGetLastError() will return WSAEWOULDBLOCK.

Try first to keep the socket in block 'mode', and check the return value of recv (checking return values is one of the most important things, especially when things do not work as you expected).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.