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

Problems sending and receiving files

Lee
Hi,

I am trying to send and receive a file (it is actually an xml file, however
any file may possibly be sent) using;-

sending .....

public void send()
{

byte[] data = new byte[1024];
string input, stringData;
TcpClient client;
try
{
client = new TcpClient(server, 9050);
}
catch (SocketException)
{
Console.WriteLine("Unable to connect to server");
return;
}

NetworkStream ns = client.GetStream();
int recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);

input = "STOR";
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
ns.Flush();

data = new byte[1024];
recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);

FileInfo finfo = new FileInfo(theFile);
input = finfo.Length.ToString();
Console.WriteLine(input);
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
ns.Flush();

FileStream fs = new FileStream(theFile, FileMode.Open);
BinaryReader r = new BinaryReader(fs, Encoding.UTF8);

long len = finfo.Length;
int bytesSent = 0;
fs.Position = 0;

while (len > 0)
{
data = new byte[1024];

bytesSent = r.Read(data, 0, data.Length);
ns.Write(data, 0, bytesSent);
ns.Flush();

len = len - bytesSent;
}

Console.WriteLine("Disconnecting from server...");
ns.Close();
client.Close();

}
and receiving using ...

(have not put in all the preamble, code up to here works fine)

public void receive()
{
string welcome = null;
long length;
string clientdata = null;
int bytesRead = 0;
byte[] bytes = new byte[1024];

welcome="SENDLENTH";
bytes = Encoding.ASCII.GetBytes(welcome);
ns.Write(bytes, 0, welcome.Length);
ns.Flush();
bytes = new byte[1024];
bytesRead = ns.Read(bytes, 0, bytes.Length);
clientdata = Encoding.ASCII.GetString(bytes, 0, bytesRead);
Console.WriteLine(clientdata);
length = long.Parse(clientdata);

welcome = "SEND";
bytes = Encoding.ASCII.GetBytes(welcome);
ns.Write(bytes, 0, welcome.Length);
ns.Flush();
while (length > 0)
{
bytes = new byte[1024];
bytesRead = ns.Read(bytes, 0, bytes.Length);
clientdata = clientdata + Encoding.UTF8.GetString(bytes, 0,
bytesRead);
length = length-bytesRead;
}

Console.WriteLine(clientdata);
}

end of code

now when i recieve a small xml file it always has a few random chars (3 in
fact) as a preamble to it, also if i try sending a big file it seems to send
ok, but the server hangs (and or kicks up errors).

am i missing something obvious?

TIA
May 23 '06 #1
0 1297

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

Similar topics

2
by: Chuck Marques | last post by:
I'm trying to implement sending an object through messaging, but I receive an error stating I can't deserialize the object. Any clues as to why. I have the following: <Serializable()> Public...
2
by: lucifer | last post by:
actually i am transmitting data over wireless medium so i have to control the data ie slow it down thats i have to get a delay in the transmitting function inversely to the speed of the the...
3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
1
by: Amadej | last post by:
Hello everyone, I'm having some odd problems with a little program I wrote for sending/receiving bytes across the network. I am using synchronous sockets, and it seems that when I send byte...
6
by: Piedro | last post by:
Hi, has anyone in here got a good example or some hints on how to send a file directly from one pc to another pc using vb.net, for example I've got my pc's ip and a friends pc's ip and I want to...
0
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples...
1
by: Andreas R. | last post by:
Hello, I'm using Python's asynchat module for network support in a Python-based game, and I run into two problems, both of which occur at random times. Most of the time, the network sending and...
3
by: WoodenSWord | last post by:
Hello, I am just wondering what is the best practice for sending/receiving XML Files Through a web service method. For example: 1) As a string <WebMethod()> _ Public Function...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.