Thread: [Dev-C++] Internet programming again
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ben M. <ili...@at...> - 2007-12-07 03:05:28
|
hello. I am trying to write that winsock app that i was working on the
other day. I want to find the IP address of the computer that is
connecting to the server. Here is the code for the server application:
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
WSADATA wsadat;
SOCKET Socket, TempSocket = SOCKET_ERROR;
SOCKADDR_IN SockAddr;
SOCKADDR ConnAddr;
ifstream infile;
string filename = "C:\\counter.java";
char send_str[1], recv_str[6];
int a = 0, in_char, RetVal;
if (WSAStartup (MAKEWORD(1,1), &wsadat) != 0)
{
cout << "WSAStartup failed!!!";
}
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
cout << "Socket creation failed!!!";
}
SockAddr.sin_port = 50;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 192;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 168;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 1;
SockAddr.sin_addr.S_un.S_un_b.s_b4 = 102;
if (bind (Socket, (SOCKADDR *)(&SockAddr), sizeof (SockAddr)) ==
SOCKET_ERROR)
{
cout << "Attempt to bind failed!!!";
}
listen (Socket, 1);
while (TempSocket == SOCKET_ERROR)
{
TempSocket = accept (Socket, &ConnAddr, sizeof (ConnAddr));
}
Socket = TempSocket;
printf ("%s", ConnAddr.sa_data);
while (a <= filename.length())
{
send_str[0] = filename[a];
cout << send_str;
send (Socket, send_str, 1, 0);
++a;
}
infile.open (filename.c_str());
while (!infile.eof())
{
in_char = infile.get();
send_str[0] = in_char;
cout << send_str;
send (Socket, send_str, 1, 0);
}
infile.close();
while (RetVal == SOCKET_ERROR)
{
RetVal = recv (Socket, recv_str, 6, 0);
}
cout << recv_str;
system ("pause");
return 0;
}
The problem text is highlighted in green. I get the error message
"invalid conversion from 'unsigned int' to 'int *' initializing argument
3 of 'SOCKET accept (SOCKET, sockaddr*, int*)'. But how else am i
supposed to pass the size of the structure to the method?
|
|
From: Ben M. <ili...@at...> - 2007-12-07 03:51:16
|
adrian wrote:
> Доброго времени суток, ili...@at...!
> Вами, Ben Melikant <mailto:ili...@at...> было написано:
>
>
>> hello. I am trying to write that winsock app that i was working on
>> the other day. I want to find the IP address of the computer that is
>> connecting to the server. Here is the code for the server application:
>>
>> #include <winsock2.h>
>> #include <windows.h>
>> #include <iostream>
>> #include <fstream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main ()
>> {
>> WSADATA wsadat;
>> SOCKET Socket, TempSocket = SOCKET_ERROR;
>> SOCKADDR_IN SockAddr;
>> SOCKADDR ConnAddr;
>> ifstream infile;
>> string filename = "C:\\counter.java";
>> char send_str[1], recv_str[6];
>> int a = 0, in_char, RetVal;
>>
>> if (WSAStartup (MAKEWORD(1,1), &wsadat) != 0)
>> {
>> cout << "WSAStartup failed!!!";
>> }
>>
>> Socket = socket(AF_INET, SOCK_STREAM, 0);
>> if (Socket == INVALID_SOCKET)
>> {
>> cout << "Socket creation failed!!!";
>> }
>>
>
>> SockAddr.sin_port = 50;
> Do U believe what 50 port is always free? Ports less than 1024 is
> standard ports and may be allocated with another program. In any ways
> you need another port when your port don't acceptabling. Also U need
> write:
> SockAddr.sin_port=htons(50);//or another port number
>> SockAddr.sin_family = AF_INET;
> //-------------------------------------------------------
>> SockAddr.sin_addr.S_un.S_un_b.s_b1 = 192;
>> SockAddr.sin_addr.S_un.S_un_b.s_b2 = 168;
>> SockAddr.sin_addr.S_un.S_un_b.s_b3 = 1;
>> SockAddr.sin_addr.S_un.S_un_b.s_b4 = 102;
> this paragraph can be replaced with:
> SockAddr.sin_addr.s_addr=inet_addr("192.168.1.102");
> you can replace text in "" with any address U need.
>
>> if (bind (Socket, (SOCKADDR *)(&SockAddr), sizeof (SockAddr)) ==
>> SOCKET_ERROR)
>> {
>> cout << "Attempt to bind failed!!!";
>> }
>>
>> listen (Socket, 1);
>>
>> while (TempSocket == SOCKET_ERROR)
>> {
>> TempSocket = accept (Socket, &ConnAddr, sizeof (ConnAddr));
>> }
>> Socket = TempSocket;
>>
>> printf ("%s", ConnAddr.sa_data);
>> while (a <= filename.length())
>> {
>> send_str[0] = filename[a];
>> cout << send_str;
>> send (Socket, send_str, 1, 0);
>> ++a;
>> }
>>
>> infile.open (filename.c_str());
>> while (!infile.eof())
>> {
>> in_char = infile.get();
>> send_str[0] = in_char;
>> cout << send_str;
>> send (Socket, send_str, 1, 0);
>> }
>> infile.close();
>>
>>
>> while (RetVal == SOCKET_ERROR)
>> {
>> RetVal = recv (Socket, recv_str, 6, 0);
>> }
>> cout << recv_str;
>>
>> system ("pause");
>> return 0;
>> }
>> The problem text is highlighted in green. I get the error message
>> "invalid conversion from 'unsigned int' to 'int *' initializing
>> argument 3 of 'SOCKET accept (SOCKET, sockaddr*, int*)'. But how else
>> am i supposed to pass the size of the structure to the method?
>>
> This error mean what U sent constant int in function what demand
> pointer on int.
> IMHO U need write:
>
> ...
> int iSize=sizeof(SOCKADDR);
> ...=accept(Socket, (sockaddr_in*)&ConnAddr, &iSize);
>
> I don't saw green text 'cos I recieve mail it plain text format. IMHO
> many another people also receiving messages in plain text format and
> can't see color mark.
>
> С уважением, Чурин.
> P.S. Excuse me for my bad english - it's not my native language.
>
>
> Well, now i have another problem. How can I get the value of
> ConnAddr.sa_data to print to the screen? When i try to print it using
> printf("%s", ConnAddr.sa_data) it prints only a 2.
|
|
From: Per W. <pw...@ia...> - 2007-12-07 12:24:53
|
Green text? In a text mail? Don't think so :)
int size = 6;
accept(xx,yy,&size);
This sends the value 6 to accept, and the reason why it is sent as a
pointer is that on call, it reports the size of your buffer, and on return
it will inform how large the returned address actually was.
But why the magic value 6? Never use numerical values randomly in the
code. Why didn't you use sizeof() to make sure that the value 6 really
matches the size of your buffer?
/pwm
On Thu, 6 Dec 2007, Ben Melikant wrote:
> hello. I am trying to write that winsock app that i was working on the
> other day. I want to find the IP address of the computer that is
> connecting to the server. Here is the code for the server application:
>
> #include <winsock2.h>
> #include <windows.h>
> #include <iostream>
> #include <fstream>
> #include <string>
>
> using namespace std;
>
> int main ()
> {
> WSADATA wsadat;
> SOCKET Socket, TempSocket = SOCKET_ERROR;
> SOCKADDR_IN SockAddr;
> SOCKADDR ConnAddr;
> ifstream infile;
> string filename = "C:\\counter.java";
> char send_str[1], recv_str[6];
> int a = 0, in_char, RetVal;
>
> if (WSAStartup (MAKEWORD(1,1), &wsadat) != 0)
> {
> cout << "WSAStartup failed!!!";
> }
>
> Socket = socket(AF_INET, SOCK_STREAM, 0);
> if (Socket == INVALID_SOCKET)
> {
> cout << "Socket creation failed!!!";
> }
>
> SockAddr.sin_port = 50;
> SockAddr.sin_family = AF_INET;
> SockAddr.sin_addr.S_un.S_un_b.s_b1 = 192;
> SockAddr.sin_addr.S_un.S_un_b.s_b2 = 168;
> SockAddr.sin_addr.S_un.S_un_b.s_b3 = 1;
> SockAddr.sin_addr.S_un.S_un_b.s_b4 = 102;
>
> if (bind (Socket, (SOCKADDR *)(&SockAddr), sizeof (SockAddr)) ==
> SOCKET_ERROR)
> {
> cout << "Attempt to bind failed!!!";
> }
>
> listen (Socket, 1);
>
> while (TempSocket == SOCKET_ERROR)
> {
> TempSocket = accept (Socket, &ConnAddr, sizeof (ConnAddr));
> }
> Socket = TempSocket;
>
> printf ("%s", ConnAddr.sa_data);
> while (a <= filename.length())
> {
> send_str[0] = filename[a];
> cout << send_str;
> send (Socket, send_str, 1, 0);
> ++a;
> }
>
> infile.open (filename.c_str());
> while (!infile.eof())
> {
> in_char = infile.get();
> send_str[0] = in_char;
> cout << send_str;
> send (Socket, send_str, 1, 0);
> }
> infile.close();
>
>
> while (RetVal == SOCKET_ERROR)
> {
> RetVal = recv (Socket, recv_str, 6, 0);
> }
> cout << recv_str;
>
> system ("pause");
> return 0;
> }
> The problem text is highlighted in green. I get the error message
> "invalid conversion from 'unsigned int' to 'int *' initializing argument
> 3 of 'SOCKET accept (SOCKET, sockaddr*, int*)'. But how else am i
> supposed to pass the size of the structure to the method?
>
>
> -------------------------------------------------------------------------
> SF.Net email is sponsored by:
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://sourceforge.net/services/buy/index.php
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|