473,239 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

C# file Transfer

Hello Everyone,

I have been looking around a lot to create a server app which can
accept multiple client connections and receive files. So far I have
combined a few examples and added lots of my own code to make it do
what I want so far, but at the moment I知 having trouble sending/
receiving an actual file. I知 just not sure on the best way to do it.
I致e provided parts of my code below. I知 just not sure the best way
to receive the file stream and re-create it in to the file again.

Any help would be greatly appreciated.

Client Code

I connect to the server using

m_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );

I can send normal text data to the server fine

Object Data = message.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(Data.ToString ());
m_socClient.Send (byData);

But now I want to send a file instead
m_socClient.SendFile(@"C:\NewDocument.doc");

So I do this, and it sends and the server recieves it, but displays it
as weird ASCII charactors in message received box (obviously). Is this
the best way to send a file?
The server Code

I listen on
m_socListener = new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

When the data is received

public void WaitForData(System.Net.Sockets.Socket soc)
{
if ( pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.dataBuffer ,
0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnWorkerCallBack,theSocPkt);
}
public void OnDataReceived(IAsyncResult asyn)
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
//Writes to textbox
txtDataRx.Text += status;
WaitForData(theSockId.thisSocket);
}

So how do I modify this to receive a file propperly?
Thanks in advance
Jul 30 '08 #1
3 5165
Bonzol wrote:
I have been looking around a lot to create a server app which can
accept multiple client connections and receive files. So far I have
combined a few examples and added lots of my own code to make it do
what I want so far, but at the moment I知 having trouble sending/
receiving an actual file. I知 just not sure on the best way to do it.
I致e provided parts of my code below. I知 just not sure the best way
to receive the file stream and re-create it in to the file again.
m_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );

I can send normal text data to the server fine

Object Data = message.Text;
Why object and not string ?
byte[] byData = System.Text.Encoding.ASCII.GetBytes(Data.ToString ());
m_socClient.Send (byData);
Encoding.ASCII will only work with characters in US ASCII.

I doubt that is your intention.
But now I want to send a file instead
m_socClient.SendFile(@"C:\NewDocument.doc");
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
Now you assume UTF-8 ?
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
I suggest you fix the character set stuff and if it still fails post
the correct content and how it looks like with your code.

Arne
Jul 30 '08 #2
I suggest you fix the character set stuff and if it still fails post
the correct content and how it looks like with your code.
Better yet, change from string to byte arrays so there is no character set
conversion whatsoever, if you want a faithful copy.
>
Arne

Aug 6 '08 #3
Ben Voigt [C++ MVP] wrote:
>I suggest you fix the character set stuff and if it still fails post
the correct content and how it looks like with your code.

Better yet, change from string to byte arrays so there is no character set
conversion whatsoever, if you want a faithful copy.
He is sending and receiving bytes.

But he has the data in a String, so he has to convert from
String to Bytes.

Arne
Aug 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
11
by: Stephan Steiner | last post by:
Hi Generally, FileInfo fi = new FileInfo(path); long size = fi.Length; gets you the length of a file in bytes. However, when copying files, even while the copy operation is still in...
8
by: Xarky | last post by:
Hi, I am downloading a GIF file(as a mail attachement) with this file format, Content-Transfer-Encoding: base64; Now I am writing the downloaded data to a file with this technique: ...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
by: Alex | last post by:
Hello, I'm trying to write a little php script to transfert some files from a server to clients (web/http). It's working fin with small files. But transfering big files (try on 1Gb) failed!...
10
by: David | last post by:
I have googled to no avail on getting specifically what I'm looking for. I have found plenty of full blown apps that implement some type of file transfer but what I'm specifcally looking for is an...
2
by: tedpottel | last post by:
Hi, My program has the following code to transfer a binary file f = open(pathanme+filename,'rb') print "start transfer" self.fthHandle.storbinary('STOR '+filename, f) How can I do an ASCII...
0
by: fiona | last post by:
Yucca Valley, CA, - October 2007: Catalyst Development Corporation, publisher of SocketTools, SocketWrench and LogicGem, today announced the release of Catalyst File Transfer .NET V5.0. For...
6
by: Thom Little | last post by:
I need to transfer an XML file between an application on the client and the server. This would in fact be a copy of the application's .config file. HTML <input form=...would transfer the file...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you値l learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.