Example Socket Program: Getting newsgroups from a news server
Here is program to download the newsgroups from a news server:
//----------------------------------------------------------------- // File: GetNewsgroups.wcc //
// This wcBASIC program is designed to connect to a news server // and download the newsgroups names into a file. If you plan
// to use this file to import into Wildcat!, you will need to // clean up the file before you use it. We could of done it
// here but reading line by line would of slowed down the // receiver. // // How to use: // // wcRUN -r getnewsgroups hostdomain [outputfilename] //
// connect to the host domain name and save the newsgroups in // a file outputfilename. If no output file is provided, the
// the default output file name is hostdomain.newsgroup. // // example: // // wcRUN -r getnewsgroups news.winserver.com //
// Please note that no authenthention being done here. If you need // to authenticate, the AUTH command format to send is: // // AUTH USER name // AUTH PASS password //
// Make sure you check for response codes if you use these commands. // // //-----------------------------------------------------------------
#include "util.wch" #include "wsockhlp.wch"
//----------------------------------------------------------------- // some constants
const crlf = chr(13)+chr(10)
//----------------------------------------------------------------- // Wrapper to close socket
sub ss_closesocket(s as Tsocket)
if s <> INVALID_SOCKET then print "[0;1;40;37m>>[0;1;40;33m Closing Socket" closesocket(s) end if end sub
sub DoUpdateScreen UpdateScreen() end sub
////////////////////////////////////////////////////////////// // Main Program
//////////////////////////////////////////////////////////////
cls DoUpdateScreen() ColorEnabled = True
print "--------------------------------------" print "[0;1;40;36mGetNewsGroup v1.0"
print "--------------------------------------" DoUpdateScreen()
dim port as integer = 119
dim host as string = paramstr(1) dim newsfn as string = paramstr(2)
if host = "" then
print "[0;1;40;31mno host defined" end end if
if newsfn = "" then
newsfn = host+".newsgroups" end if
//----------------------------------------- // create socket
dim s as TSocket = INVALID_SOCKET s = socket(AF_INET, SOCK_STREAM, 0) if s = INVALID_SOCKET then
print "[0;1;40;31mInvalid socket: "; GetLastError WaitEnter end end if
//----------------------------------------- // prepare server info and connect
dim sa as TSockAddr sa.sin_family = AF_INET
sa.sin_port = htons(port) sa.sin_addr.S_addr = ReverseDNS(host)
print "[0;1;40;37m>>[0;1;40;33m Connecting to [0;1;40;37m";host DoUpdateScreen()
if ws_connect(s, sa, sizeof(sa)) <> 0 then
print "[0;1;40;31mConnect failed: "; GetLastError ss_closesocket(s) WaitEnter end end if
//----------------------------------------- // expected first line must be 200 response
dim r as string
if GetSocketResponse(s, r) <> 200 then print "[0;1;40;31mInvalid Socket Response" ss_closesocket(s)
WaitEnter end end if
print "[0;1;40;37m>>[0;1;40;33m Connected! Sending request..."
DoUpdateScreen()
//----------------------------------------- // send List Newsgroups command
dim t as string = "LIST NEWSGROUPS"+crlf ws_send(s, @t, len(t), 0)
//-----------------------------------------
// Read result and save to file
print "[0;1;40;37m>>[0;1;40;33m Reading result. Saving to file ";newsfn DoUpdateScreen()
dim h as integer = open newsfn for output dim buf as string = "" dim bytes as integer = 0 while (ReadSocketRaw(s,buf) <> 0)
print #h, buf; bytes = bytes + len(buf) wend close #h
print "[0;1;40;37m>>[0;1;40;33m Complete! bytes received: "; bytes DoUpdateScreen()
ss_closesocket(s)
End
//////////////////////////////////////////////////////// catch err_nocarrier
ss_closesocket(s)
|
© 2003 Hector Santos, http://www.santronics.com
|