Windows Socket 代码贴着防忘

来源:互联网 发布:衣服销售软件 编辑:程序博客网 时间:2024/05/10 05:12
// ADBSocket.cpp : ADB服务访问测试
//


#include "stdafx.h"
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h > 


#pragma comment( lib, "ws2_32.lib" )


#define PORT 5037
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 4096


const char* addr = "127.0.0.1";



int _tmain(int argc, _TCHAR* argv[])
{
int iClientSock;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
int numbytes;
// struct hostent *he;
WSADATA WSAData;


if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}


if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}


ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
ServerAddr.sin_addr.s_addr = inet_addr( addr );
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );


if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "connect失败!" );
WSACleanup( );
exit( 0 );
}


char sendDataBuf[1024];
char* conData ="host:transport-any";
char* sendData = "shell:ls";


sprintf(sendDataBuf, "%04x", strlen(conData));
numbytes = send(iClientSock, sendDataBuf, 4, 0);
sprintf(sendDataBuf, conData);
numbytes = send(iClientSock, sendDataBuf, strlen(conData), 0);


numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );


sprintf(sendDataBuf, "%04x", strlen(sendData));
numbytes = send(iClientSock, sendDataBuf, 4, 0);
sprintf(sendDataBuf, sendData);
numbytes = send(iClientSock, sendDataBuf, strlen(sendData), 0);


numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );


if( numbytes == -1 )
{
printf( "recv失败!" );
WSACleanup( );
exit( 0 );
}


buf[ numbytes ] = '\0';


printf( "Received: %s", buf );


closesocket( iClientSock );
WSACleanup( );


return 0;
}


原创粉丝点击