473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(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
1 3767
The project is done... There was a problem with firewall...
Oct 21 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: jean | last post by:
I am very new to .Net and Visual Basic in general. My goal is to write a Visual Basic .Net DLL that can have its functions and subroutines called from another .Net application. My DLL will not...
4
by: Ron | last post by:
Greetings, below is a sample app for connecting to a mainframe server using Sockets for the purpose of using FTP service to interact with it from a PC. I got as far as creating the connection....
10
by: Robert Jacobson | last post by:
Hi, I'm develing a COM add-in for Microsoft Word XP that displays a form. I'd like to have the form display using the Windows XP theme. However, neither using a manifest nor calling...
36
by: AussieRules | last post by:
Hi, I want to use the user color scheme to set the color of my forms. I now I have to use the. System.Drawing.SystemColors, but which color is the color of a form background as used in other...
20
by: Jason Dravet | last post by:
Some time ago I wrote an application that controls the projectors in our classrooms(turn on, turn off, and volume control). This application is written in VB.net 2003. It was working perfectly...
0
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples...
0
by: mandarkraftware | last post by:
Hi All, Let me present you with list of consultants currently available. Name / Job Title Summary Ramchandran/Java 7+ years experience in the field of Software Development. Expertise...
5
by: Rajkiran R.B. | last post by:
I want to log in to a website using C# code.. The main aim is to check whether the username and password I provide is correct or not. I used the following code bool somefunction() {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.