windows 如何使用c将文件从Linux复制到Windows服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/186944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:25:10  来源:igfitidea点击:

How to copy file from linux to windows server using c

cwindowslinuxfilecopy

提问by Scott Bennett-McLeish

I have to create a C program which will run on Linux server. It will take information from Oracle database, create a local file and then copy that file to Windows server. I know how to create a local file on Linux server. But what is the way to copy it to windows server from C?

我必须创建一个将在 Linux 服务器上运行的 C 程序。它将从 Oracle 数据库获取信息,创建一个本地文件,然后将该文件复制到 Windows 服务器。我知道如何在 Linux 服务器上创建本地文件。但是从 C 复制到 windows server 的方法是什么?

回答by Tomasz Tybulewicz

Mount Windows Share first and then create the file in the mounted directory.

首先挂载 Windows 共享,然后在挂载的目录中创建文件。

回答by Remo.D

It depends on the type of connectivty between the two machines and on the level of security you have to achieve.

这取决于两台机器之间的连接类型以及您必须达到的安全级别。

The simplest scenario would be with the two machine on the same LAN and no particular security. In this case possible solution would be:

最简单的情况是两台机器在同一个局域网上,没有特别的安全性。在这种情况下,可能的解决方案是:

  • Samba: Share a directory on the Win machine, install/configure Samba on the Linux box. The C program will see the shared disk as a local disk under a specific path (e.g. /win/share).

  • NFS: Alternatively you can export a directory on Linux using NFS and install/configure an NFS product on the Win machine. I see this as a second option, if Samba cannot be used for any reason (e.g. security/authentication).

  • ftp: you will need an ftp server on the Windows machine. It also will be trickier to copy the file via a C program. If I'm not mistaken the ftp client on Linux is interactive and it is not supposed to be used in a script (or via another program) but you should check.

  • http: you will need an http server on the Windows machine and a page that would allow upload (IIS plus some asp page, should suffice) and use libcurlto dialog with it.

  • Samba:在Win机器上共享一个目录,在Linux机器上安装/配置Samba。C 程序会将共享磁盘视为特定路径(例如 /win/share)下的本地磁盘。

  • NFS:或者,您可以使用 NFS 在 Linux 上导出目录并在 Win 机器上安装/配置 NFS 产品。如果出于任何原因(例如安全/身份验证)无法使用 Samba,我认为这是第二个选择。

  • ftp:您需要在 Windows 机器上安装一个 ftp 服务器。通过 C 程序复制文件也会更加棘手。如果我没记错的话,Linux 上的 ftp 客户端是交互式的,不应该在脚本中使用(或通过其他程序),但您应该检查一下。

  • http:您需要在 Windows 机器上有一个 http 服务器和一个允许上传的页面(IIS 加上一些 asp 页面,应该足够了)并使用libcurl与它对话。

More complicated scenario when security is a concerm, would require the use of scp or sftp over and SSL connection. Also the libcurl with https could provide a good enough solution.

当安全是一个问题时,更复杂的场景将需要使用 scp 或 sftp over 和 SSL 连接。此外,带有 https 的 libcurl 可以提供足够好的解决方案。

My advice is: try Samba first and see if meets your need, all the other options will require more work to you as a programmer.

我的建议是:首先尝试 Samba,看看是否满足您的需求,所有其他选项都需要您作为程序员做更多的工作。

回答by Scott Bennett-McLeish

Perhaps you could simply use smbclient? No need to setup any servers or anything, just have a shared drive of some kind on the server.

也许您可以简单地使用 smbclient?无需设置任何服务器或任何东西,只需在服务器上安装某种共享驱动器即可。

smbclient //myserver/my_directory <password> -U [domain/]<my_user>

Then you can just 'put' and 'get' whichever files you like betwen the current directory on the linux box and your windows server.

然后,您可以在 linux 机器上的当前目录和您的 Windows 服务器之间“放置”和“获取”您喜欢的任何文件。

put my_file_to_copy.dat

Thats about it.

就是这样。

回答by MarkR

Yep - just mount the windows box using whatever network filesystem you want (e.g. Samba) and copy the file into that directory using normal IO primitives.

是的 - 只需使用您想要的任何网络文件系统(例如 Samba)挂载 Windows 框,然后使用普通 IO 原语将文件复制到该目录中。

Mark

标记

回答by Ray

How about using SMTP and mailing it?

使用 SMTP 和邮寄怎么样?

回答by Andrew

Or use FTP. There's plenty of FTP libraries you can link into your Linux C code.

或者使用FTP。有很多 FTP 库可以链接到您的 Linux C 代码中。

回答by tafa

The windows machine should provide a mean to accept this behaviour first (FTP comes to mind). You may develop an application to run on windows machine to accept the file, but easier is use one of the already developed ones.

Windows 机器应该首先提供一种接受这种行为的方法(想到 FTP)。您可以开发一个在 Windows 机器上运行的应用程序来接受文件,但更容易使用已经开发的应用程序之一。

filezillais an example.

filezilla就是一个例子。

For the actual question, how to upload the file, any ftp client would do the trick.

对于实际问题,如何上传文件,任何 ftp 客户端都可以解决问题。