WinSock2.h

1
2
3
4
 while trying to match the argument list '(std::istream, const char)'
2>c:\users\fuzzy\documents\visual studio 2010\projects\test\client\client.cpp(37): error C2664: 'inet_addr' : cannot convert parameter 1 from 'const char' to 'const char *'
2>          Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I am working with sockets. I am trying put in some small code to enter your own server ip address. So above is my problem. I'll copy the source code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma comment(lib, "ws2_32.lib")

#include <iostream>
#include <winsock2.h>
using namespace std;

int main(int argc, char *argv[])
{
    WSADATA wsaData;
    int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (starterr != 0) {
        cout << "WSADATA startup has failed!" << endl;
        cout << "Error Code: " << WSAGetLastError() << endl;
        system("pause >nul");
        WSACleanup();
        return 0;
    }
    cout << "WSADATA Startup Successful!" << endl;
    
    SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
    if (mysock == INVALID_SOCKET) {
        cout << "Socket Creation Failed!" << endl;
        cout << "Error Code:  " << WSAGetLastError() << endl;
        system("pause >nul");
        WSACleanup();
        return 0;
    }
    cout << "Socket Creation Successful!" << endl;
    
    // Code mod.  allowing user to imput address
    const char ans;
    cout<<"Please input server address: "<<endl;
    cin>>ans;

    sockaddr_in sin;
    sin.sin_port = htons(4080);
    sin.sin_addr.s_addr = inet_addr(ans);
    sin.sin_family = AF_INET;

    if (connect(mysock,(sockaddr*)&sin, sizeof(sin)) == INVALID_SOCKET) {
        cout << "Socket Connection Failed" << endl;
        cout << "Error Code:  " << WSAGetLastError() << endl;
        system("pause >nul");
        closesocket(mysock);
        WSACleanup();
        return 0;
    }
    cout << "Socket successfully connected to "<<ans<<"!" << endl;

    char buf[200];
    recv(mysock, buf, sizeof(buf), 0);
    cout << buf;
    cout<<"\nClosing connection to server!"<<endl;
    WSACleanup();
    closesocket(mysock);
    return 0;
}
const char ans;

You can't have it const if you want the user to be able to input stuff.
Also, you need it to be a pointer if you want to store a C-string.
Finally, you'll need to allocate memory for the string.

I'd suggest just using an std::string and .c_str() instead though.
// Code mod. allowing user to imput address
const char ans;
cout<<"Please input server address: "<<endl;
cin>>ans;
//here error

may be this;
char ans[20] = {0};
cout<<"Please input server address: "<<endl;
cin>>ans;
const char ans;
cout<<"Please input server address: "<<endl;
cin>>ans;
//here error


in your code
ans is a character,not a string
you must use string
so you can modify it like the following

don,t use const

char ans[10];
What if I wanted to read in from a .ini file? Or could you give me some good google key words to search for? That's what I really want to do is when I complie have a text file made that has a default config.ini file.
You can access an inifile using GetPrivateProfileString.
http://msdn.microsoft.com/en-us/library/ms724353%28VS.85%29.aspx
Topic archived. No new replies allowed.