在c ++中发送和接收 - windows简单的irc客户端

时间:2013-10-11 17:49:20

标签: c++ windows

很多irc适用于linux - 不适合我。

我尝试制作自己的简单irc客户端。 但我认为需要等待“终止/ MOTD命令”。在加入频道之前。

看看我的代码:

int connect_serv(char* server, int port, char* nick, char* channel){
    WSADATA wsaData; /* winsock stuff, linux/unix/*bsd users need not worry about this */

    if (WSAStartup(MAKEWORD(1, 1), &wsaData)) /* more winsock rubbish */
    {
        printf("Failed to initialise winsock!\n");
    }
    HOSTENT* resolv;
    sockaddr_in rem;

    irc_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (irc_socket==INVALID_SOCKET)
    {
        printf("Socket nie smiga: %d\n", WSAGetLastError());
        return 1;
    }
    resolv=gethostbyname(server);
    if (!resolv)
    {
        closesocket(irc_socket);
        return 1;
    }
    memcpy(&rem.sin_addr, resolv->h_addr, 4);
    rem.sin_family=AF_INET;
    rem.sin_port=htons(port);

    if (connect(irc_socket, (const sockaddr*)&rem, sizeof(rem))==SOCKET_ERROR)
    {
        printf("Failed to connect: %d\n", WSAGetLastError());
        closesocket(irc_socket);
        return 1;
    }
    send_cmd("NICK "+char_to_str(nick),irc_socket);
    send_cmd("USER "+char_to_str(nick)+" . . :"+char_to_str(nick),irc_socket);
    send_cmd("JOIN "+char_to_str(channel),irc_socket);
}

void send_cmd(string cmd,int fpsocket){
    char sendmsg[128];
    sprintf(sendmsg,"%s\r\n",cmd.c_str());
    send( fpsocket, sendmsg, strlen(sendmsg),0 );
}

0 个答案:

没有答案