초 간단 텔넷 접속 C 예제...
#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;
}
'블로그 (Blog) > 개발로그 (Devlogs)' 카테고리의 다른 글
sed 를 이용한 newline 제거 (0) | 2025.02.17 |
---|---|
C에서 Python 호출하기 (0) | 2025.02.17 |
FTP 라이브러리 (0) | 2025.02.17 |
ffmpeg 를 이용한 비디오 플레이어 만들기 (0) | 2025.02.17 |
wxWidgets + SDL 의 조합 (0) | 2025.02.17 |