I am new to Win32 programming.
sprintf(lpszBuff,"HELO Mail-Server\r\n");
send(s,lpszBuff,strlen(lpszBuff),0);
recv(s,lpszBuff,100,0);
cout << lpszBuff;
In here I connect to a local mail server. The buffer contains the 开发者_运维百科request I send, the same buffer contains the reply send by the browser. How do I see the reply? cout <<buffer
doesn't show any output. I am doing this on VC++ 2008.
Are you using a blocking or non-blocking socket? What is the return value of recv()
? Chances are, you are reading before there is anything available to read, and recv()
is returning an error code that you are ignoring. What you have shown is way too simplistic. Proper socket coding requires more logic (looping when incomplete sends/reads occur, using select()
to detect socket states, error handling, etc)
When you talk to SMTP server as a sender, the reception/mail-server should send 220 command to sender first.
R: 220 BBN-UNIX.ARPA Simple Mail Transfer Service Ready
S: HELO USC-ISIF.ARPA
R: 250 BBN-UNIX.ARPA
S: MAIL FROM:<Smith@USC-ISIF.ARPA>
R: 250 OK
S: RCPT TO:<Jones@BBN-UNIX.ARPA>
R: 250 OK
S: RCPT TO:<Green@BBN-UNIX.ARPA>
R: 550 No such user here
S: RCPT TO:<Brown@BBN-UNIX.ARPA>
R: 250 OK
S: DATA
R: 354 Start mail input; end with <CRLF>.<CRLF>
S: Blah blah blah...
S: ...etc. etc. etc.
S: .
R: 250 OK
S: QUIT
R: 221 BBN-UNIX.ARPA Service closing transmission channel
R: Receptor -- local SMTP server S: indicates the local Sender
Basically you should:
recv(s,lpszBuff,200,0); // for 220
cout << lpszBuff;
sprintf(lpszBuff,"HELO Mail-Server\r\n");
send(s,lpszBuff,strlen(lpszBuff),0);
recv(s,lpszBuff,100,0);
cout << lpszBuff;
Make sure your socket is in block mode.
精彩评论