By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(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) ─î├*slo portu, ke kter├ęmu se p┼Öipoj├*me
serverSock.sin_port = 80;
// 3) Nastaven├* IP adresy, ke kter├ę se p┼Öipoj├*me
memcpy(&(serverSock.sin_addr), host->h_addr, host->h_length);
// P┼Öipojen├* soketu
if (connect(mySocket, (sockaddr *)&serverSock, sizeof(serverSock)) == -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(mySocket);
WSACleanup();
cout << endl << text << endl;
return 0;
}

Oct 21 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The project is done... There was a problem with firewall...
Oct 21 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.