|
Hi all,
I have made a application in vc2008, I have made a TCHAR variable...
TCHAR prdcode[10];
My problem is i want to assign this value to CString variable...
CString test = prdcode;
but while assigning value it takes some garbage value....
how can i do it...
thanks in advance
|
|
|
|
|
VCProgrammer wrote: but while assigning value it takes some garbage value....
How do you check that ?
In oyur code snippet, you didn't assign anything to prdcode, so it will contain garbage at that time. Show use the code where you write something in it.
|
|
|
|
|
my prcode is getting a value....
and i am assigning that value to my CString variable....
suppose it has 10 characters
starting will be ok but at end it'll show garbage value....
|
|
|
|
|
VCProgrammer wrote: my prcode is getting a value....
Well, where is your code ? Without code, it's difficult to help...
VCProgrammer wrote: starting will be ok but at end it'll show garbage value....
Did you forget the zero termination of the string ?
|
|
|
|
|
TCHAR prdcode[10] = _T("thisvalue");
CString test = prdcode;
«_Superman_»
|
|
|
|
|
Either use _tcscpy() for initializing prdcode or put the string directly to the CString object.
|
|
|
|
|
Well, possibly prdcode contains garbage, you didn't show us how did you fill the array content...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
VCProgrammer wrote: ...it takes some garbage value....
How are you verifying this?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I've been following Beej's Guide to network programming, (which is really great) and when implementing
his streaming socket server ( <a href="http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver">http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver</a>[<a href="http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver" target="_blank" title="New Window">^</a>] )
it works just fine. To test it I opened a Konsole (using Kubuntu) and ran:
<code>
pacific@mainbox $ telnet localhost 3490
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello, world!Connection closed by foreign host.
</code>
However, when trying to make a class out of the above code, as shown below, I get a "accept: Bad file descriptor" error, repeatedly.
Not sure if these two functions (which are not part of the class) have anything to do with it:
void sigchld_handler(int s);
void *get_in_addr(struct sockaddr *sa);
Help on this would be much appreciated !!
Gvanto
Sources also here: <a href="http://www.sharedigest.com/socketserver.tar.gz">http://www.sharedigest.com/socketserver.tar.gz</a>[<a href="http://www.sharedigest.com/socketserver.tar.gz" target="_blank" title="New Window">^</a>]
main (WLFeeder.cpp):
<code>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "SocketServer.h"
#define DO_MAIN 1
using namespace std;
using namespace wlns;
#ifdef DO_MAIN
int main(int argc, char *argv[]) {
SocketServer *s = new SocketServer();
if( s->initAddrInfo() != SUCCESS ) {
cout << "Error initializing address " << endl;
exit(1);
}
if( s->initBind() != SUCCESS ) {
cout << "Error initializing bind " << endl;
exit(1);
}
if( s->initListen() != SUCCESS ) {
cout << "Error initializing listen " << endl;
exit(1);
}
if( s->clearDeadProcesses() != SUCCESS ) {
cout << "Error clearing dead processes " << endl;
exit(1);
}
if( s->listenForConnections() != SUCCESS ) {
cout << "Error in listening for connections " << endl;
exit(1);
}
return 0;
}
#endif
</code>
SocketServer.h:
<code>
/*
* SocketServer.h
*
* Created on: 11/02/2009
* Author: pacific
*
* Generic, extendable socket-managing class handling
* network communications via sockets
*/
#ifndef SOCKETSERVER_H_
#define SOCKETSERVER_H_
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define BACKLOG 10 //no. of pending connections the queue will hold
#define PORT "3490"
using namespace std;
namespace wlns {
static int SUCCESS = 1;
static int FAILURE = 0;
class SocketServer {
private:
int sockfd_, new_fd_; //listen on sock_fd_, new connection on new_fd_
struct addrinfo hints_, *servinfo_, *p_; //hints and actual address
struct sockaddr_storage their_addr_; //connector's address information
socklen_t sin_size_;
struct sigaction sa_;
int yes_;
char s_[INET6_ADDRSTRLEN];
int rv_;
//char *remote_host_, *remote_port_;
public:
SocketServer();
virtual ~SocketServer();
int setLocalPort(char *lp);
char *getLocalPort();
int initAddrInfo();
int initBind();
int initListen();
int clearDeadProcesses();
int listenForConnections();
// int getSocketFileDescriptor();
// int setRemoteHostAndPort(char *host, char *port);
// int connectToRemoteHost();
};
}
#endif /* SOCKETSERVER_H_ */
</code>
SocketServer.cpp:
<code>
/*
* SocketServer.cpp
*
* Created on: 11/02/2009
* Author: pacific
*/
#include "SocketServer.h"
using namespace std;
namespace wlns {
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
SocketServer::SocketServer() {
yes_ = 1;
memset(&hints_, 0, sizeof hints_);
hints_.ai_family = AF_UNSPEC;
hints_.ai_socktype = SOCK_STREAM;
hints_.ai_flags = AI_PASSIVE; //bind to IP of the host we're running on, p148 Beej's
}
SocketServer::~SocketServer() {
// TODO Auto-generated destructor stub
}
/**
* Populates servinfo with the appropriate
*/
int SocketServer::initAddrInfo() {
cout << "initAddrInfo() :: Initializing address ..." << endl;
if( (rv_ = getaddrinfo(NULL, PORT, &hints_, &servinfo_)) == -1 ) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv_));
return FAILURE;
}
return SUCCESS;
}
int SocketServer::initBind() {
cout << "bindFirstPossible() :: ..." << endl;
for(p_ = servinfo_; p_ != NULL; p_ = p_->ai_next) {
if( (sockfd_ = socket(p_->ai_family, p_->ai_socktype, p_->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if( setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, &yes_, sizeof(int)) == -1 ) {
perror("setsockopt");
return FAILURE;
}
if (bind(sockfd_, p_->ai_addr, p_->ai_addrlen) == -1) {
close(sockfd_);
perror("server: bind");
continue;
}
break;
}
freeaddrinfo(servinfo_); //done with this struct
if(p_ == NULL) {
fprintf(stderr, "server failed to bind\n");
return FAILURE;
}
return SUCCESS;
}
int SocketServer::initListen() {
cout << "initListen() :: ..." << endl;
if( listen(sockfd_, BACKLOG) == -1 ) {
perror("listen");
return FAILURE;
}
return SUCCESS;
}
int SocketServer::clearDeadProcesses() {
cout << "clearDeadProcesses() :: ..." << endl;
sa_.sa_handler = sigchld_handler; //assign function pointer - not sure if it matters if its not a class method ?
sigemptyset(&sa_.sa_mask);
sa_.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa_, NULL) == -1) {
perror("sigaction");
return FAILURE;
}
return SUCCESS;
}
int SocketServer::listenForConnections() {
cout << "listenForConnections() :: Listening..." << endl;
int rc = SUCCESS;
while(1) {
sin_size_ = sizeof their_addr_;
cout << "waiting to accept " << endl;
new_fd_ = accept(sockfd_, (struct sockaddr *)&their_addr_, &sin_size_);
if(new_fd_ == -1) {
perror("accept");
rc = FAILURE;
continue;
}
inet_ntop(their_addr_.ss_family, get_in_addr((struct sockaddr *)&their_addr_), s_, sizeof s_);
printf("SocketServer: got connection from %s\n", s_);
if(!fork()) { //this is child process
close(sockfd_);
if (send(new_fd_, "Hello, world!", 13, 0) == -1) {
perror("send");
rc = FAILURE;
}
close(new_fd_);
}
close(new_fd_); //parent doesnt need this
}
return rc;
}
} //end ns
</code>
OUTPUT:
<code>
pacific@mainbox /workspace/WLFeeder/Debug$ ./WLFeeder
initAddrInfo() :: Initializing address ...
bindFirstPossible() :: ...
initListen() :: ...
clearDeadProcesses() :: ...
listenForConnections() :: Listening...
waiting to accept //Here I entered the telnet command again, in a seperate Konsole terminal ...
SocketServer: got connection from ::ffff:127.0.0.1 //works once
waiting to accept
accept: Bad file descriptor // then keeps giving errors ...
waiting to accept
accept: Bad file descriptor
waiting to accept
accept: Bad file descriptor
waiting to accept
accept: Bad file descriptor
waiting to accept
</code>
Find Your Dream Job in Australia, Free!
http://www.WebCV.com.au
|
|
|
|
|
The one thing I notice is that your fork ed process doesn't exit (unlike the equivalent code in Beej's guide)?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks a million Stuart, that did it !
I thought exit(0) was only to exit from a main routine or something,
did not realize it would exit from the if( ) { } routine ...
Gvanto
|
|
|
|
|
gvanto wrote: exit(0) was only to exit from a main routine or something
No, it exits the current process - of course, which process is 'current' can get confusing with *nix, as the fork function creates a new process by duplicating the caller, so you have to track the two code paths, the parent process (where fork returns the child PID) and the child process (wehere fork returns zero).
In some ways it's easier to work with than Win32's CreateProcess , in other ways not.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Ah ic, yeah that's what kind of got me I think. Not quite got the hang of these socket processes (and I'm pretty new to C++)
so still got a way to go. and a loooooooooong way before I start coding c++ in windows !
Thanks again for the help Stuart, much appreciated !
Best,
gvanto
|
|
|
|
|
hi all,
can anybody help me build an application having multilingual support using VC++.
thanks!!
Varun Bhatt
|
|
|
|
|
Check this[^]
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
Thanks a lot Sarath,
but i had already tried this and lots of other examples on code project and codeguru.com and on various other sites but not really useful to get an clear idea about how to do this.
THough, now i have an obscure idea about the steps but can you tell me the stepwise procedure to proceed. i was trying the following example
ddj.com/184404314?pgno=1
but atlast it resulted the following error
d:\varun\mltest\mltest\mltest.h(10) : fatal error C1083: Cannot open include file: 'MLDLL\resource.h': No such file or directory
MLTestDlg.cpp
d:\varun\mltest\mltest\mltest.h(10) : fatal error C1083: Cannot open include file: 'MLDLL\resource.h': No such file or directory
i have tried a lot to solve this but failed, can you suggest any solution?
Varun Bhatt
|
|
|
|
|
Get the ML directory and files for your project and put it under current path of the workspace or in any include paths and compile.
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
|
Hi All,
---------------------
| | |
| | |
---------------------
| | |
| | |
---------------------
I need a splitter window (4 way) as shown above. I use the code
m_wndSplitter.CreateStatic(this, 1, 2));
m_wndSplitter1.CreateStatic(&m_wndSplitter,2,1,WS_CHILD |WS_VISIBLE, m_wndSplitter.IdFromRowCol (0, 0));
m_wndSplitter1.CreateView(0, 0, RUNTIME_CLASS(CLabel), CSize(250, 15), pContext) ;
m_wndSplitter1.CreateView(1, 0, RUNTIME_CLASS(CLView), CSize(250, 500), pContext) ;
m_wndSplitter2.CreateStatic(&m_wndSplitter,2,1,WS_CHILD |WS_VISIBLE, m_wndSplitter.IdFromRowCol (0, 1));
m_wndSplitter2.CreateView(0, 0, RUNTIME_CLASS(CLabel), CSize(250, 15), pContext) ;
m_wndSplitter2.CreateView(1, 0, RUNTIME_CLASS(CLView), CSize(250, 500), pContext) ;
But i got this
---------------------
|| |
|| |
---------------------
|| |
|| |
---------------------
I know that m_wndSplitter.CreateStatic(this, 2, 2));
is possible for this 4 way splittering.
But here wndSplitter1 & wndSplitter2 are the object of different splitter classes
Help me..
|
|
|
|
|
You have to call m_wndSplitter.CreateView before calling .CreateStatic on the other splitters even though you will never use those views.
«_Superman_»
|
|
|
|
|
Wow!! it works well.Thank u very much
|
|
|
|
|
Dears,
I have a doubt about the type casting in CString and LPCTSTR.
Please see the below code.
void FnWithArgs( LPCTSTR lpctstrBuffer )
{
// Do nothing;
}
void FnTest()
{
CString csBuffer;
FnWithArgs( csBuffer.operator LPCTSTR()); // Casting-1
FnWithArgs( static_cast<<LPCTSTR>>( csBuffer )); // Casting-2
}
In the code we can see that two casting methods are done.
My doubt is that which of the above ( Casting-1 or Casting-2 ) is better?
Actually I need the reason also.
Regards,
Krishnakumar
|
|
|
|
|
They are the same. Casting-2 will internally be converted to Casting-1 before execution.
«_Superman_»
|
|
|
|
|
«_Superman_» wrote: They are the same. Casting-2 will internally be converted to Casting-1 before execution.
Which one I can prefer?
Casting-1?
|
|
|
|
|
Casting-2 is the standard.
So go with that.
«_Superman_»
|
|
|
|
|