68user's page 掲示板

Prev< No. 3778> Next  [最新発言に戻る] [過去ログ一覧]
No. 3778 # den 2004/06/14 (月) 21:42:32
ソースを載せます。

//***************************************************************
// From the book "Win32 System Services: The Heart of Windows 95
// and Windows NT"
// by Marshall Brain
// Published by Prentice Hall
//
// Copyright 1995, by Prentice Hall.
//
// This code implements a TCP sender.
//***************************************************************

// ssipsend.cpp

#include <windows.h>
#include <iostream.h>
#include <winsock.h>

#define NO_FLAGS_SET 0

#define PORT (u_short) 8080
#define DEST_IP_ADDR "127.0.0.1"

#define MAXBUFLEN 256

INT main(VOID)
{
    WSADATA Data;
    SOCKADDR_IN destSockAddr;
    SOCKET destSocket;
    unsigned long destAddr;
    int status;
    int numsnt;
    char toSendtxt[3][256];

    int numrcv;
    char buffer[MAXBUFLEN];

    strcpy( toSendtxt[0], "GET http://localhost:8080/example00/HitTime?userid=1207&date=20004/06/11%2008:30:00 HTTP/1.1\r\n" );
    strcpy( toSendtxt[1], "Host: localhost:8080\r\n" );
    strcpy( toSendtxt[2], "\r\n" );

    /* initialize the Windows Socket DLL */
    status=WSAStartup(MAKEWORD(1, 1), &Data);
    if (status != 0)
        cerr << "ERROR: WSAStartup unsuccessful"
            << endl;

    /* convert IP address into in_addr form */
    destAddr=inet_addr(DEST_IP_ADDR);
    /* copy destAddr into sockaddr_in structure */
    memcpy(&destSockAddr.sin_addr,
        &destAddr, sizeof(destAddr));
    /* specify the port portion of the address */
    destSockAddr.sin_port=htons(PORT);
    /* specify the address family as Internet */
    destSockAddr.sin_family=AF_INET;

    /* create a socket */
    destSocket=socket(AF_INET, SOCK_STREAM, 0);
    if (destSocket == INVALID_SOCKET)
    {
        cerr << "ERROR: socket unsuccessful" << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
            cerr << "ERROR: WSACleanup unsuccessful"
                << endl;
        return(1);
    }

    cout << "Trying to connect to IP Address: "
        << DEST_IP_ADDR << endl;

    /* connect to the server */
    status=connect(destSocket,
        (LPSOCKADDR) &destSockAddr,
        sizeof(destSockAddr));
    if (status == SOCKET_ERROR)
    {
        cerr << "ERROR: connect unsuccessful" << endl;
        status=closesocket(destSocket);
        if (status == SOCKET_ERROR)
            cerr << "ERROR: closesocket unsuccessful"
                << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
            cerr << "ERROR: WSACleanup unsuccessful"
                << endl;
        return(1);
    }

    cout << "Connected..." << endl;

    int idx = 0;
    while(1)
    {
    Sleep(1000);
        cout << "Sending..." << endl;
        numsnt=send(destSocket, toSendtxt[idx],
            strlen(toSendtxt[idx]) + 1, NO_FLAGS_SET);
        if (numsnt != (int)strlen(toSendtxt[idx]) + 1)
        {
            cout << "Connection terminated" << endl;
            status=closesocket(destSocket);
            if (status == SOCKET_ERROR)
                cerr << "ERROR: closesocket unsuccessful"
                    << endl;
            status=WSACleanup();
            if (status == SOCKET_ERROR)
                cerr << "ERROR: WSACleanup unsuccessful"
                    << endl;
            return(1);
        }
    idx++;
    if(idx>2){
        break;
    }

    /* Wait before sending the message again */
    //Sleep(4800);
    } /* while */

    return 0;
}

それほど難しいことはしていないはずなのですが...
よろしくお願いします。

Prev< No. 3778> Next  [最新発言に戻る] [過去ログ一覧]