[英]How to send file to all the clients connected to the server in C# using NetworksApi.TCP.SERVER
here is the SERVER code i want to make a online exam system which will send question.txt and rules .txt to all the clients connected to the server. 这是我要制作一个在线考试系统的服务器代码,它将向所有连接到服务器的客户端发送问题.txt和规则.txt。 i dont know how to send file ..i want to brows the file from local drive then send the file to all the clients please help me
我不知道如何发送文件..我想从本地驱动器中浏览文件,然后将文件发送给所有客户端,请帮助我
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetworksApi.TCP.SERVER;
namespace server
{
public delegate void show_status(string soneya);
public partial class Form1 : Form
{
Server examserver;
string star_time;
string finish_time;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ChangeText(string soneya)
{
if (textBox3.InvokeRequired)
{
Invoke(new show_status(ChangeText), new object[] { soneya });
}
else
{
textBox3.Text += soneya + "\r\n";
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != " ")
{
examserver = new Server(textBox1.Text, textBox2.Text);
examserver.OnClientConnected += new OnConnectedDelegate(examserver_OnClientConnected);
examserver.OnClientDisconnected += new OnDisconnectedDelegate(examserver_OnClientDisconnected);
examserver.OnDataReceived += new OnReceivedDelegate(examserver_OnDataReceived);
examserver.OnServerError += new OnErrorDelegate(examserver_OnServerError);
examserver.Start();
ChangeText("server is Running");
}
}
void examserver_OnServerError(object Sender, ErrorArguments R)
{
//ChangeText("server is not Running");
}
void examserver_OnDataReceived(object Sender, ReceivedArguments R)
{
throw new NotImplementedException();
}
void examserver_OnClientDisconnected(object Sender, DisconnectedArguments R)
{
ChangeText("disconnected "+ R.Name );
}
void examserver_OnClientConnected(object Sender, ConnectedArguments R)
{
ChangeText("accepted request from"+R.Name );
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
star_time = textBox4.Text;
finish_time = textBox5.Text;
ChangeText("Exam will be Start from" + textBox4.Text);
ChangeText("Exam will be finish at " + textBox5.Text);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
}
}
I think the NetworksAPI Library, does not contain a way to "Send" / "Broacast" files. 我认为NetworksAPI库没有包含“发送” /“广播”文件的方法。 (Either to one User or to Everyone).
(向一个用户或每个人)。 Although i believe it is possible if every Client (Works also as Server) and if Server could also work as Client.
尽管我相信,如果每个客户端(也可以作为服务器)以及服务器也可以作为客户端,则是可能的。
Either ways i never tested that, since in my Application i just required to connected my Client to the Server, and communicate with it. 无论哪种方式,我都从未测试过,因为在我的应用程序中,我只需要将客户端连接到服务器并与其通信即可。
I noticed that you've also got your code from the Video Tutorial. 我注意到您还从视频教程中获得了代码。 I made some modifications on my application including adding a method to send file(s) to Server.
我对应用程序进行了一些修改,包括添加了一种将文件发送到Server的方法。 (Wich is not included on that Tutorial)
(该教程中未包括)
I may not Help you 100% in sending a File "Broadcast" (send file to everyone), but i can help you (or anyone who requies a way to do this) into getting a solution to send a file (or Files) from Client to Server. 我可能不会100%帮助您发送文件“广播”(将文件发送给所有人),但是我可以帮助您(或要求这样做的任何人)获得从以下位置发送文件的解决方案:客户端到服务器。
Check My Tutorial Bellow (Suiting as best as possible to your code / the Standard Tutorial found on the Internet): 检查“我的教程”,以下为“最佳”(最适合您的代码/ Internet上的“标准教程”):
"How to send a File From Client to Server" Using NetworksAPI 使用NetworksAPI的“如何从客户端向服务器发送文件”
[On the Client] [在客户端上]
1) create a string: 1)创建一个字符串:
string fileName;
2) create a Get / Set Method: 2)创建一个Get / Set方法:
public string FileName { get { return fileName; } set { fileName = value; } }
3) Under the Button "Send_File_Click" (Explanation): 3)在按钮“ Send_File_Click”下(说明):
a) Configure your OpenFile Dialog a)配置您的OpenFile对话框
b) Then Load OpenFileDialog b)然后加载OpenFileDialog
c) Set the string you created above to OpenFileDialog Selected File c)将您在上面创建的字符串设置为OpenFileDialog Selected File
Code Bellow: 波纹管代码:
private void button_SendFile_Click(object sender, EventArgs e)
{
try
{
ofd = new OpenFileDialog();
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.Multiselect = false;
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
fileName = ofd.FileName;
textbox_FileName.Text = FileName;
c.SendFile(fileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if you want you can add the Transfer Percentage to Your "Status" textBox. 如果需要,可以将转移百分比添加到“状态”文本框中。
Note: I believe that in the Tutorial (Video) where you got that Code the TextBox 注意:我相信在教程(视频)中可以找到该代码的TextBox
is Called: txt_Chat or txt_Message. 被称为:txt_Chat或txt_Message。
Code Bellow: 波纹管代码:
void c_OnClientFileSending(object Sender, ClientFileSendingArguments R)
{
try
{
/*
Set Text stands for the method: "updateText" or "changeText" as referenced in that Tutorial you've seen.
Included a `String.Format` to Properly Display Percentage.
*/
string percentage = String.Format("Transfering: {0}%", R.TransferPercentage); SetText(percentage);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
[On the Server] [在服务器上]
1) Inside "Form1_Load()"
Method you must set your server (Received) Files Path. 1)在
"Form1_Load()"
方法内部,您必须设置服务器(已接收)文件路径。
Example: 例:
private void Form1_Load(object sender, EventArgs e)
{
server.FilesPath = @"C:\";
}
That's all M8! 这就是M8!
Note that there isn't any message (in my code) that will indicate if the file was received or not. 请注意,没有任何消息(在我的代码中)表明是否已接收到该文件。
If you've set the "textbox_Status"
like i mentioned above, you'll notice that it will display: "100%" after the file was sent. 如果您像上面提到的那样设置了
"textbox_Status"
,您会注意到在发送文件后它将显示:“ 100%”。
just check the (server) received files folder after the transfer is complete. 传输完成后,只需检查(服务器)收到的文件文件夹即可。
In this case it will be under C:\\
在这种情况下,它将在
C:\\
The file will be there. 该文件将在那里。
Best Regards; 最好的祝福;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.