CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    [RESOLVED] MFC & Internet

    Hi!

    I am trying to develop a SIMPLE application that should transfer some files to another computer (i.e. from China to UK). I have succeeded some sort of data transfer (using CSocket - a simple way from MFC), but it seems it works only on the local network. and I have absolutely no ideea how to make it work on a wide network.

    Can somebody PLEASE help me?
    (this is a both Visual C++ (MFC) and a networking thing, so I don't pretty know where it fits)

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: MFC & Internet

    CSocket will definitely work over the internet, but the socket server must be listening on a live IP address to which the client socket can connect.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: MFC & Internet

    ok, and... can you please tell me how to do that?
    I don't see a way I can call listen with other parameters and to work...
    Code:
    BOOL Listen(
       int nConnectionBacklog = 5 
    );

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: MFC & Internet

    There will be no change in the code.
    Do you mean the server itself is failing or is the connection not being established?
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  5. #5
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: MFC & Internet

    the connection to the server return an error...
    I checked with GetLastError() and returned 10060, which is WSAETIMEDOUT.

    one more question I have: how is the client going to find my IP address which is x.x.x.x over the whole internet?
    when the client attempts to connect, it has the x.x.x.x ip and the port number, but are they unique through internet? (that's hard to believe)

  6. #6
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: MFC & Internet

    ok... one thing I found out...
    it seems like I used my address from the router (for which, the connection to the other computer connected on the same router worked). I now used my 'real' ip, which should be the one specified here: http://www.whatsmyip.org. now, I don't get 10060 error anymore, but 10061 (WSAECONNREFUSED), and I can't connect at all.

    does anybody have an ideea what I can do?
    Last edited by Feoggou; September 12th, 2009 at 05:56 AM.

  7. #7
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: MFC & Internet

    You need to check if the server is listening on the port.
    Do a netstat -a command on the server to find out.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  8. #8
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: MFC & Internet

    yeah...
    my port nr is 5150
    I used netstat -a -n and saw that the ip address is 0.0.0.0:5150 (well, my real ip address I couldn't find, instead I found 0.0.0.0, 127.0.0.1, and the one assigned by the router) which is listening.

    with netstat -a I found my_username:5150 LISTENING

    by the way... the connection is started on a different thread... it looks like this:

    Code:
    DWORD ThreadProc(CWnd* pWnd)
    {
    	CSocket sockServer;
    	AfxSocketInit();   //i heard it must be used in every thread.
    	sockServer.Create(5150);
    	sockServer.Listen();
    
    	//sockServer
    	CSocket sockConnection;
    	sockServer.Accept(sockConnection);
    	
    	CSocketFile file(&sockConnection);
    	CArchive arIn(&file, CArchive::load);
    	arIn.ReadString(retValue);
    
    	pWnd->SetDlgItemTextW(IDC_EDIT1, retValue);
    	
    	arIn.Close();
    	file.Close();
    	sockServer.Close();
    
    	return 0;
    }
    the client is a program that has an editbox and a send button. when I push Send, the function it calls is:

    Code:
    void CSKClientDlg::OnBnClickedOk()
    {
    	// TODO: Add your control notification handler code here
    	UpdateData();
    	if (m_sEdit.GetLength() == 0)
    	{
    		MessageBox(L"Nothing written!");
    		return;
    	}
    	
    	CSocket sockClient;
    	sockClient.Create();
    	if (0 == sockClient.Connect(L"my.real.ip address.here", 5150))
    	{
    		DWORD k = GetLastError();
    		CString s;
    		s.Format(L"Could not connect! Error %u", k);
    		MessageBox(s);
    		return;
    	};  
    
    	CSocketFile file(&sockClient);
    	CArchive arOut(&file, CArchive::store);
    	arOut.WriteString(m_sEdit);
    
    	arOut.Close();
    	file.Close();
    	sockClient.Close();
    
    	MessageBox(L"Information Sent!");
    }
    did i write it wrong?

  9. #9
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Post Re: MFC & Internet

    or, do I need special settings or hardware, or something, in order to be able to act as a server? (it is the first time I try to make such kind of application).

  10. #10
    Join Date
    Feb 2005
    Posts
    2,160

    Re: MFC & Internet

    It sounds like your server may be behind a router/firewall. In this case, you'll need to open a port-forward in your router. If your server is listening on a private network, then it can't be seen from the rest of the Internet without the router handling the transition between your private network and the Internet.

  11. #11
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Post Re: MFC & Internet

    if I did nothing to my computer (my computer is an ordinary computer, I didn't install ISS or anything else) should it anyway work? or do I have to do something? can you please tell me what to do, or give me a link where it is explained?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: MFC & Internet

    Wouldn't it be easier to just use CFtpConnection?

  13. #13
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Unhappy Re: MFC & Internet

    I tried the thing with the router...

    on the router I had set the start ip = 192.168.2.2 and the end ip = 192.168.2.5 (I have accessed my router with 192.168.2.1). On settings, I chose NAT->Port Forwarding, and inserted ip = 192.168.2.5 (if I insert any ip I wish I get the message "It should be set within the current subnet" - which I understood that my ip must start with 192.168.2, I set the type = TCP+UDP, though I use on my program only TCP. At the port range field I chose the port that I set for my program: 5150.


    I have tried the programs on my computer. I have changed for the client, the ip to which it connects. I have tried putting the 192.168.2.5 and it didn't work; I have tried putting my real ip and it didn't work either. I get connection error on client 10060 which is WSAETIMEDOUT.

    about ftp... well... I don't know how to use it. and this variant seemed convenient as the file that I will send will rarely have more than 1 MB. The one I tried with had 126 KB. And about sockets, I have more than 0% ideea what it is about; + I need to know how to make a good data transfer (for future). After I will succeed in doing it, I might start to learn ftp.

    can you please help me?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured