【发布时间】:2012-02-25 00:49:57
【问题描述】:
刚开始学习socket编程,学习了winsock,取得了一些进步。我的问题基本上是:我想发送电子邮件,我该怎么办?
需要提及的点:
- 我了解了如何初始化winsock。 SMTP 端口 (25)。成功创建并连接到套接字。我现在该怎么办?!!! (我被困在这里)
- 我不想要一个可工作的代码。我想学。因此,需要任何书籍、文档、教程或文章推荐。
- 我知道 C 本身对网络一无所知,这是否意味着我必须下载一些库? (我使用的是 VS2010,Windows 7)
以下是我目前阅读的页面链接:
winsock 基本指南:http://msdn.microsoft.com/en-us/library/windows/desktop/ms737629(v=vs.85).aspx
我已经阅读了beej指南的前14页(不能发布链接,新用户最多只能发布两个超链接)
我已经了解了类型(WSADATA、addrinfo structure、sockaddr、SOCKET)和函数(WSAStartup()、WSACleanup()、getaddrinfo()、Shutdown()、WSAGetLastError()、@ 987654332@, ...)
我刚刚开始阅读这篇关于SMTPhttp://www.faqs.org/rfcs/rfc821.html的文章
这是我到目前为止所写的:
#include <stdio.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib") // Applications that use Winsock must be linked with the Ws2_32.lib library file.
#define HTTP_PORT "80"
#define SMTP_PORT "25"
#define HOSTNAME_PORT "101"
/*
All ports and web services names ( which are string aliases of the ports
can be found here:
%WINDIR%\system32\drivers\etc\services
*/
int main(void)
{
WSADATA wsdata;
int iresult, retval; //iresult : instant result
SOCKET connect_socket;
struct addrinfo *result, *ptr, hints;
iresult = WSAStartup(MAKEWORD(2, 2), &wsdata);
if(iresult != 0) printf("Initiation of Winsock succeeded.\n");
else
{
printf("WinSock initialization failed..\n");
WSACleanup();
return 0;
}
if(LOBYTE(wsdata.wVersion) == 2 && HIBYTE(wsdata.wVersion) == 2) printf("winsock.dll is found.\n");
else
{
printf("Can not find the required winsock.dll file.\n");
WSACleanup();
return 0;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_protocol = IPPROTO_TCP; // TCP connection ( full duplex )
hints.ai_socktype = SOCK_STREAM; // Provides sequenced, reliable, two-way, connection-based byte streams
connect_socket = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
if(connect_socket == INVALID_SOCKET)
{
printf("Socket Creation failed..\n");
WSACleanup();
return 0;
}
else printf("Socket Creation Succeeded ..\n");
WSACleanup();
return 1;
}
我跑题了吗?
【问题讨论】:
-
我推荐阅读 Stevens 出色的“TCP/IP 图解”一书,以及他的“Unix 环境中的高级编程”。是的,我知道它说的是 Unix,但这确实是一本很棒的书,
winsock基本上是 BSD 套接字 API 的实现。
标签: c sockets smtp winsock send