473,383 Members | 1,929 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,383 software developers and data experts.

Losing data with Socket.SendFile

Hi all,

Probably a silly mistake, but I seem to be losing 24 bytes every time I
attempt to send a file using Socket.SendFile().

Here is my code:

Here is the server method:

void receiveCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;

MemoryStream ms = new MemoryStream();
FileStream fs = null;
try
{
int totalBytes = 0;
int numBytes = 0;
do
{
totalBytes += numBytes =
handler.Receive(state.buffer);
ms.Write(state.buffer, 0, numBytes);
System.Threading.Thread.Sleep(500);
} while (numBytes 0);

fs = new FileStream(@"C:\abc.doc", FileMode.Create);
ms.WriteTo(fs);
}
catch (Exception e)
{
EventLog.WriteEntry("File Transfer failed: \n" +
e.Message, System.Diagnostics.EventLogEntryType.Error);
Debug.WriteLine(e);
}
finally
{
if (fs != null)
{
fs.Flush();
fs.Close();
}
ms.Close();
handler.EndReceive(ar);
}
}
and the test client uses simply:

static void SendFile(string file)
{
//socket.Send(new byte[1]); //UNCOMMENT THIS LINE
socket.SendFile(file);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}

If I send something down the pipe - even just one byte - before sending
the file, the file will transfer successfully. I guess I'm not
initialising the socket correctly at one end or the other.

Does anyone have any idea on this?

Cheers,

Matt Jacobsen

Dec 12 '06 #1
0 2452

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

Similar topics

2
by: Fatih BOY | last post by:
Hi, I want to send a report from a windows application to a web page like 'report.asp' Currently i can send it via post method with a context like local=En&Username=fatih&UserId=45&Firm=none...
2
by: bernardpace | last post by:
Hi, I am writing a client server application using TcpClient and TcpListener classes. Now I need to transfer structured large amounts of data. I was thinking to transfer the data in an xml...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
0
by: Simon Says | last post by:
Hi all, Can anyone advice on how to use the SendFile function? If I have a connection-oriented socket, I'll do a sck.SendFile("C:\test.txt") to send out the file, but what should I do on...
1
by: Aerodyne | last post by:
Hi all, I'm using .NET 2.0 but unfortunately there's a windows service that needs to be implemented in .NET 1.1 ... so I need to send a file to a server, what's the similar method of doing so...
0
by: iwdu15 | last post by:
hi, using sockets, how can i receive a file? i see a SendFile method, but no receivefile? ive done simple text sending and bytes, but that requires knowing an array size...so how can i receive a...
0
by: iwdu15 | last post by:
hi, ive done work with sockets and such before, so its nothing new. thought id try the Socket.SendFile() method instead of the manual way. however, there is no Socket.ReceiveFile() method. How do...
7
by: DaTurk | last post by:
Hi, I'm trying to send a large number of messages to a UDP multicast group with which I have one client subscribed. But I'm losing mesages, and I'm not entirely sure why. I can't seem to find...
4
by: id.engg | last post by:
logFileName = 'log.txt' logfile = open(logFileName, "a") class MyUDPServer(SocketServer.UDPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8388608)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.