The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
xrusna02 xsiebe03
Posted by: xrusna02 xsiebe03 | September 30, 2010 @ 8:32am
C++ Code
[
Download
]
// BPPA_SERVER.cpp : Defines the entry point for the console application. // // xsiebe03 (FEKT) // xrusna02 (FIT) #include <iostream> #include <stdio.h> #include <windows.h> #include <string> #include <winsock.h> #include <fstream> #pragma comment (lib,"WS2_32.lib") #define BUFSIZE 1000 using namespace std; int main(int argc, char *argv[]) { WORD wVersionRequested = MAKEWORD(1,1); // ��slo verze WSADATA data; // Struktura s info. o knihovn�; std::string text; // P�ij�man� text sockaddr_in sockName; // "Jm�no" soketu a ��slo portu sockaddr_in clientInfo; // Klient, kter� se p�ipojil SOCKET mainSocket; // Soket int port; // ��slo portu char buf[BUFSIZE]; // P�ij�mac� buffer int size; // Po�et p�ijat�ch a odeslan�ch byt� int addrlen; // Velikost adresy vzd�len�ho po��ta�e int count = 0; // Po�et p�ipojen� while(1){ if (WSAStartup(wVersionRequested, &data) != 0) { cout << "Nepodarilo se inicializovat sokety" << endl; return -1; } port = 80; if ((mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { cerr << "Nelze vytvorit soket" << endl; WSACleanup(); return -1; } // Zapln�me strukturu sockaddr_in // 1) Rodina protokol�; sockName.sin_family = AF_INET; // 2) ��slo portu, na kter�m �ek�me sockName.sin_port = htons(port); // 3) Nastaven� IP adresy lok�ln� s�ov� karty, p�es kterou // je mo�no se p�ipojit. // Nastav�me mo�nost p�ipojit se odkudkoliv. sockName.sin_addr.s_addr = INADDR_ANY; //p�i�ad�me soketu jm�no if (bind(mainSocket, (sockaddr *)&sockName, sizeof(sockName)) == SOCKET_ERROR) { cerr << "Problem s pojemnovanim soketu" << endl; WSACleanup(); return -1; } // Vytvo��me frontu po�adavk� na spojen�. // Vytvo��me frontu maxim�ln� velikosti 10 po�adavk�. cout << "Nasloucham na portu:" << port << endl; if (listen(mainSocket, 10) == SOCKET_ERROR) { cerr << "Problem s vytvorenim fronty" << endl; WSACleanup(); return -1; } // Pozna��m si velikost struktury clientInfo. // P�ed�m to funkci accept. addrlen = sizeof(clientInfo); // Vyberu z fronty po�adavek na spojen�. // "client" je nov� soket spojuj�c� klienta se serverem. SOCKET client = accept(mainSocket, (sockaddr*)&clientInfo, &addrlen); int totalSize = 0; if (client == INVALID_SOCKET) { cerr << "Problem s prijetim dat" <<endl; WSACleanup(); return -1; } // Zjist�m IP adresu klienta. cout << "Nekdo se pripojil z adresy: " << inet_ntoa((in_addr)clientInfo.sin_addr) << endl; // P�ijmu data. Ke komunikaci s klientem pou��v�m soket // "client" text = ""; // Prijmuti dat od clienta for (int i=0;i<BUFSIZE;i++) buf[i] = 0; while (totalSize <= 50) { if ((size = recv(client, buf, BUFSIZE-1, 0)) == SOCKET_ERROR) { cerr << "Problem s prijetim dat." << endl; WSACleanup(); return -1; } cout << "Prijato:" << size << endl; totalSize += size; text.append(buf); } //segmentace textu ze zpravy klienta int zero=text.find('\n'); text.erase(zero); string cesta; int zero3 = text.rfind(' '); text.erase(zero3); zero = text.find('/'); cesta.append(text,zero+1,(text.length())-(zero+1)); cout << endl << "Obsah pole je:" << cesta << endl; //Prace se souborem ifstream myfile; string line,obsah; myfile.open (cesta.c_str()); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); obsah +=line; obsah +='\n'; } cout << "Obsah souboru:" << endl << obsah; myfile.close(); } else { myfile.close(); myfile.clear(); myfile.open ("404.html"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); obsah +=line; obsah +='\n'; } cout << "Obsah souboru:" << endl << obsah; myfile.close(); } } string OK = "HTTP/1.1 200 OK\r\nAccept-ranges: bytes\r\nContent-length: 82\r\nContent-type: text/html\r\n"; // Odeslu potvrzeni komunikace if ((size = send(client, OK.c_str(), OK.length(), 0))==SOCKET_ERROR) { cerr << "Problem s odeslanim dat" << endl; WSACleanup(); return -1; } if ((size = send(client, obsah.c_str(), obsah.length(), 0))==SOCKET_ERROR) { cerr << "Problem s odeslanim dat" << endl; WSACleanup(); return -1; } cout << "Odeslano: " << size << endl; // Uzav�u spojen� s klientem closesocket(client); cout << "Koncim" << endl << endl << endl; closesocket(mainSocket); WSACleanup(); } return 0; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.55 / 8
53 Votes
http://codebin.yi.org/870
page generated in 0.00 seconds