标签:
#include <WINSOCK2.H> #include <stdio.h> // socket 套接字 #pragma comment (lib,"Ws2_32") //library DWORD WINAPI ThreadInput(LPVOID lpParam); DWORD WINAPI ThreadOutput(LPVOID lpParam); SOCKET sClient; void main() { //first 初始化DLL WSADATA wsadata; if (WSAStartup(MAKEWORD(2,2),&wsadata)!=0) { printf("Init Error\n"); return ; } //second 创建套接字 sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (sClient == INVALID_SOCKET) { printf("Create Listen Is Error\n"); return; } sockaddr_in LocalAddr; LocalAddr.sin_family = AF_INET; LocalAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //127.0.0.1 LocalAddr.sin_port = htons(8888); if (connect(sClient,(sockaddr*)&LocalAddr,sizeof(LocalAddr))==SOCKET_ERROR) { // WSAENETDOWN int a = GetLastError(); closesocket(sClient); printf("Connect Is Error\n"); return; } HANDLE hThread[2]; //这里是整个程序的核心部分我们一定要那个管道图 hThread[0] = CreateThread(NULL,0,ThreadOutput,NULL,0,NULL); hThread[1] = CreateThread(NULL,0,ThreadInput,NULL,0,NULL); WaitForMultipleObjects(2,hThread,TRUE,INFINITE); } DWORD WINAPI ThreadInput(LPVOID lpParam) { SOCKET s = sClient; //该线程函数是从套接字中获取信息并且通过管道注入cmd的工作函数 char Buffer[4096] = {0}; DWORD dWrite = 0; DWORD dwRecvd = 0; while (TRUE) { scanf("%s",Buffer); send(s,Buffer,strlen(Buffer),0); } return 0; } DWORD WINAPI ThreadOutput(LPVOID lpParam) { SOCKET s = sClient; //该线程函数是从被控端返给控端信息的线程函数 char Buffer[4096]={0}; while (TRUE) { int Ret = recv(s,Buffer,4096,0); if(Ret<0) { break; } Buffer[Ret]=‘\0‘; printf("%s",Buffer); } return 0; }
标签:
原文地址:http://www.cnblogs.com/yifi/p/4548102.html