The UNIX and Linux Forums  
Hello and Welcome-tól az Egyesült Államokat, hogy az UNIX és Linux Forums? Köszönjük, hogy meglátogatta és csatlakozik Globális Közösség.

Go Back   A UNIX és Linux Forums > Top Fórumok > Magas szintű Programozás
.
google unix.com



Magas szintű Programozás Post kérdések C, C + +, Java, SQL, és más programozási nyelvek itt.

Több, UNIX és Linux fórum témák Ön által talált Hasznos
Szál Thread Starter Fórum Válaszok Utolsó hozzászólás
probléma aljzat olvasatban swap007 A UNIX a fejlett és szakértői Felhasználók 2 05-21-2008 02:08 AM
Probléma Kapcsolódás Socket Stevhp Magas szintű Programozás 6 04-30-2007 08:27 PM
Probléma HP Unix-írás közben a foglalat AshokG HP-UX 0 02-25-2005 02:19 AM
Socket A Agent007 Magas szintű Programozás 3 04-03-2004 09:15 PM
[A] Reuse kikötőben BSD aljzat Vagyis Magas szintű Programozás 1 11-28-2003 11:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Téma eszközök Keresés a téma Rate Thread Megjelenítési módok
  #1 (permalink)  
Old 03-29-2008
imdupeng imdupeng is offline
Regisztrált felhasználó
  
 

Join Date: Mar 2008
Hozzászólások: 1
Question HTTP Keep-Alive socket probléma

Hello everyone, I am a newbie UNIX / Linux socket programozással. Ez a projekt az osztályba, hogy én gondom.

\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u0

Azt akartam, hogy "Keep-Alive" HTTP kapcsolatokat a kiszolgáló egy apró web crawler projekt. Itt van a probléma: amikor megpróbáltam recv () az első oldalon, ez sikerült. Azonban a 2. követő recv () kap nulla bájt, amelyek tényleg nincs ötletem. Én tettem fel a "Keep-Alive" mezőben a kérelemben üzenetet küld, amikor felhívtam ().

Nem nagyon tudom, hogyan hajtsák végre a HTTP-kapcsolat segítségével Keep-Alive. Én ki az aljzat kapcsolódó kódot egy kis programot csatolták.

Tudna nézd meg a kódot, és elmagyarázza, mi volt a baj, hogy én? Ez lenne a nagy segítség, mert én ragasztani ezt a problémát a nap.

By the way, a program keretében összeállított SunOS használ g + + és lsocket.
\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u0

Kód:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <string>
#include <iostream>

using namespace std;

// ------------------------------------------------------
const short SOCKET_ERROR = -1;

const short RECV_BUFFER_SIZE = 20;
const short REQUEST_BUFFER_SIZE = 255;

int sock;
char recv_buf [RECV_BUFFER_SIZE + 1];
char req_buf [REQUEST_BUFFER_SIZE + 1];

const static char REQUEST_TEMPLATE [] = 
{
	"GET %s HTTP/1.1\r\n"
	"Host: xxx.xxx.xxx\r\n" // should be replaced with a really host
	"Connection: Keep-Alive\r\n"
	"\r\n"
};

// ------------------------------------------------------
void create_socket ();
void download (const string& path, string& response);

// ------------------------------------------------------

int main (void)
{
	string first_addr = "/~pdu/index.html"; // should be replaced with a really URL
	string second_addr = "/~pdu/a.html"; // should be replaced with a really URL
	string response;

	create_socket ();
	download (first_addr, response);
	cout << response << endl << endl;
	response = "";
	download (second_addr, response);
	if (response.size() > 0)
	{
		cout << response << endl;
	}
	else
	{
		cout << "### The 2nd recv() failed to receive any bytes from the socket!" << endl << endl;
	}
	
	close (sock);

	return 0;
}

void create_socket ()
{
	struct sockaddr_in addr;

	// ------------------------------------------------------
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(sock == SOCKET_ERROR)
    {
        perror ("Could not make a socket.\n");
		exit (-1);
    }

	cout << ">>> Socket created!" << endl;

	// ------------------------------------------------------
	struct hostent* host_info = gethostbyname("cse.unl.edu");	
	
	cout << ">>> DNS done!" << endl;

	long host_addr;

	/* copy address into long */
	memcpy(&host_addr, host_info->h_addr,
		host_info->h_length);

	/* fill address struct */
	addr.sin_addr.s_addr = host_addr;
	addr.sin_port = htons(80);
	addr.sin_family = AF_INET;

	// ------------------------------------------------------
	if( connect(sock, (struct sockaddr*)(&addr),
		sizeof(addr)) == SOCKET_ERROR )
    {
        perror("Could not connect to HTTP server.\n");
		exit (-1);
    }

	cout << ">>> Connection established!" << endl;
}

void download (const string& path, string& response) 
{
	size_t nBytes = snprintf(
		req_buf, 
		REQUEST_BUFFER_SIZE, 
		REQUEST_TEMPLATE, 
		path.c_str());

	if (nBytes >= REQUEST_BUFFER_SIZE)
	{
        cerr << "Buffer is too small for making a request message" << endl;
		exit (-1);
	}

	if (send(sock, req_buf, nBytes, 0) != nBytes)
	{
        perror("Could not send request to the HTTP server.\n");
		exit (-1);
	}
	cout << ">>> Request sent! -> " << path << endl << req_buf << endl;
	
	ssize_t size = 0;
	
	while ((size = recv(sock, recv_buf, 
		RECV_BUFFER_SIZE, 0)) > 0) 
	{
		recv_buf[size] = '\0';
		response.append(recv_buf);
    }

	cout << ">>> Response received!" << endl;
}
Köszönöm!
Closed Thread

Könyvjelzõk

Téma eszközök Keresés a téma
Keresés a téma:

Részletes keresés
Megjelenítési módok Rate this thread
Rate this thread:

Posting szabályzat
Ön nem post new threads
Ön nem post válaszok
Ön nem post Csatolmányok
Ön nem szerkeszteni az üzeneteidet

BB kód van Be
Smilies vannak Be
[IMG] kód Be
HTML kód Ki
Trackbacks vannak Be
Pingbacks vannak Be
Refbacks vannak Be




Minden idő GMT -4. Az idő most 01:18 AM.


Powered by: vBulletin, Copyright © 2000 - 2006, Jelsoft Enterprises Limited. Nyelvre lefordítva Powered by .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
A UNIX és Linux Fórum Tartalom Copyright © 1993-2009. Minden jog Reserved.Ad menedzsment RedTyger

Content Relevant URLs by vBSEO 3.2.0