【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

建立了连接:

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

通过命令操作电脑:

关闭cmd窗体:

附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. 求助(VC++) 隐藏Console窗体无效

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

  2. Effective C++ 第二版 8) 写operator new 和operator delete 9) 避免隐藏标准形式的new

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

  3. vue中使用console.log无效

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

  4. .net Console.ReadLine无效

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

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

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

  6. 条款九: 避免隐藏标准形式的new

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

  7. golang隐藏/显示window系统下的黑色命令窗(hide/show console)

    导入包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. 宝更容易使用比读IC卡信息的工具

    编程语言:VC++ 更新时间:2014.10.23 操作系统:windowAll 工具:PCSC读卡器 在上一个博文<<解惑:NFC手机怎样轻松读取银行卡信息?>>中,介绍了支 ...

  2. DDDLite的权限管理

    领域驱动设计实战—基于DDDLite的权限管理   在园子里面,搜索一下“权限管理”至少能得到上千条的有效记录.记得刚开始工作的时候,写个通用的权限系统一直是自己的一个梦想.中间因为工作忙(其实就是懒 ...

  3. 什么场景Hbase

    Hbase不太复杂,但适合于存储大量的数据资料.因为是商城系统:用户.商品.订单,店,卖家,这些数据是不适合复杂的关系Hbase. 有一个非常大的数据量订购,并经常来计算.只考虑存款订单Hbase. ...

  4. Android开发学习总结(五)——Android应用目录结构分析(转)

    一.手动创建android项目 手动创建一个Android项目,命名为HelloWorld,命令如下: android create project -n HelloWorld -t 1 -p E:/ ...

  5. Codeforces 327B-Hungry Sequence(素数筛)

    B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. Android与server通信中的方法(TCP)高效的安全完整

    我以前一直使用sockets实现emulator和PC进行通讯,卡了几天,最后请教其它人最终能够连接了.  错误原因是在IP和port,IP要用本机IP(在CMD输入ipconfig.注意:每次开机本 ...

  7. Chapter 3 Protecting the Data(3):创建和使用数据库角色

    原版的:http://blog.csdn.net/dba_huangzj/article/details/39639365.专题文件夹:http://blog.csdn.net/dba_huangzj ...

  8. WPF学习(11)2D绘图

    本篇我们来学习WPF的绘图,在2D绘图中主要有这么几个重要的类:Drawing.Visual和Shape,顺便讲下Brush和BitmapEffect. 1 2D绘图 1.1Drawing类 Draw ...

  9. SQL Server系统数据库备份最佳实践

    原文:SQL Server系统数据库备份最佳实践 首先了解主要的系统数据库: 系统数据库 master 包含登录信息和其他数据库的核心信息 msdb 存储作业.操作员.警报.备份还原历史.数据库邮件信 ...

  10. 怎么样linux下的目录名的目录,系统用来操作空间

    在Windows操作系统可以容易地创建\举\空删除的目录名格目录, 在linux我们需要一些特殊的处理能力实现上述功能. (1)创建一个目录 mkdir my\ first 此命令创建一个目录&quo ...