|
|
Did you mean that it's not possible to declare the class in a h file and the implementation in a cpp file?
I have the same trouble as I convert my list which use void* to use template. But I don't move the code from cpp to h and when I link the library, it's ok. But when I link the application , I get link error.
Does it exist another way to move the implementation from cpp to h for solving this kind of problem?
Bruno
|
|
|
|
|
When you use your template class in more than one source file you should put the class declaration and the function definition in the same header file. Most template libraries like STL, ATL, WTL ship without cpp files.
Oliver
|
|
|
|
|
Hello
I am currently working on a SERVER which could send/receive string using Win32 Socket.... Could someone please send me a simple example?
I am a real beginner using Win32...
PLEASE HELP ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
take a look at jon sagara's triple-post below. rcopy.cpp and server.cpp should get you started. He's written them for unix but the basic principles are essentially the same
Nish
Sonork ID 100.9786 voidmain
www.busterboy.org
Nish is a BIG fan of Goran Ivanisevic
|
|
|
|
|
|
Hi guys,
I'm in a bit of a bind (haha, I made a funny). I'm writing a small file copying application for a class. We're trying to emulate the datalink layer on Unix by implementing a stop-and-wait protocol using UDP datagrams.
I have two separate programs:
rcopy is the client. It requests a file from the server. If the file does not exist, rcopy exits gracefully.
server is the server (naturally). It accepts requests from clients who want to copy a file. If the file does not exist on the server, then the server sends a flag back to rcopy basically saying "File does not exist."
I can get the datagrams to work one way: I can get the file name from the command line in rcopy and successfully send that to server. server can then attempt to open the file, and based on the outcome, can prepare a flag to send back to rcopy.
*** Here's the problem ***
When I call recvfrom(...) on the server to get the file name from rcopy, the sockaddr_in parameter is not getting sent. Therefore, I can't send the file ACK back to rcopy, and rcopy blocks indefinitely.
Anyhow, I have been trying to get this for *several* hours now, and I'm no further along than when I started. I hope one of you networking gurus will be able to help me out here.
Many thanks in advance for any help.
Note - I will post my code below because I'm not sure of the max size of these messages.
Thanks again,
Jon Sagara
"After all is said and done, usually more is said than done." -- Unknown
|
|
|
|
|
// ============================================================================
// rcopy.cc
//
// This program is repsonsible for taking the file names as command line
// arguments and communicating with the server program to request the remote-
// file. This program will then receive the file and store it to disk using
// the local-file name. Thr program will be run as:
//
// rcopy remote-file local-file remote-machine remote-port error-percent
//
// where:
//
// remote-file: is the file on the remote machine to be copied
// local-file: is the file cpied into on the local machine
// remote-machine: is the remote machine running the server
// remote-port: is the port number of the server application
// error-percent: is the percent of packets that are in error (floating
// poing number)
//
// Author: Jon Sagara
//
// This file represents the CLIENT.
// ============================================================================
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <stream.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include "networks.h"
#include "sendtoErrB.h"
int CreateUDPSocket(struct sockaddr_in &remote, char *szHostName, char *szPort);
int main(int argc, char *argv[])
{
int nSocket; // Socket descriptor.
int nBytesRead = 0; // Number of bytes read from server.
int nBytesSent = 0; // Number of bytes send to server.
int nSendLen = 0; // Number of bytes to send to server.
int nRecvLen = 0; // Number of bytes received from server.
float fErrorPct = 0.0; // Percent of packets that are in error.
sockaddr_in recv; // Socket address passed by recvfrom().
sockaddr_in remote; // Socket address for remote socket (server).
unsigned char recv_buf[BUFF_SIZE];
unsigned char send_buf[BUFF_SIZE];
//
// Check command line parameters.
//
if (6 != argc)
{
cout << "usage: " << argv[0] << " remote-file local-file"
<< "remote-machine remote-port error-percent\n";
exit(1);
}
fErrorPct = atof(argv[5]);
//
// Create the socket.
//
nSocket = CreateUDPSocket(remote, argv[3], argv[4]);
cout << "nSocket right after socket creation: " << nSocket << endl;
cout << "Enter the data to transmit: ";
cin.getline((char *) send_buf, BUFF_SIZE);
nSendLen = strlen((char *) send_buf) + 1;
cout << send_buf << " len: " << nSendLen << endl;
//
// Send data to server.
//
cout << "nSocket right before sending to server: " << nSocket << endl;
nBytesSent = sendto(nSocket, send_buf, nSendLen, 0, (struct sockaddr *) &remote, sizeof(remote));
if (nBytesSent < 0)
{
perror("Error sending data in rcopy. Trying to send to server.");
close(nSocket);
exit(1);
}
//
// Read data back from server.
//
nBytesRead = recvfrom(nSocket, recv_buf, BUFF_SIZE, 0, (struct sockaddr *) &recv, &nRecvLen);
if (nBytesRead < 0)
{
perror("Error reading data in rcopy. Trying to read back from server.");
close(nSocket);
exit(1);
}
cout << "recv_buf: " << recv_buf << endl;
cout << "nRecvLen: " << nRecvLen << endl;
close (nSocket);
return 0;
}
// ----------------------------------------------------------------------------
// CreateUDPSocket(...)
//
// Creates a UDP socket that connects to the server application on a remote
// machine. The applications port is passed in on the command line.
//
// Parameters:
// struct sockaddr_in &remote [IN/OUT]:
// Socket address for remote socket.
// char *hostName [IN]:
// Name of the remote host.
//
// Assumes: The function has been called correctly from the command line. Also
// assumes that server is already running and is blocking, waiting for
// data.
// Results: A UPD socket is created.
// Returns: The socket descriptor of the remote socket.
// ----------------------------------------------------------------------------
//
int CreateUDPSocket(struct sockaddr_in &remote, char *szHostName, char *szPort)
{
int nSocket;
hostent *hp; // Address of remote host (server).
//
// Create the socket.
//
if ((nSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("Error creating socket in CreateUDPSocket");
exit(1);
}
//
// Get the address of the remote host.
//
hp = gethostbyname(szHostName);
//
// Set up the remote address.
//
remote.sin_family = AF_INET;
memcpy(&remote.sin_addr, hp->h_addr_list[0], hp->h_length);
remote.sin_port = atoi(szPort);
return nSocket;
}
Jon Sagara
"After all is said and done, usually more is said than done." -- <i>Unknown</i>
|
|
|
|
|
// ============================================================================
// server.cc
//
// This program is responsible for accepting requests for files from the rcopy
// program and transmitting the file back. This program should never terminate
// (unless you kill it with a ctrl-c). It should continually process requests
// from the rcopy program.
//
// The server needs to handle error conditions such as non-existing files by
// passing back a flag to the rcopy program and then waiting for a new file
// request.
//
// The server should output its port number to be used by the rcopy program.
// The server program will be run as:
//
// server error-percent
//
// where
//
// error-percent is the percent of packets that are in error (floating
// point number)
//
// Author: Jon Sagara
//
// This file represents the SERVER.
// ============================================================================
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <stream.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include "networks.h"
#include "sendtoErrB.h"
int CreateServerSocket();
int main(int argc, char *argv[])
{
int nSocket;
int nBytesRead = 0; // Number of bytes read from rcopy.
int nBytesSent = 0; // Number of bytes send to rcopy.
int nRemoteLen = 0; // Length of remote
sockaddr_in remote; // Socket address of rcopy.
unsigned char recv_buf[BUFF_SIZE];
unsigned char send_buf[BUFF_SIZE];
//
// Check command line arguments.
//
if (2 != argc)
{
cout << "usage: " << argv[0] << " error-percent" << endl;
exit(-1);
}
nSocket = CreateServerSocket();
nBytesRead = recvfrom(nSocket, recv_buf, BUFF_SIZE, 0, (struct sockaddr *) &remote, &nRemoteLen);
cout << recv_buf << " len: " << nBytesRead << endl;
memcpy(send_buf, "Jon Sagara", 11);
nBytesSent = sendto(nSocket, send_buf, 11, 0, (struct sockaddr *) &remote, sizeof(remote));
if (nBytesSent < 0)
{
perror("Error sending data in server. Sending back to rcopy");
close(nSocket);
exit(1);
}
close(nSocket);
return 0;
}
// ----------------------------------------------------------------------------
// CreateServerSocket()
//
// Creates a server socket and binds it to a particular address and port. This
// allows any process to (theoretically) connect to our server.
//
// Assumes: We will bind to the local address.
// Results: A socket is created, bound to the local machine's address.
// Returns: The socket descriptor.
// ----------------------------------------------------------------------------
//
int CreateServerSocket()
{
int nSocket = 0; // Socket descriptor.
int nLocalLen = 0; // sizeof(local).
sockaddr_in local; // Local socket address.
//
// Create the socket.
//
if ((nSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("Error creating socket in CreateServerSocket()");
exit(1);
}
//
// Set up the socket ("name it").
//
local.sin_family = AF_INET; // Internet family.
local.sin_addr.s_addr = INADDR_ANY; // Wild card machine address (local address).
local.sin_port = 0; // Let the system choose the port.
//
// Bind the name (address) to a port.
//
nLocalLen = sizeof(local);
if ((bind(nSocket, (struct sockaddr *) &local, nLocalLen)) < 0)
{
perror("Error calling bind() in CreateServerSocket()");
exit(1);
}
//
// Get the port name and print it out.
//
getsockname(nSocket, (struct sockaddr *) &local, &nLocalLen);
cout << "Socket has port " << local.sin_port << "\n";
return nSocket;
}
Jon Sagara
"After all is said and done, usually more is said than done." -- <i>Unknown</i>
|
|
|
|
|
|
Jon
you've got some nerve posting Unix code on codeproject
Nish
Sonork ID 100.9786 voidmain
www.busterboy.org
Nish is a BIG fan of Goran Ivanisevic
|
|
|
|
|
Like you said in the next thread up, the concepts are largely the same. I was stuck on the same lamo bug for a few hours and I was getting desperate, so I thought I'd give it a go here in the CP forums. Seems like every time I post a question here, I end up answering it myself 20 minutes later. Maybe I should post first and think later.
Jon Sagara
"After all is said and done, usually more is said than done." -- Unknown
|
|
|
|
|
Jon Sagara wrote:
I end up answering it myself 20 minutes later.
Who am I kidding? It took almost 3 hours. Time stamps are the devil.
Jon Sagara
"After all is said and done, usually more is said than done." -- Unknown
|
|
|
|
|
Jon Sagara wrote:
Time stamps are the devil.
got you that time, didnt we !
Nish
Sonork ID 100.9786 voidmain
www.busterboy.org
Nish is a BIG fan of Goran Ivanisevic
|
|
|
|
|
Hi, I have a question that may be very very easy for most of you. Please don't laugh, I'm embarassed not able to find it after spending quite a bit of time on it.
How can I call up a dialog box that browses for a directory? I know CFileDialog can browse for a file, but I do not need the file. I just want the directory.
Thank you very much for your help.
|
|
|
|
|
Use SHFileOperation(). There are wrapper classes here on CP to help you get started if you need it.
--Mike--
http://home.inreach.com/mdunn/
#include "witty_sig.h"
your with and
|
|
|
|
|
SHBrowseForFolder() works better IMHO
---
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
|
I'll ride on this topic with mine...on the off chance the original poster is using WinXP
http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=80917&fr=176&df=2&app=50#xx79842xx
--Mark Terrano
|
|
|
|
|
How to refresh CommandUI of menu and toolsbar at once?
|
|
|
|
|
Problem:
Code working for a long time under WinME/2000/98 does not work under WinXP. Little function to pop up a dialog box and allow the user to pick a folder (or file) which is returned as a CString. I get back the wrong value if user creates new folder or types non-existing folder into box.
What I'm looking for:
Function to pop up dialog and get back existing or new folder as specified by user.
Symptoms:
Works just as I would expect it to under Win98/ME/2000.
Under WinXP if the user creates a folder, or types a new folder into the edit box I get back a reference to their user directory. (Please excuse a bit of 'debugging mess' I'm still trying to figure out what is going on).
Also, there are some header files that I don't have the latest versions of, being limited to VC6 with all patches (so you can ignore my comments about BIF_USENEWUI - which is documented but appears at some later point I think).
I didn't see any KB articles related to WinXP for either of my two functions-in-question.
Any help would be appreciated!
--Mark
-----My Test Code------
CString CXpgetfoldercheckDlg::getFolderName(CString title, bool requireTrailingBackslash, bool allowNewFolders)
{
// PURPOSE:
// Uses latest UI to get a folder name.
// PARAMS:
// title = the title of the browse dialog.
// rquireTrailingBackslash = true if you want the last character of the folder to be '\' (default t)
// allowNewFolders = true if you want a 'new' button to appear on the dialog (default t)
// RETURNS:
// valid folder name if successful or NULL if not
// REQUIRED:
// IE 5.0 on up, or Win2000 on up
// STEPS:
// Bring up a common file dialog,
// Allow the user's to select a folder name
// Close the dialog
//
// KNOWN BUG: Problem with WindowXP (11-2-2001) - typed-in filename (i.e. new file name) returns the
// user's mydocuments folder.
// Results with the following settings
// info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_EDITBOX;
BROWSEINFO info;
ITEMIDLIST *list;
HRESULT ret;
DWORD dwCoInit=COINIT_APARTMENTTHREADED;
ret = CoInitializeEx(NULL,dwCoInit);
if (ret == S_OK || ret == S_FALSE )
TRACE("CoInitted\n");
else
{
return "ERR: Failed CoInit";
}
//info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI ; //new
// Where are these?
// BIF_USENEWUI
// BIF_NEWDIALOGSTYLE
//FIXME: Could not get the various header issues to work, so I just added the param values for the
//reqd. params above. Docs are correct, could not get them to appear. Will work with IE5.0+ versions.
info.hwndOwner = NULL;
info.lpszTitle = title;
info.lpfn = NULL;
info.pidlRoot = NULL;
info.lParam = 0L;
info.iImage= NULL;
info.pszDisplayName = NULL;
if (allowNewFolders)
{
info.ulFlags = 0x0040 | BIF_EDITBOX;
//info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_EDITBOX;
//info.ulFlags = BIF_RETURNONLYFSDIRS | 0x0040 | BIF_EDITBOX;
//info.ulFlags = BIF_RETURNONLYFSDIRS | BIF_EDITBOX | BIF_STATUSTEXT ;
//info.ulFlags = BIF_RETURNONLYFSDIRS | 0x0040 | BIF_EDITBOX;
}
else
{
info.ulFlags = BIF_RETURNONLYFSDIRS;
}
list = SHBrowseForFolder( &info );
if (list == NULL)
{
//User hit cancel
AfxMessageBox("User cancelled");
return "ERR: User Cancelled";
}
CString fileName;
char buf[MAX_PATH+1];
buf[0]='\0';
ret = SHGetPathFromIDList( list, buf );
if (!ret)
{
return("ERR:Failed to get path from IDList");
}
else
{
fileName = buf;
}
#ifdef _DEBUG
TRACE1("getFolderName] User folder: %s\n", buf);
#endif
//Could still be invalid return - i.e. spaces.
if (fileName.IsEmpty())
return fileName;
// For most uses, I require a trailing \ at the end of the filename. If it is not
// there, then it is appended.
if (requireTrailingBackslash)
if (fileName.Right(1) != "\\")
fileName += "\\";
return fileName;
}
--Mark Terrano
|
|
|
|
|
hi :
does any body knows about how to access external hardware through vc++ like what type of programmig should i do menas how to access the external hardware . like if i want to handle external sensors then wht should i do if any one wanna more detail on it then he can ask me at email
bilal_bilu82@hotmail.com
|
|
|
|
|
Maybe you can just post some more details about the kind of hardware you are going to interface to and the communication to it. Different ways to talk over the Com port, USB, Keyboard, etc. If your question is a general one about how to interface with some sensors - look at companies like Home Automation Labs and X-10 - who have some inexpensive up to professional sensors that are designed to be connected to PCs.
There are lab automation application applications of all kinds that are designed to do real-time processing of sensor data and feed other applications clean data, do logs, etc - and you can pick one to interface to an API with C++, VB or your langage of choice.
Good luck - post details if you need additional help.
--Mark Terrano
www.ensemblestudios.com
(Creators of the Age of Empires series)
|
|
|
|
|
I'm hoping to use the registry as a sort of associative container, where I can set a key, then iterate through the items stored there AND their values. Is there a way to discover the names of items stored in the registry under a specific subkey ? For example, I have HKEY_CURRENT_USER/Software/Graus/MovieDatabase/Files, and I want to know the name as well as the value of every item stored there.
Thanks.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
Is it still unanswered? CP is very slow today, at least for me
RegEnumKeyEx and RegEnumValue will do the job.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|