int _tmain(int argc, _TCHAR* argv[])
{
WinSockStartup();
//declarations
char data[255]=" server data",buff[255];
int len,port,ch,y=1,n=0, no_of_threads=0,CP_ret=0;//,Sel_ret=0,itr=0,max_fd;
struct sockaddr_in new_addr;
int sockfd,data_len_recv=-1,on=1,accfd=0,len_addr,data_len_send=-1;
//file writing attributes
char FileName[1024],logdata[1024];
SYSTEMTIME st;
//setting initial values
memset(&new_addr,0,sizeof(new_addr));
memset(&sockfd,0,sizeof(int));
memset(&accfd,0,sizeof(int));
memset(&len_addr,sizeof(struct sockaddr),sizeof(int));
memset(&port,0,sizeof(int));
memset(buff,0,sizeof(buff));
//create process atrribute
STARTUPINFO si;
PROCESS_INFORMATION pi_child,pi_parent,pi_current;
ZeroMemory( &si, sizeof(STARTUPINFO) );
si.cb = sizeof(STARTUPINFO);
ZeroMemory( &pi_child, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &pi_parent, sizeof(PROCESS_INFORMATION) );
//select attributes
struct timeval soctimeval;
soctimeval.tv_sec = 20;
soctimeval.tv_usec = 0;
fd_set read_fd_set;
FD_ZERO(&read_fd_set);
printf("enter the port number\n");
scanf("%d",&port);
new_addr.sin_family= AF_INET;
for(int y=0;y<7;y++)
new_addr.sin_zero[y]= 0;
new_addr.sin_port=htons(port);
new_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//get parent process id
pi_parent.dwProcessId= GetCurrentProcessId();
//socket making
sockfd =socket(new_addr.sin_family, SOCK_STREAM,IPPROTO_TCP);
if(sockfd==-1)
{
printf("error in opening socket %d \n",WSAGetLastError());
}
else
printf("Socket made Successfully\n");
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
//bind socket
if((bind(sockfd,(struct sockaddr *)&new_addr,sizeof(struct sockaddr)))==-1)
{
printf("bind error %d \n",WSAGetLastError());
}
else
printf("Binded Successfully\n");
//listen socket
if( listen(sockfd,10)==-1)
{
printf("Error in listening %d \n",WSAGetLastError());
}
else
printf("Listened Successfully\n");
while(1)
{
//accept
accfd= accept(sockfd,(struct sockaddr *)&new_addr,&len_addr);
if(accfd==-1)
{
printf("error in accept %d \n",WSAGetLastError());
}
else
printf("Accepted Successfully\n");
//creating child process for multiple clients
no_of_threads ++;
printf("client no: %d\n",no_of_threads);
CP_ret=CreateProcess(TEXT("C:\\Users\\richa-a\\Desktop\\create_process_multiclient1\\client_server\\Debug\\server_tcp.exe"),
NULL,
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
(BOOL)1, // Set handle inheritance to TRUE
DETACHED_PROCESS, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi_child );
pi_current.dwProcessId=GetCurrentProcessId(); //even after the create process. the current id is still of the parent.
if(pi_parent.dwProcessId==pi_current.dwProcessId || CP_ret==0)
{
printf("\nparent id is %d\n",pi_parent.dwProcessId);
printf("process id of child is %d \n",pi_child.dwProcessId);
printf("process id of current function is %d \n",pi_current.dwProcessId);
printf("child is nt running the code\n");
}
//pi_current.dwProcessId=GetCurrentProcessId();
if(pi_child.dwProcessId==pi_current.dwProcessId)
{
//opening file
GetLocalTime (&st);
sprintf(FileName,"D:\\Serve_Client\\Child_Process_%d_%d__%d.%d.%d.%d.log",st.wDay,st.wMonth,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
Child_FPtr=fopen(FileName,"wb");
sprintf(logdata,"logging start for client %d\n",no_of_threads);
LogInfo(logdata);
//fwrite(logdata,sizeof(logdata),sizeof(logdata),Child_FPtr);
sprintf(logdata,"process id of parent is %d\n",pi_parent.dwProcessId);
LogInfo(logdata);
sprintf(logdata,"process id of child is %d \n",pi_child.dwProcessId);
LogInfo(logdata);
sprintf(logdata,"process id of current function is %d \n",pi_current.dwProcessId);
LogInfo(logdata);
sprintf(logdata,"child is running the code\n");
LogInfo(logdata);
//recv data
data_len_recv=recv(accfd,(char *)buff, sizeof(buff)-1,0);
buff[data_len_recv]='\0';
sprintf(logdata,"%s",buff);
LogInfo(logdata);
if(data_len_recv==sizeof(buff))
{
sprintf(logdata,"data rev Successfully\n");
LogInfo(logdata);
sprintf(logdata,"Recv data: %s\n",buff);
LogInfo(logdata);
}
else if(data_len_recv<sizeof(buff))
{
sprintf(logdata,"hlf data rev Successfully\n");
LogInfo(logdata);
sprintf(logdata,"Recv data: %s\n",buff);
LogInfo(logdata);
}
else if(data_len_recv==-1 || data_len_recv==0)
{
sprintf(logdata,"error in recv %d \n",WSAGetLastError());
LogInfo(logdata);
}
//send data
data_len_send=send(accfd,(const char *)data, sizeof(data),0);
if(data_len_send==sizeof(data))
{
sprintf(logdata,"data send Successfully\n");
LogInfo(logdata);
sprintf(logdata,"data Send: %s\n",data);
LogInfo(logdata);
}
else if(data_len_send<sizeof(data)&& data_len_send!=0)
{
sprintf(logdata,"hlf data send Successfully\n");
LogInfo(logdata);
sprintf(logdata,"data Send: %s\n",data);
LogInfo(logdata);
}
else if(data_len_send==-1)
{
sprintf(logdata,"error %d \n",WSAGetLastError());
LogInfo(logdata);
}
closesocket(accfd);
return 0;
}//child end
}//while loop end
closesocket(sockfd);
printf("socket closed\n");
WinSockCleanup();
printf("cleanup done\n");
getch();
return 0;
}
我制作了一个服务器和多客户端进程。在创建过程之后,我需要将当前程序ID更改为子ID以运行程序的其余部分。我希望子进程运行发送并接收我的服务器部分
请帮我解决这个问题。
答案 0 :(得分:2)
进程启动时会为进程分配一个id。没有办法改变它。这也意味着你无法将它转移给任何人。
发布的代码片段不完整。目前尚不清楚它在做什么。
您似乎也在混淆CreateProcess
与Unix fork
。它们的语义非常不同。