Home > Software engineering >  C # client to the server (vc) do Get requests, but vc this always accept two requests
C # client to the server (vc) do Get requests, but vc this always accept two requests

Time:09-29

Do it himself a c + + Http server, dealing with Http get request,
Client is a c # to do, what has happened is that c # has launched a request, c + + can accept to two connections, is this why? Are there any understand c + + and c # programming and network
HTTP server-side code
 do 
{

//initialize the WSA
WORD sockVersion=MAKEWORD (2, 2);
WSADATA WSADATA;
CResult. IResult=WSAStartup (sockVersion, & amp; WsaData);
If (cResult. IResult)
{
CResult. StrErr="WSAStartup failed";
break;
}
//create a socket
SOCKET slisten=SOCKET (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (slisten==INVALID_SOCKET)
{
CResult. IResult=1;
CResult. StrErr="failed to create a socket";
break;
}

//binding IP and port
Sockaddr_in sin;
Sin. Sin_family=AF_INET;
Int iPort=g_cfgManger. GetListenPort ();
If (iPort & lt; 0 {
CResult. IResult=1;
CResult. StrErr="port illegal";
break;
}
Sin. Sin_port=htons (iPort);
Sin. The sin_addr. S_un. S_addr=INADDR_ANY;
If (bind (slisten, (LPSOCKADDR) & amp; Sin, sizeof (sin))==SOCKET_ERROR)
{
CResult. IResult=1;
CResult. StrErr="binding IP and port failure";
break;
}
//to start listening to
If (listen (slisten, 1)==SOCKET_ERROR)
{
CResult. IResult=1;
CResult. StrErr="monitoring error";
break;
}
//loop receiving data
The SOCKET sClient;
Sockaddr_in remoteAddr;
Int nAddrlen=sizeof (remoteAddr);
Char revData [255].
Int iCntText=0;
While (true)
{
ICntText++;
Printf (" listening \ n ");
SClient=accept (slisten, (SOCKADDR *) & amp; RemoteAddr, & amp; NAddrlen);
If (sClient==INVALID_SOCKET)
{
//printf (" accept error!" );
Printf (" listen to the wrong socket ");
continue;
}

//printf (" received a connection: % s \ r \ n ", inet_ntoa (remoteAddr. Sin_addr));//an error C4996: 'inet_ntoa' : Use inet_ntop () or InetNtop home () or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings d: \ \ linhs \ project multithreaded receives the file service \ myhttpserver \ myhttpserver \ myhttpserver CPP 115 1 myhttpserver
Printf (" receiving the first % d a connection: % s \ r \ n ", iCntText, inet_ntoa (remoteAddr. Sin_addr));
Char acBuf [1024]={0};
The recv (sClient acBuf, 1024, NULL);
DWORD dwThreadId;
NewCnctThdParam * cnctParam=new newCnctThdParam;
CnctParam - & gt; OneSocket=sClient;
CnctParam - & gt; StrRecvData=https://bbs.csdn.net/topics/acBuf;
CnctParam - & gt; PClass=this;
CreateThread (NULL, NULL, NewConnectThread cnctParam, NULL, & amp; DwThreadId);

}

Closesocket (slisten);
WSACleanup();
} while (0);


HTTP requests in c # code
 string Url="http://127.0.0.1/AskforTransport? FileSize=1111 "; 
//the string Url="http://www.ltaaa.com/";
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (Url);
Request. The Method="GET";
Request. The ContentType="text/HTML. Charset=utf-8 ";
Request. The KeepAlive=false;
HttpWebResponse response;
Try
{
The response=(HttpWebResponse) request. The method GetResponse ();
}
The catch (System. Exception ex)
{
MessageBox. Show (ex. ToString ());
}

CodePudding user response:

How many connection depends on the c # how internal implementation, it has nothing to do with your server, it to request what you respond to what we have to do is, first to parse the request line, find out the request method,
  • Related