간단한 텔넷 접속 예제

#include “winsock2.h”

void main() {

// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf(“Error at WSAStartup()\n”);

// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {
printf( “Error at socket(): %ld\n”, WSAGetLastError() );
WSACleanup();
return;
}

// Connect to a server.
sockaddr_in clientService;

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(“192.168.0.7”);
clientService.sin_port = htons(23);

if ( connect( m_socket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
printf( “Failed to connect.\n” );
WSACleanup();
return;
}

// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = “Client: Sending data.”;
char recvbuf[255] = “”;

while( true ) {
bytesRecv = recv( m_socket, recvbuf, sizeof(recvbuf), 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( “Connection Closed.\n”);
break;
}
if (bytesRecv < 0) return; printf( "Bytes: %ld\n", bytesRecv ); printf( "Message: %s\n", recvbuf); } return; } [/cpp]

각종 네트웍 프로토콜 제어 라이브러리

홈페이지: http://www.datareel.com

DataReel – C++ cross-platform database and communications toolkit.

DataReel 이란 이름을 가진.. 라이브러리.
텔넷 제어 라이브러리를 찾아보다 오픈소스로 프로젝트를 찾았음.
텔넷, ftp, pop3등등.. 여러 프로토콜을 지원함.
간단하게 예제로 들어 있는 텔넷 프로토콜을 MinGW에서 테스트 해보니.. 잘 동작함.

다운로드: dreel464.zip

텔넷 예제

// ——————————- //
// ——– Start of File ——– //
// ——————————- //
// ———————————————————– //
// C++ Source Code File Name: testprog.cpp
// C++ Compiler Used: MSVC, BCC32, GCC, HPUX aCC, SOLARIS CC
// Produced By: DataReel Software Development Team
// File Creation Date: 02/23/2001
// Date Last Modified: 01/01/2009
// Copyright (c) 2001-2009 DataReel Software Development
// ———————————————————– //
// ————- Program Description and Details ————- //
// ———————————————————– //
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA

Test program for the embedded telnet client class.
*/
// ———————————————————– //
#include “gxdlcode.h”

#if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library
#include
using namespace std; // Use unqualified names for Standard C++ library
#else // Use the old iostream library by default
#include
#endif // __USE_ANSI_CPP__

#include
#include
#include
#include “gxtelnet.h”

#ifdef __MSVC_DEBUG__
#include “leaktest.h”
#endif

int CheckError(gxsTelnetClient *telnetc)
// Checks the specified stream socket for an error condition
{
if(telnetc->GetSocketError() != gxSOCKET_NO_ERROR) {
cout << "\n" << flush; cout << telnetc->SocketExceptionMessage() << flush << "\n"; telnetc->Close();
return 0;
}
return 1; // No errors reported
}

int main(int argc, char **argv)
{
#ifdef __MSVC_DEBUG__
InitLeakTest();
#endif

if(argc < 4) { cout << "You must specify host, username, and password" << "\n"; cout << "Example: " << argv[0] << " host usr passwd [port]" << "\n"; cout << "host = Host name of the telnet server" << "\n"; cout << "usr = Your user name" << "\n"; cout << "passwd = Your password" << "\n"; return 0; } char *hostname = argv[1]; char *username = argv[2]; char *password = argv[3]; unsigned short port = (unsigned short)23; if(argc == 5) port = atoi(argv[4]); gxsTelnetClient telnetc; // Test terminal emulations // telnetc.SetTermType(gxsTEL_NVT_TERM); // Default // telnetc.SetTermType(gxsTEL_ENV_TERM); // Use ENV TERM setting // telnetc.SetTermType(gxsTEL_VT100F_TERM); // Filtered VT100 emulation // Try with WIN32 consoles using the ANSI.SYS driver // telnetc.SetTermType("ANSI"); cout << "Connecting to " << hostname << " on port " << port << "\n"; cout << "Our terminal type is: " << telnetc.GetTermType() << "\n"; telnetc.ConnectClient(hostname, port); if(!CheckError(&telnetc)) return 1; telnetc.RecvInitSeq(); if(!CheckError(&telnetc)) return 1; const int sbuf_size = 4096; char sbuf[sbuf_size]; telnetc.RecvString(sbuf, sbuf_size, "login"); if(!CheckError(&telnetc)) { cout << sbuf << "\n" << flush; return 1; } cout << sbuf << flush; telnetc.SendString((const char *)username, strlen(username)); if(!CheckError(&telnetc)) return 1; telnetc.RecvString(sbuf, sbuf_size, "Password"); if(!CheckError(&telnetc)) { cout << sbuf << "\n" << flush; return 1; } cout << sbuf << "\n" << flush; telnetc.SendString((const char *)password, strlen(password)); if(!CheckError(&telnetc)) return 1; telnetc.RecvString(sbuf, sbuf_size, "$"); if(!CheckError(&telnetc)) { cout << sbuf << "\n" << flush; return 1; } cout << sbuf << "\n" << flush; telnetc.SendString("ls -l", strlen("ls -l")); if(!CheckError(&telnetc)) return 1; telnetc.RecvString(sbuf, sbuf_size, "$"); cout << sbuf << "\n" << flush; cout << "Closing the telnet connection..." << "\n"; cout << "Exiting..." << "\n"; telnetc.Close(); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- // [/cpp] FTP 예제 [cpp] // ------------------------------- // // -------- Start of File -------- // // ------------------------------- // // ----------------------------------------------------------- // // C++ Source Code File Name: testprog.cpp // C++ Compiler Used: MSVC, BCC32, GCC, HPUX aCC, SOLARIS CC // Produced By: DataReel Software Development Team // File Creation Date: 02/23/2001 // Date Last Modified: 01/01/2009 // Copyright (c) 2001-2009 DataReel Software Development // ----------------------------------------------------------- // // ------------- Program Description and Details ------------- // // ----------------------------------------------------------- // /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Test program for the embedded FTP client class. */ // ----------------------------------------------------------- // #include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include
using namespace std; // Use unqualified names for Standard C++ library
#else // Use the old iostream library by default
#include
#endif // __USE_ANSI_CPP__

#include
#include “gxsftp.h”

#ifdef __MSVC_DEBUG__
#include “leaktest.h”
#endif

int CheckError(gxsFTPClient *ftp, int close_on_err = 1)
{
if(ftp->GetSocketError() != gxSOCKET_NO_ERROR) {
cout << ftp->SocketExceptionMessage() << "\n" << flush; if(ftp->reply_buf[0] != 0) { // Check the reply buffer
cout << ftp->reply_buf << flush; } if(close_on_err == 1) { ftp->Close();
}
return 0;
}
return 1; // No errors reported
}

void ClearInputStream(istream &s)
// Used to clear istream
{
char c;
s.clear();
while(s.get(c) && c != ‘\n’) { ; }
}

void Menu()
{
cout << "\n" << flush; cout << "(a) Set image type to ASCII" << "\n" << flush; cout << "(A) Set active mode on (default)" << "\n" << flush; cout << "(b, B) Set image type to binary" << "\n" << flush; cout << "(c, C) Change working directory" << "\n" << flush; cout << "(d, D) Delete a file" << "\n" << flush; cout << "(h, H, ?) Display this menu" << "\n" << flush; cout << "(g, G) Get a file" << "\n" << flush; cout << "(l) Long directory listing" << "\n" << flush; cout << "(L) Short directory listing" << "\n" << flush; cout << "(M, m) Make a directory" << "\n" << flush; cout << "(n) Long named directory listing" << "\n" << flush; cout << "(N) Short named directory listing" << "\n" << flush; cout << "(p) Put a file" << "\n" << flush; cout << "(P) Set passive FTP mode on" << "\n" << flush; cout << "(q, Q) Quit this program" << "\n" << flush; cout << "(r, R) Remove directory" << "\n" << flush; cout << "(s, S) Display statistics" << "\n" << flush; cout << "(v, V) Move a file" << "\n" << flush; cout << "(w, W) Get working directory" << "\n" << flush; cout << "(z, Z) Get a file size in bytes" << "\n" << flush; cout << "\n" << flush; } void List(gxsFTPClient *ftp, int full) { const int sbuf_size = 8192; char sbuf[sbuf_size]; sbuf[0] = 0; ftp->FTPList(sbuf, sizeof(sbuf), full);
if(!CheckError(ftp, 0)) return;
cout << sbuf << flush << "\n" << flush; cout << ftp->reply_buf << flush; } void ListDir(gxsFTPClient *ftp, int full) { char name[255]; cout << "Enter file or directory name>“;
cin >> name;

const int sbuf_size = 8192;
char sbuf[sbuf_size];
sbuf[0] = 0;
ftp->FTPList(sbuf, sizeof(sbuf), full, name);
if(!CheckError(ftp, 0)) return;
cout << sbuf << flush << "\n" << flush; cout << ftp->reply_buf << flush; } void Stat(gxsFTPClient *ftp) { ftp->FTPStat();
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void PWD(gxsFTPClient *ftp) { ftp->FTPPWD();
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void ASC(gxsFTPClient *ftp) { ftp->FTPImageType(‘A’);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void Active(gxsFTPClient *ftp) { ftp->FTPActive();
cout << "Passive mode off" << "\n" << flush; } void Passive(gxsFTPClient *ftp) { ftp->FTPPassive();
cout << "Passive mode on" << "\n" << flush; } void BIN(gxsFTPClient *ftp) { ftp->FTPImageType(‘B’);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void ChDir(gxsFTPClient *ftp) { char sbuf[255]; cout << "Enter directory name>“;
cin >> sbuf;
ftp->FTPChDir((const char *)sbuf);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void MkDir(gxsFTPClient *ftp) { char sbuf[255]; cout << "Enter directory to make>“;
cin >> sbuf;
ftp->FTPMkDir((const char *)sbuf);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void RmDir(gxsFTPClient *ftp) { char sbuf[255]; cout << "Enter directory to remove>“;
cin >> sbuf;
ftp->FTPRmDir((const char *)sbuf);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void Delete(gxsFTPClient *ftp) { char sbuf[255]; cout << "Enter file name to delete>“;
cin >> sbuf;
ftp->FTPDelete((const char *)sbuf);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void Move(gxsFTPClient *ftp) { char from[255]; char to[255]; cout << "Enter file name to rename>“;
cin >> from;
cout << "Enter new name>“;
cin >> to;
ftp->FTPMove((const char *)from, (const char *)to);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void Size(gxsFTPClient *ftp) { char sbuf[255]; cout << "Enter file name>“;
cin >> sbuf;
ftp->FTPSize((const char *)sbuf);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; } void Get(gxsFTPClient *ftp) { char rfile[255]; char lfile[255]; cout << "Enter file name to download>“;
cin >> rfile;
cout << "Enter local file name>“;
cin >> lfile;

// Open the output and truncate if it already exists
DiskFileB stream(lfile, DiskFileB::df_READWRITE, DiskFileB::df_CREATE);
if(!stream) {
cout << "Could not open the " << lfile << " output file" << "\n" << flush; return; } FAU_t bytes; ftp->FTPGet((const char *)rfile, stream, bytes);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; cout << (long)bytes << " bytes transferred" << "\n" << flush; } void Put(gxsFTPClient *ftp) { char rfile[255]; char lfile[255]; cout << "Enter file name to upload>“;
cin >> lfile;
cout << "Enter remote file name>“;
cin >> rfile;

DiskFileB stream(lfile);
if(!stream) {
cout << "Could not open the " << lfile << " input file" << "\n" << flush; return; } FAU_t bytes; ftp->FTPPut((const char *)rfile, stream, bytes);
if(!CheckError(ftp, 0)) return;
cout << ftp->reply_buf << flush; cout << (long)bytes << " bytes transfered" << "\n" << flush; } int main(int argc, char **argv) { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif if(argc < 4) { cout << "Usage: " << argv[0] << " server username password [port]" << "\n" << flush; cout << "server = Your FTP server" << "\n" << flush; cout << "username = Your FTP account user name" << "\n" << flush; cout << "username = Your FTP account password" << "\n" << flush; cout << "port = Optional port number" << "\n" << flush; cout << "\n" << flush; cout << "Example: " << argv[0] << " ftp.xyz.com myname mypassword" << "\n" << flush; return 1; } char *server = argv[1]; char *username = argv[2]; char *password = argv[3]; unsigned short port = (unsigned short)21; if(argc == 5) port = atoi(argv[4]); gxsFTPClient ftp; ftp.SetTimeOut(60, 0); // Set a long time for remote servers cout << "\n" << flush; cout << "Connecting to ftp server" << "\n" << flush; ftp.ConnectClient((const char *)server, port); if(!CheckError(&ftp)) return 1; cout << ftp.reply_buf << flush; ftp.FTPLogin((const char *)username, (const char *)password); if(!CheckError(&ftp)) return 1; cout << ftp.reply_buf << flush; char key; Menu(); int rv = 1; while(rv) { if (!cin) { ClearInputStream(cin); if (!cin) { cout << "Input stream error" << "\n" << flush; return 0; } } cout << '>‘;
cin >> key;
if (!cin) continue;
switch(key) {
case ‘?’ : Menu(); break;
case ‘a’ : ClearInputStream(cin); ASC(&ftp); break;
case ‘A’ : ClearInputStream(cin); Active(&ftp); break;
case ‘b’ : case ‘B’ : ClearInputStream(cin); BIN(&ftp); break;
case ‘c’ : case ‘C’ : ClearInputStream(cin); ChDir(&ftp); break;
case ‘d’ : case ‘D’ : ClearInputStream(cin); Delete(&ftp); break;
case ‘g’ : case ‘G’ : ClearInputStream(cin); Get(&ftp); break;
case ‘h’ : case ‘H’ : Menu(); break;
case ‘l’ : ClearInputStream(cin); List(&ftp, 1); break;
case ‘L’ : ClearInputStream(cin); List(&ftp, 0); break;
case ‘m’ : case ‘M’ : ClearInputStream(cin); MkDir(&ftp); break;
case ‘n’ : ClearInputStream(cin); ListDir(&ftp, 1); break;
case ‘N’ : ClearInputStream(cin); ListDir(&ftp, 0); break;
case ‘p’ : ClearInputStream(cin); Put(&ftp); break;
case ‘P’ : ClearInputStream(cin); Passive(&ftp); break;
case ‘q’ : case ‘Q’ : rv = 0; break;
case ‘r’ : case ‘R’ : ClearInputStream(cin); RmDir(&ftp); break;
case ‘s’ : case ‘S’ : ClearInputStream(cin); Stat(&ftp); break;
case ‘v’ : case ‘V’ : ClearInputStream(cin); Move(&ftp); break;
case ‘w’ : case ‘W’ : ClearInputStream(cin); PWD(&ftp); break;
case ‘z’ : case ‘Z’ : ClearInputStream(cin); Size(&ftp); break;
default:
cout << "Unrecognized command" << "\n" << flush; break; } } ftp.FTPLogout(); if(!CheckError(&ftp)) return 1; cout << ftp.reply_buf << flush; cout << "\n" << flush; ftp.Close(); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- // [/cpp]

plink

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

plink는 putty에서 제공되는 배치모드가 제공되는 원격접속 프로그램.

예를 들어..

cmds.txt

ls
pwd
exit

접속..

c:\> plink -ssh -l 아이디 -pw 암호 텔넷주소 < cmds.tx [/cpp] 아래는 결과화면. [cpp] Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-8-server x86_64) * Documentation: http://www.ubuntu.com/server/doc System information as of Fri Feb 10 17:24:37 KST 2012 System load: 0.08 Processes: 100 Usage of /home: 46.7% of 819.03GB Users logged in: 0 Memory usage: 44% IP address for eth0: xxx.xxx.xxx.xxx Swap usage: 0% Graph this data and manage this system at https://landscape.canonical.com/ New release 'oneiric' available. Run 'do-release-upgrade' to upgrade to it. Last login: Fri Feb 10 17:24:18 2012 from xx.xx.xxx.xxx ls pwd exit xxxx@xxxx:~$ ls public_html xxxxl@xxxx:~$ xxxxl@xxxx:~$ pwd /home/xxxx xxxxl@xxxx:~$ xxxx@xxxx:~$ exit logout  [/cpp]

텔넷 기반의 BBS…

옛기억이 나서 한번 정리해본다.
너무 오래된 자료이고.. 요즘에 누가 텔넷 기반의 bbs를 운영하겠냐만은..

등대(Light House) 3.3
lhouse.7z

호롱불
사설 BBS 프로그램. 제작자 최오길.
horong.arj

xhost
C로 작성된 리눅스에서 돌아가는 bbs 호스트 프로그램이다. 디비를 사용하지 않고 파일을 사용한다.

xhost511.tgz (소스코드)
XHOST5.22d.zip (소스코드)

모닥불 호스트
C언어로 작성된 호스트 프로그램. 분석이 쉬워보인다.
790919549_camp.lzh (소스)
790919045_campdoc.lzh (설명서)

돌탱크 호스트
돌탱크 호스트. 소스포함.
811193339_dtank152.tgz

EZBBS
윈도우에서 사설 BBS 를 만들수 있는 프로그램.
ezbbs.zip ezbbs 소스코드

SyncTERM
출처: http://syncterm.bbsdev.net/
SDL위에서 돌아가는 ansi 코드를 지원하는 BBS 전용 접속 프로그램.
한글은 당연 지원이 되질 않는다.
소스코드도 공개가 되어 있으므로 한글지원은 쉬워보인다.
상당히 유용한 소스가 될것같다.
SyncTERM-0.9.3b-20090224-Setup.exe.7z
syncterm-20110209.7z (소스)

syncterm