The problem with sockets using Visual C++ windows form application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RGFydGgtQ3ot?=

    The problem with sockets using Visual C++ windows form application

    Hi,

    I'm beginner in Visual C++ so I want to ask you a question. And I'm not good
    in English:D
    So I have the program which communicates over sockets. The program has to
    connect to the server and send and receive data. But when I start the
    program, there is one Socketexception - something like the program is unable
    to connect to the server or the server didn't answer...
    I apologize for my English... Thank you for advice...

    I made this code:

    private: System::Void Form1_Load(Syst em::Object^ sender, System::EventAr gs^
    e) {
    try
    {
    Int32 port = 80;
    TcpClient^ client = gcnew TcpClient( "www.centrum.cz ",port );

    // Translate the passed message into ASCII and store it as a Byte array.
    array<Byte>^dat a = System::Text::E ncoding::ASCII->GetBytes( "GET /
    HTTP/1.1\n\n" );

    // Get a client stream for reading and writing.
    // Stream stream = client->GetStream();

    NetworkStream^ stream = client->GetStream();

    // Send the message to the connected TcpServer.
    stream->Write( data, 0, data->Length );
    String^ message;
    MessageBox::Sho w( "Sent: {0}", message );

    // Receive the TcpServer::resp onse.

    // Buffer to store the response bytes.
    data = gcnew array<Byte>(256 );

    // String to store the response ASCII representation.
    String^ responseData = String::Empty;

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream->Read( data, 0, data->Length );
    responseData = System::Text::E ncoding::ASCII->GetString( data, 0, bytes );
    MessageBox::Sho w( "Received: {0}", responseData );

    // Close everything.
    client->Close();
    }
    catch ( ArgumentNullExc eption^ e )
    {
    MessageBox::Sho w( e->ToString(),"Ar gumentNullExcep tion: {0}" );
    }
    catch ( SocketException ^ e )
    {
    MessageBox::Sho w( e->ToString(),"So cketException: {0}" );
    }

    }

    The same problem I have when I do console application:

    // chatik2.cpp : Defines the entry point for the console application.
    //
    #include <stdafx.h>

    #define BUFSIZE 1000

    using namespace std;

    int main(void)
    {
    WORD wVersionRequest ed = MAKEWORD(1,1); // Číslo verze
    WSADATA data; // Struktura s info. o knihovnÄ›
    string text("GET HTTP/1.0\r\n"); // Odesílaný a
    přijímaný text
    hostent *host; // Vzdálený počítač
    sockaddr_in serverSock; // Vzdálený "konec potrubí"
    int mySocket; // Soket
    int port; // Číslo portu
    char buf[BUFSIZE]; // Přijímací buffer
    int size; // Počet přijatých a odeslaných bytů

    // Připravíme sokety na práci
    if (WSAStartup(wVe rsionRequested, &data) != 0)
    {
    cout << "Nepodařil o se inicializovat sokety" << endl;
    // Podle všeho, zde se WSACleanup volat nemusí.
    return -1;
    }
    port = 80;
    // Zjistíme info o vzdáleném počítači
    if ((host = gethostbyname(" www.seznam.cz") ) == NULL)
    {
    cerr << "Špatná adresa" << endl;
    WSACleanup();
    return -1;
    }
    // Vytvoříme soket
    if ((mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    {
    cerr << "Nelze vytvořit soket" << endl;
    WSACleanup();
    return -1;
    }
    // Zaplníme strukturu sockaddr_in
    // 1) Rodina protokolů
    serverSock.sin_ family = AF_INET;
    // 2) Číslo portu, ke kterému se připojíme
    serverSock.sin_ port = 80;
    // 3) Nastavení IP adresy, ke které se připojíme
    memcpy(&(server Sock.sin_addr), host->h_addr, host->h_length);
    // Připojení soketu
    if (connect(mySock et, (sockaddr *)&serverSock, sizeof(serverSo ck)) == -1)
    {
    cerr << "Nelze navázat spojení" << endl;
    WSACleanup();
    return -1;
    }
    // Odeslání dat
    if ((size = send(mySocket, text.c_str(), text.size() + 1, 0)) == -1)
    {
    cerr << "Problém s odesláním dat" << endl;
    WSACleanup();
    return -1;
    }
    cout << "Odesláno " << size << endl;
    // Příjem dat
    text = "";
    while (((size = recv(mySocket, buf, BUFSIZE, 0)) != 0) && (size != -1))
    {
    cout << "Přijato " << size << endl;
    text += buf;
    }
    if (size == -1)
    {
    cout << "Nelze přijmout data" << endl;
    }
    // Uzavřu spojení
    closesocket(myS ocket);
    WSACleanup();
    cout << endl << text << endl;
    return 0;
    }

  • =?Utf-8?B?RGFydGgtQ3ot?=

    #2
    RE: The problem with sockets using Visual C++ windows form application

    The project is done... There was a problem with firewall...

    Comment

    Working...