amer83

Hello :

i'am working on VC++ 6 and when i do this it doesn't work :

#include <string.h>

#include <iostream.h>

void main()

{

string s;

s = "Hello, world";

cout << s << endl;

}

error is that string : undeclared identifier.

i want to work with C-style string because i'am working on a proxy project

and i can't solve my problem if the solution will depend on something like :

#include <string>

#include <iostream>

//without .h

using namespace std;

Best regards



Re: Visual C++ Language VC6 problem in string.h

pagen_hd

/* C-style string is simply an array of char.

"string" really is an unknown identifier.

*/

#include <string.h>

#include <iostream.h>

void main()

{

char* s;

s = "Hello, world";

cout << s << endl;

}






Re: Visual C++ Language VC6 problem in string.h

amer83

thnx for your reply.

i need string variable to capture data from socket.

like this sample :

int main(){
//declarions
int new_fd;
string s;
.................

new_fd = accept(sockfd, (struct sockaddr 8)&thier_addr, &addr_length);
s = *new_fd

// this assigenment what i need to do

// another style of this can be done like this s << *new_fd

//both tow kinds of assigenment doesn't work with me cuz string is undecalred.
cout << s << endl;





Re: Visual C++ Language VC6 problem in string.h

Ðãv? S. Â???????

You can do:

char *str = "hello world!";
cout << ctr;





Re: Visual C++ Language VC6 problem in string.h

amer83

Here is my proxy code and i attached errors with it :

#include <iostream.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MYPORT 8080 // the port users will be connecting to

#define BACKLOG 10

int main(void)
{
WSADATA wsaData; // if this doesn't work
//WSAData wsaData; // then try this instead

if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
exit(1);}


int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int addr_length;
string s;

if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}

while(true){

addr_length = sizeof ( my_addr );
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_length)) == -1) {
perror("accept");
}
else {

s = *new_fd;
cout << "Browser says : " << s << endl;
}
}

return 0;
}

When I compile my file I got this :

Compiling...
test.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\httpOroxy\test.cpp(26) : error C2065: 'string' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\httpOroxy\test.cpp(26) : error C2146: syntax error : missing ';' before identifier 's'
C:\Program Files\Microsoft Visual Studio\MyProjects\httpOroxy\test.cpp(26) : error C2065: 's' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\httpOroxy\test.cpp(56) : error C2100: illegal indirection
Error executing cl.exe.

test.obj - 4 error(s), 0 warning(s)





Re: Visual C++ Language VC6 problem in string.h

einaros

Replace iostream.h with iostream and string.h with string. The STL headers don't use the .h extension. In addition to this, you should add a "using namespace std;" prior to the int main() function. The STL classes and functions are in the std namespace.

Reading your original post it appears that you don't want to include the right headers. Could you elaborate on what exactly your problem is To use the string class, you need to include the string header.






Re: Visual C++ Language VC6 problem in string.h

amer83

thnx for your reply.

about including string, stream (both without .h) and using namespace std

i try that when i begin my work, but there was this problem :

s = *new_fd;

illegal inderiction

what i need is printing the content of the socket.

or simple doing this(or any another working method) :

s = *new_fd;

cout << s << endl;

Best regards





Re: Visual C++ Language VC6 problem in string.h

Bite Qiu - MSFT

amer83 wrote:

thnx for your reply.

about including string, stream (both without .h) and using namespace std

i try that when i begin my work, but there was this problem :

s = *new_fd;

illegal inderiction

what i need is printing the content of the socket.

or simple doing this(or any another working method) :

s = *new_fd;

cout << s << endl;

Well, the reason that you get highlighted "illegal inderiction" is obvious: http://msdn2.microsoft.com/en-us/library/bzf3eha6(vs.71).aspx

And if you read MSDN about the accept function especially its return value, you will get the idea that dereference the return value and assign it to a string makes NO sense at all: http://msdn2.microsoft.com/en-us/library/ms737526.aspx






Re: Visual C++ Language VC6 problem in string.h

amer83

Hello :

thnx a lot, i'll try it and post what i'll find.

regards





Re: Visual C++ Language VC6 problem in string.h

Shakje

What will you try The point made is that you can't just try and assign a handle to a string, it doesn't make sense. Using <string> means you are using STL strings which are purely C++, using iostreams (ie cout) is also a C++ feature.

To create a C-style string you need to create an array of chars, but even then you can't just throw a handle at it and expect it to understand what to do. The best that can be offered is something along the lines of sprintf(s, "0x%08X", somehandle) where s is a pointer to an array of chars and somehandle is the return of accept. Even then all you're doing is printing out the handle in hex, which is completely useless information except as a unique identifier.

What information do you need from the socket, and do you have a background in Java There is no standard ToString function in C or native C++ so you can't just throw something at a string and expect to get back anything in plain English.






Re: Visual C++ Language VC6 problem in string.h

amer83

Hello ;

thnx for your reply.

what i need is HTTP data(Header)

so my socket is listening on my browser and add the that my browser send as a requist to a string variable s.

why that

i need string s (which contain HTTP Header data) to see what URL my browser requist so i'll choose to forward the requist or deney it it the URL is in a black list

regards





Re: Visual C++ Language VC6 problem in string.h

Shakje

I'm not sure why you're passing and casting a sockaddr_in for the accept function, but the point of passing in a sockaddr there is to get the information you are looking for. It contains a C-style string (called sa_data) which contains the ip of the connection. I can't remember the function but you should be able to get the URL from the ip. The handle is used internally to identify the connection you are talking about, it does not contain any data itself and so cannot be used as you are suggesting. As mentioned before a C-string is just an array of chars, but you can use a constructor to create an STL string from a C-string.