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

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

Post by RGFydGgtQ3 » Thu, 23 Oct 2008 03:15:01


i,

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(System::Object^ sender, System::EventArgs^
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>^data = System::Text::Encoding::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::Show( "Sent: {0}", message );

// Receive the TcpServer::response.

// 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::Encoding::ASCII->GetString( data, 0, bytes );
MessageBox::Show( "Received: {0}", responseData );

// Close everything.
client->Close();
}
catch ( ArgumentNullException^ e )
{
MessageBox::Show( e->ToString(),"ArgumentNullException: {0}" );
}
catch ( SocketException^ e )
{
MessageBox::Show( e->ToString(),"SocketException: {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 wVersionRequested = 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(wVersionRequested, &data) != 0)
{
cout << "Nepoda艡ilo 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)
 
 
 

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

Post by RGFydGgtQ3 » Thu, 23 Oct 2008 05:00:02

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