Home > Back-end >  Sockt HTTP problem
Sockt HTTP problem

Time:11-22

Everybody is good, small white a c, practice writing HTTP server, the code is as follows:
Problem;
The following code to compile a good run in the CMD window after,
1. Enter http://localhost:8088/test9.html in the browser, test9. HTML file doesn't exist,
Is meant to the browser output error message: "error! Check the request file name and the way!" , but the browser is a blank, but in the CMD debugging output, there is a HTTP poison
Discussion output string ah, as shown in figure 1 below, could you tell me which line of code is out of the question

2. Enter http://localhost:8088/test.html in the browser, the test. The HTML file exists, but the file content is not complete output
As shown in figure 2, can you tell me where the problem is,



 
//TCPS. CPP: defines the entry point of the console application,
//the server


#include

#include
#include


# include "stdafx. H"
#include
#include

# pragma comment (lib, "WS2_32. Lib")

# define BUF_SIZE 2048
# define BUF_SMALL 100
# define PORT 8888

Unsigned WINAPI RequestHandler (void * arg);
Char * ContentType file (char *);
Void SendData publishes the event (SOCKET sock, char * ct, char * fileName);

Void SendErrorMSG (SOCKET sock);
Void ErrorHandling (char * message);

Char recvData [BUF_SIZE];
Int nclients=0;



Int main (int arg c, char * argv [])
{

//initialize the dynamic link library ws2_32. DLL, negotiate version
WORD sockVersion=MAKEWORD (2, 2);
WSADATA WSADATA;
The SOCKET hServSock hClntSock;
SOCKADDR_IN servAdr clntAdr;


HANDLE hThread;
DWORD dwThreadID;
Int clntAdrSize;

If (arg c!
=2){
Usage: printf (" % s & lt; Port> \ n ", argv [0]);
The exit (1);
}

If (WSAStartup (sockVersion, & amp; WsaData)!=0)
{
ErrorHandling (" WSAStartup () error!" );
}
//create a socket socket, using TCP protocol specified
HServSock=socket (PF_INET SOCK_STREAM, 0).
If (hServSock==INVALID_SOCKET)
{
//printf (" failed to create a socket! \n");
//return 0;
ErrorHandling (" failed to create a socket!" );
}

//populate the server address information, binding IP address, protocol and port
//struct sockaddr_in server;
Memset (& amp; ServAdr, 0, sizeof (servAdr));

ServAdr. Sin_family=AF_INET;
ServAdr. Sin_addr. S_addr=htonl (INADDR_ANY);
ServAdr. Sin_port=htons (atoi (argv [1]));

If (bind (hServSock, (LPSOCKADDR) & amp; ServAdr, sizeof (servAdr))==SOCKET_ERROR)
ErrorHandling (" binding address failed!" );

//to start listening to
If (listen (hServSock, 5)==SOCKET_ERROR)
ErrorHandling (" listen () error ");

Printf (" server has started ");

//loop receiving data
//SOCKET clientsocket;
//struct sockaddr_in clientaddr;
//int nclientaddrlen=sizeof (clientaddr);

While (1)
{
//printf (" \ n wait for connection... \n");
ClntAdrSize=sizeof (clntAdr);
HClntSock=accept (hServSock, (SOCKADDR *) & amp; ClntAdr, & amp; ClntAdrSize);
If (hClntSock==INVALID_SOCKET)
{
Printf (" accept the client connection error!" );
continue;
}

//note that if written in the following sentence is not to the HTTP server, otherwise, the browser will not have any display, the output is no walk in the HTTP protocol
//a have to open a multithreaded mode again, otherwise it will cause the browser client communication congestion
//printf (" % s client connection success! . \ n ", inet_ntoa (clientaddr sin_addr));

Printf (" \ n the connection Request: % s: % d \ n ", inet_ntoa (clntAdr. Sin_addr), ntohs (clntAdr. Sin_port));
HThread=(HANDLE) _beginthreadex (NULL, 0, RequestHandler, (void *) hClntSock, 0, (unsigned *) & amp; DwThreadID);
}


//close the server socket, clean up and release the related resources
Closesocket (hServSock);
WSACleanup ();
return 0;
}

Unsigned WINAPI RequestHandler (void * arg)
{
The SOCKET hClntSock=arg (SOCKET);
Char buf [BUF_SIZE];
Char method [BUF_SMALL];
Char ct [BUF_SMALL];
Char fileName [BUF_SMALL];
Int ret=recv (hClntSock, buf, BUF_SIZE, 0);
If (STRSTR (buf, "HTTP/")==NULL)
{
SendErrorMSG (hClntSock);
Closesocket (hClntSock);
return 1;
}

Strcpy (method, strtok (buf, "/"));
If (STRCMP (method, "GET"))
SendErrorMSG (hClntSock);

Strcpy (fileName, strtok (NULL, "/"));
Printf (" fileName: % s \ n ", fileName);

Strcpy (ct, ContentType (fileName));


SendData publishes the event (hClntSock, ct, fileName);
return 0;

}

Void SendData publishes the event (SOCKET sock, char * ct, char * fileName)
{

//send data
Char protocol []="HTTP/1.0 200 OK \ r \ n";
Char servName []="Server: simple web Server \ r \ n";
Char cntLen []="the Content - length: 2048 \ r \ n";
Char cntType [BUF_SMALL];
Char buf [BUF_SIZE];
The FILE * sendFile;
Memset (buf, 0, sizeof (buf));

Sprintf (cntType, "content-type: % s \ r \ n \ r \ n", ct);
//printf (" -- - "cntType - % s, cntType);

If ((sendFile=fopen (fileName, "r"))==NULL)
{
Printf (" % s fopen error! \ n ", fileName);
SendErrorMSG (sock);
return;
}
/* the transmission header file */
Send (the sock, protocol, strlen (protocol), 0).
Printf (" % s ", protocol);

Send (the sock, servName, strlen (servName), 0).
Printf (" % s ", servName);

Send (the sock, cntLen, strlen (cntLen), 0).
Printf (" % s ", cntLen);

Send (cntType sock, strlen (cntType), 0).
Printf (" % s ", cntType);


/* * transfer requests data/

while (! The feof (sendFile))
{
Memset (buf, 0, sizeof (buf));
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related