for client
#include <stdio.h>
#include "winsock2.h"
#include <iostream>
using namespace std;
int 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 1;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
char ip[15];
cout<<"Enter ip:";
cin>> ip;
clientService.sin_addr.s_addr = inet_addr( ip );
clientService.sin_port = htons( 27015 );
if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return 1;
}
// Send and receive data.
char sendbuf[32];
// Command to be executed
char recvbuf[20] = "";
int bytesRecv = SOCKET_ERROR;
while(sendbuf!=0)
{
cout<<"Root:";
cin>> sendbuf;
send( m_socket, sendbuf, strlen(sendbuf), 0 );
bytesRecv=recv( m_socket, recvbuf, 20, 0 );
if ( bytesRecv == -1 )
{
printf( "Connection Closed.\n");
return 1;
}
cout<< recvbuf;
}
}
for server
#include <stdio.h>
#include "winsock2.h"
int 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 1;
}
// Bind the socket.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = htonl(INADDR_ANY);
service.sin_port = htons( 27015 );
if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
printf( "bind() failed.\n" );
closesocket(m_socket);
return 1;
}
// Listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");
// Accept connections.
SOCKET AcceptSocket;
printf( "Waiting for a client to connect to server...\n" );
while (1) {
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( m_socket, NULL, NULL );
}
printf( "Client Connected.\n");
m_socket = AcceptSocket;
while(1)
{
char recvbuf[32] = "";
char sendbuf[32] = "Command executed\n\n";
recv( m_socket, recvbuf, 32, 0 );
system(recvbuf); // Executes a remote command
send( m_socket, sendbuf, strlen(sendbuf), 0 );
}
}
return 1;
}
Thank you.
#include <stdio.h>
#include "winsock2.h"
#include <iostream>
using namespace std;
int 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 1;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
char ip[15];
cout<<"Enter ip:";
cin>> ip;
clientService.sin_addr.s_addr = inet_addr( ip );
clientService.sin_port = htons( 27015 );
if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return 1;
}
// Send and receive data.
char sendbuf[32];
// Command to be executed
char recvbuf[20] = "";
int bytesRecv = SOCKET_ERROR;
while(sendbuf!=0)
{
cout<<"Root:";
cin>> sendbuf;
send( m_socket, sendbuf, strlen(sendbuf), 0 );
bytesRecv=recv( m_socket, recvbuf, 20, 0 );
if ( bytesRecv == -1 )
{
printf( "Connection Closed.\n");
return 1;
}
cout<< recvbuf;
}
}
for server
#include <stdio.h>
#include "winsock2.h"
int 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 1;
}
// Bind the socket.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = htonl(INADDR_ANY);
service.sin_port = htons( 27015 );
if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
printf( "bind() failed.\n" );
closesocket(m_socket);
return 1;
}
// Listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");
// Accept connections.
SOCKET AcceptSocket;
printf( "Waiting for a client to connect to server...\n" );
while (1) {
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( m_socket, NULL, NULL );
}
printf( "Client Connected.\n");
m_socket = AcceptSocket;
while(1)
{
char recvbuf[32] = "";
char sendbuf[32] = "Command executed\n\n";
recv( m_socket, recvbuf, 32, 0 );
system(recvbuf); // Executes a remote command
send( m_socket, sendbuf, strlen(sendbuf), 0 );
}
}
return 1;
}
Thank you.
0 comments:
Post a Comment