Ok well I'm making a console msn client.

#pragma comment(lib, "wsock32.lib ")
#include <windows.h> 
#include <iostream>
#include <stdio.h>
#include <string.h>

int main(){
char buf[12];

WSAData wsdata; 
WORD wsver=MAKEWORD(2, 0); 
int nret=WSAStartup(wsver, &wsdata); 
if(nret != 0){ 
std::cout<<"Startup failed, error code: "<<WSAGetLastError(); 
WSACleanup(); 
return -1;
}

std::cout<<"Init success\n";
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
std::cout<<"Socket init failed";
return -1;
}

std::cout<<"Socket initialized\n";
sockaddr_in sin;
sin.sin_port=htons(1863); 
sin.sin_addr.s_addr=inet_addr("64.4.9.254"); 
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ 
std::cout<<"Connect failed, error: "<<WSAGetLastError();
WSACleanup(); 
return -1;
}

    std::cout<<"Connection successful!\n\n";

    char text[] = "VER 1 MSNP18 MSNP17 CVR0\n\r";
    send(kSock, text, sizeof(text), 0);
    printf(">>Client: %s\n",text);

    recv(kSock,buf, sizeof(buf), 0); 
	printf("<<Server: %s\n",buf);

    memset( buf, 0, sizeof(buf) );
    puts (buf);

	char text1[] = "CVR 2 0x040c winnt 5.1 i386 MSNMSGR 14.0.8064.0206 msmsgs MYEMAILHERE\n\r";
    send(kSock, text1, sizeof(text1), 0);
    printf(">>Client: %s\n",text1);

    recv(kSock, buf, sizeof(buf), 0); 
    printf("<<Server: %s\n",buf);
    closesocket(kSock);
return 0;
}

What this should do is:
Sends "VER 1 MSNP18 MSNP17 CVR0\n\r"
MSN server will reply with "VER 1 MSNP18"

Buffer is Flushed

Sends "CVR 2 0x040c winnt 5.1 i386 MSNMSGR 14.0.8064.0206 msmsgs MYEMAILHERE\n\r"
Server will then reply with something.

What actually happens is:
first command is sent
server replies with "VER 1 MSNP18"

Buffer is Flushed

Second command sent
the buffer remains empty and no response from server is displayed

I'm lost maby you guys would know how to help?
P.S i think it's a problem with my code i don't think the server sends an empty response.

Recommended Answers

All 6 Replies

> std::cout<<"Connection successful!\n\n";
> printf(">>Client: %s\n",text);
First you decide whether you're a C programmer or a C++ programmer.

> memset( buf, 0, sizeof(buf) );
> puts (buf);
Huh?

> recv(kSock, buf, sizeof(buf), 0);
> printf("<<Server: %s\n",buf);
Many things wrong here
1. recv() returns a result, which you're ignoring.
2. recv() does NOT automatically append a \0 to make buf a proper string.
3. There isn't space in the buffer to append a \0 either.

n = recv(kSock, buf, sizeof(buf)-1, 0); 
    if ( n > 0 ) {
        buf[n] = '\0';  // make it printable
        printf("<<Server: %s\n",buf);
    } else {
        // do other things with n == 0 and n < 0
    }

4. recv() does not guarantee to receive the whole message in one call (neither does send for that matter).
The server might send "HELLO WORLD" in a single send() call, but you might only get as far as "HELLO WO". You would need to call recv() again to get "RLD".

Both send() and recv() need to pay close attention to the return results, and if necessary be called again to ensure all the data is transferred.

commented: please have my babies? +11

OK, thanks I've used the looping receive method and its solved a few bugs but I still cant get the second response.

What bugs did it solve? Did anything else in your program change? Post your updated code.

Your program might not be working even though you think it is. Simply taking out or fixing one thing can cause another thing to break. Errors can cover other errors, making them look correct. Etc.

What bugs did it solve? Did anything else in your program change? Post your updated code.

Your program might not be working even though you think it is. Simply taking out or fixing one thing can cause another thing to break. Errors can cover other errors, making them look correct. Etc.

It solved a bug with the buffer full of gibberish.

Before: http://teamx-security.com/imghost/iarkey/buggy.png After: http://teamx-security.com/imghost/iarkey/lessbuggy.png Also updated code is: (note my email has been replaced it does same with and without)

#pragma comment(lib, "wsock32.lib ")
#include <windows.h> 
#include <iostream>
#include <stdio.h>
#include <string.h>
int g;
int main(){
char buf[120];

WSAData wsdata; 
WORD wsver=MAKEWORD(2, 0); 
int nret=WSAStartup(wsver, &wsdata); 
if(nret != 0){ 
printf("Startup failed"); 
WSACleanup(); 
return -1;
}

printf("Init success\n");
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
printf("Socket init failed");
return -1;
}

printf("Socket initialized\n");
sockaddr_in sin;
sin.sin_port=htons(1863); 
sin.sin_addr.s_addr=inet_addr("64.4.9.254"); 
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ 
printf("Connect failed, error: ");
WSACleanup(); 
return -1;
}

    printf("Connection successful!\n\n");

    char text[] = "VER 1 MSNP18 MSNP17 CVR0\n\r";
    send(kSock, text, sizeof(text), 0);
    printf(">>Client: %s\n",text);

    g = recv(kSock, buf, sizeof(buf)-1, 0); 
    if ( g > 0 ) {
        buf[g] = '\0';
        printf("<<Server: %s\n",buf);
    } else {
        printf("nothing Recived");
    }

    memset( buf, 0, sizeof(buf) );
    puts (buf);

	char text1[] = "CVR 2 0x040c winnt 5.1 i386 MSNMSGR 14.0.8064.0206 msmsgs MYEMAILHERE\n\r";
    send(kSock, text1, sizeof(text1), 0);
    printf(">>Client: %s\n",text1);

	 g = recv(kSock, buf, sizeof(buf)-1, 0); 
    if ( g > 0 ) {
        buf[g] = '\0';
        printf("<<Server: %s\n",buf);
    } else {
        printf("nothing Recived\n");
    }

    closesocket(kSock);
return 0;
}

But you're still not checking send() at all are you.

How would i go about doing that?

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.