Re: [Dev-C++] Internet programming again
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
>
|