sockets - Using Winsockets to connect to an URL -


i have simple win32 app use post usenet. use send email.

a snippet of code usenet posting this:

... wsaasyncselect(conn_sock,mywin,nret,fd_connect|fd_read|fd_write|fd_close|fd_accept); lpservent = getservbyname("nntp", "tcp");    saserv.sin_family = af_inet; saserv.sin_addr = *((lpin_addr)*lphostent->h_addr_list); nret = connect((socket)conn_sock,(lpsockaddr)&saserv,sizeof(sockaddr_in)); ... etc. 

it works well. when change "nntp" "smtp" can send email. if "http" code not work well. can connect , get/post if address "localhost", "127,0,01" or "192.168.0.5" not if use "195.nn.nn.nn" or "www.somesite.com" etc.

what missing? trying avoid using httpopenrequest() etc. functions , use send() recv() etc. instead.

connect() requires ip address in numeric format.

getservbyname() intended retrieve ip address , port of service provider registered in services database easy lookup. instance, if client on corporate network has own smtp server, makes sense smtp service provider might registered getservbyname("smtp") or getaddrinfo("smtp") can used. less there http service provider registered, though. , end users not have service provider database installed on pcs, or empty.

if have hostname ("smtp.somesite.com", "nntp.somesite.com", "www.somesite.com", etc), need use gethostbyname() or getaddrinfo() resolve hostname ip address:

hostent *lphostent = gethostbyname("nntp.somesite.com");    if ((lphostent) && (lphostent->h_addrtype == af_inet)) {     saserv.sin_family = af_inet;     saserv.sin_addr = *((lpin_addr)(lphostent->h_addr));     saserv.sin_port = htons(119);     nret = connect(conn_sock, (lpsockaddr)&saserv, sizeof(saserv)); } 

addr_info hints = {0}; hints.ai_family = af_inet; hints.ai_socktype = sock_stream; hints.ai_protocol = ipproto_tcp;  addr_info *addr = null;  nret = getaddrinfo("nntp.somesite.com", "119", &hints, &addr); if (nret == 0) {     nret = connect(conn_sock, addr->ai_addr, addr->ai_addrlen);     freeaddrinfo(addr); } 

if have ip address in string format instead ("127.0.0.1", "192.168.0.5", etc), need use inet_addr() or getaddrinfo() convert string numeric format:

saserv.sin_family = af_inet; saserv.sin_addr = inet_addr("192.168.0.5"); saserv.sin_port = htons(119); nret = connect(conn_sock, (lpsockaddr)&saserv, sizeof(saserv)); 

addr_info hints = {0}; hints.ai_flags = ai_numerichost; hints.ai_family = af_inet; hints.ai_socktype = sock_stream; hints.ai_protocol = ipproto_tcp;  addr_info *addr = null;  nret = getaddrinfo("192.168.0.5", "119", &hints, &addr); if (nret == 0) {     nret = connect(conn_sock, addr->ai_addr, addr->ai_addrlen);     freeaddrinfo(addr); } 

these common scenarios code needs support. getservbyname() not commonly used.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -