Link to home
Start Free TrialLog in
Avatar of MikeAdamson
MikeAdamson

asked on

How to Transfer Files Between Two Computers

How do you transfer files between to computers if you know there two ip addresses. Sample source would be nice..
Avatar of chensu
chensu
Flag of Canada image

You need to use the Windows Sockets API. Check out the Platform SDK documentation and samples.
Avatar of MikeAdamson
MikeAdamson

ASKER

Ok... a working example would be nice
Avatar of DanRollins
See
http://msdn.microsoft.com/library/devprods/vs6/visualc/vcsample/_sample_mfc_chatter.htm

Alas, this example is much nore complicated than necessary.  The steps are really pretty easy and they are described MSDN in detail.  See the article...

Windows Sockets: Sequence of Operations

And all of the topics unser CSocket when you look under the Index tab.

-- Dan
Hi,
Check out the following samples:

MFC Samples/Internet Samples:
FTPTREE, HTTPSRVR, TEAR

SDK Samples/Internet Samples:
WebPost


on CodeGuru:
http://codeguru.earthweb.com/internet/index.shtml, scroll down to FTP/Fie transfer

Other sites to have a look: CodeProject, PlanetSourceCode
----
a personal note: big points might be more attractive for a very tricky question, that take some research to resolve; I'm not sure if by this way you find  someone who writes "common" code for you.

Smile
Peter

OK, for the big bux, I'll write the code.  Put your email address and I'll send the entire project (creates two exe s)  within the hour!

-- Dan
dwayneswoboda@hotmail.com....
OK, for the big bux, I'll write the code.  Put your email address and I'll send the entire project (creates two exe s)  within the hour!

-- Dan
I have emailed you two separate projects, but I summarize here:

Both are simple Dlg-based projects.  When created with the wizard, a put a check in "Sockets support"

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
In Srvr header, i added to the dlg class
  CSocket m_cSockSrv;
  CSocket m_cSockRx;


In Srvr cpp:
I added this to OnInitDialog()

  m_cSockSrv.Create( 1234 );
  m_cSockSrv.Listen();

I added this to OnButtion1
  m_cSockSrv.Accept( m_cSockRx );

  CSocketFile cSockFile( &m_cSockRx );
  CArchive arRx( &cSockFile,  CArchive::load );

  CString s;
  arRx >> s;

  SetDlgItemText( IDC_EDIT1, s);

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
In Clnt header, i added to the dlg class
  CSocket m_cSockTx;

in Clnt cpp, i added this to OnButton 1

  m_cSockTx.Create(); // port number
  m_cSockTx.Connect("127.0.0.1", 1234);

  CSocketFile cSockFile( &m_cSockTx );
  CArchive arTx( &cSockFile,  CArchive::store );

  CString s= "Hi there!";
  arTx << s;

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
I have hard-coded the IP as "127.0.0.1" since that is the IP of the local machine.
I have hard-coded the port number as 1234.
I have hard-code the message string to send as "Hi there"

-- Dan
I'll tell you when I recieve the files
hot mail rejected the sends

>>> RCPT To:<dwayneswoboda@hotmail.com>
<<< 552 Requested mail action aborted: exceeded storage allocation
554 <dwayneswoboda@hotmail.com>... Service unavailable


I'll try posting separately

-- Dan
I got the email...
and the second one
And how exactly would I send a file? say c:\file.bmp
I'd read the entire file into a buffer and insert it into the archive.  Here's a handy function I use all of the time:

// reads an entire file into a CString
// on error, returns FALSE and the CString contains text of error msg
//
BOOL FileToStr( LPCSTR sFilename, CString& s )
{
  char sBuf[513];
  HFILE hFile= _lopen(sFilename, OF_READ );

  if ( hFile == HFILE_ERROR ) {
    CString sErr;
    GetLastErrorStr( sErr );
    s.Format("File Error Opening '%s'  Reason: %s",
        (LPCSTR)sFilename,
        (LPCSTR)sErr
    );
    return( FALSE );
  }
  int nFileLen= _llseek( hFile, 0L, SEEK_END );
  _llseek( hFile, 0L, SEEK_SET );
  for (int j=0; j<nFileLen; j += 512) {
    int nActual= _lread( hFile, sBuf, 512 );
      CString sTmp= CString(sBuf,nActual);
    s += sTmp;
  }
  _lclose( hFile );
  return( TRUE );
}

-- Dan
What mods other then that do I change in your code?
I'll give you points after I get this working, these extra ports are for this question.. How do I know what port to use for my program? What happens if someothe program is using the port?
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial