Mega Code Archive

 
Categories / Delphi / VCL
 

Simple Winsock Component for console app

Title: Simple Winsock Component for console app. Question: how can i creat a TCP/IP internet connection in a console application using winsock? Answer: This is a whole component that you can save to a *.pas file and use to create internet connections from within a console application. Its very basic and very simple. All it does is connect, read, and write. Enjoy! UNIT GSB_Socket; {$A+,B-,C-,D-,E-,F-,G+,H+,I-,J-,K-,L-,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y-,Z1} {$MINSTACKSIZE $00004000} {$MAXSTACKSIZE $00100000} {$IMAGEBASE $00400000} /////////////////////////////////////////////////////////////////////////////// // GSB_SOCK was coded by J3N7iL (EES) as a going away present to the newbies! // Unfortunatly, I have been consumed by real life, // and must depart from the scene. It's been a blast, you all have been great! // // This is at the most, the simpleset working WINSOCK component. // All it does is connect to an ip, read, and write. // Feel free to change, edit, add, or print it out and use is as toilet paper. // // This is what i now use for my SMTP connections, // and of course my IRC & yahoo bots. // But because its TCP/IP, it can be used for every widley used connection // including ICQ notify, FTP Protocol, and PHP notify. // //*****************************PLEASE FOR THE LOVE OF GOD!********************** // If your going to post this on a site, or distribute it, // LEAVE THIS HEADER INTACT!!! INTERFACE uses winsock, windows; TYPE rsp = function(dwProcessID,dwType:DWord):DWORD;stdcall; Procedure StartWSA; Function Open(Port : integer; Address : string) : integer; Function Read(var buff; len : word): word; Procedure write(Data : string); VAR WSAData: TWSAData; GSB_SOCK: TSocket; sin : TSockAddrIn; WsaReady : Boolean = False; IMPLEMENTATION Procedure StartWSA; Begin IF WSAStartup(257,WSAdata) = 0 THEN WSAReady := true; END; Function Open(Port : integer; Address : string) : integer; BEGIN StartWSA; If WsaReady = TRUE THEN BEGIN GSB_SOCK := socket(PF_INET, SOCK_STREAM, getprotobyname('tcp').p_proto); sin.sin_family := PF_INET; sin.sin_port := HTONS(PORT); SIN.sin_addr.S_addr:= inet_addr(PChar(Address)); Result := connect(GSB_SOCK,SIN,sizeof(SIN)); END; END; Function Read(var buff; len : word): word; begin IF WSAReady then result := recv(GSB_SOCK,Buff,len,0); END; Procedure write(Data : string); Var Datasize : integer; BEGIN datasize := length(data); IF WSAReady then send(GSB_SOCK,Data[1],datasize,0); END; initialization StartWSA; finalization WSACleanup; END.