Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
GeneralRe: Enterprise Library V5 Logging Pin
Ray Cassick22-Dec-10 21:46
Ray Cassick22-Dec-10 21:46 
GeneralRe: Enterprise Library V5 Logging Pin
dan!sh 22-Dec-10 21:53
professional dan!sh 22-Dec-10 21:53 
GeneralRe: Enterprise Library V5 Logging Pin
Ray Cassick22-Dec-10 22:10
Ray Cassick22-Dec-10 22:10 
QuestionLooking at the remote desktop template, help with automating acknowledgement of the legal banner [modified] Pin
turbosupramk322-Dec-10 8:26
turbosupramk322-Dec-10 8:26 
AnswerRe: Looking at the remote desktop template, help with automating acknowledgement of the legal banner Pin
Richard MacCutchan22-Dec-10 8:59
mveRichard MacCutchan22-Dec-10 8:59 
AnswerRe: Looking at the remote desktop template, help with automating acknowledgement of the legal banner [modified] Pin
RaviRanjanKr22-Dec-10 17:39
professionalRaviRanjanKr22-Dec-10 17:39 
GeneralRe: Looking at the remote desktop template, help with automating acknowledgement of the legal banner Pin
turbosupramk323-Dec-10 0:16
turbosupramk323-Dec-10 0:16 
QuestionHow do I transfer files over a local network? Pin
Megidolaon22-Dec-10 7:12
Megidolaon22-Dec-10 7:12 
I've tried a ton of tutorials but none work. Or rather, sooner or later ALL digress and leave the crucial part of transfering a file that isn't pure text.

I'm remodeling an app I've previously used to transfer text and project specific objects over a LAN. I'm just having trouble to receive the file and save it again after the transfer (I assume the sending of the file is successful, but of course have no way to actually verify this).

Here is the code for my client (sending) app:
private void Send(string arg)
{
    TcpClient client = new TcpClient(TB_Host.Text, (int)UD_Port.Value);
    FileStream fstream = File.Open(OFD_File.FileName, FileMode.Open);
    NetworkStream nstream = client.GetStream();
    int data = 0;

    while (data > 0)
    {
        data = fstream.ReadByte();
        nstream.WriteByte((byte)data);
    }

    fstream.Close();
    nstream.Close();
    client.Close();
}
And this is the code for the server (receiving) app:
public void Listen()
        {
            int port = 21112;
            byte[] result = new byte[1024];
            IPAddress address = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(address, port);
            ipEnd = new IPEndPoint(address, port);
            listener.Start();
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            int f = 0;
            object state = new object();
            socket.Connect(ipEnd);

            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                NetworkStream ns = client.GetStream();

                f = socket.Receive(result);
                Invoke(new UpdateDisplayDelegate(UpdateDisplay), result);
            }
        }
The current problem is that the code never reaches the invoke call (the Listen method is on a 2nd thread), which confuses me as there never had been any problem when I transferred text or objects.

I tried a SoapFormatter, but while it works great for text and objects, I can't get it to work for files.
I also tried using no sockets but a NetworkStream to write into the buffer:
result = ns.Write(result, 0, int.MaxValue);
but I always get an ArgumentOutOfRange exception for the size (I used int.MaxValue) parameter.

Can you tell me how to get it to work? Or is this approach wrong in the first place?
Thanks!
AnswerRe: How do I transfer files over a local network? PinPopular
Luc Pattyn22-Dec-10 8:54
sitebuilderLuc Pattyn22-Dec-10 8:54 
GeneralRe: How do I transfer files over a local network? Pin
Megidolaon22-Dec-10 10:12
Megidolaon22-Dec-10 10:12 
GeneralMy vote of 1 Pin
Keith Barrow22-Dec-10 10:33
professionalKeith Barrow22-Dec-10 10:33 
GeneralRe: My vote of 1 Pin
Megidolaon23-Dec-10 7:55
Megidolaon23-Dec-10 7:55 
GeneralRe: My vote of 1 Pin
Keith Barrow23-Dec-10 7:59
professionalKeith Barrow23-Dec-10 7:59 
GeneralRe: How do I transfer files over a local network? Pin
Henry Minute22-Dec-10 10:34
Henry Minute22-Dec-10 10:34 
GeneralRe: How do I transfer files over a local network? Pin
Luc Pattyn22-Dec-10 10:34
sitebuilderLuc Pattyn22-Dec-10 10:34 
GeneralRe: How do I transfer files over a local network? Pin
Keith Barrow22-Dec-10 10:38
professionalKeith Barrow22-Dec-10 10:38 
GeneralRe: How do I transfer files over a local network? Pin
fjdiewornncalwe22-Dec-10 10:41
professionalfjdiewornncalwe22-Dec-10 10:41 
GeneralRe: How do I transfer files over a local network? Pin
Pete O'Hanlon22-Dec-10 10:46
mvePete O'Hanlon22-Dec-10 10:46 
GeneralRe: How do I transfer files over a local network? Pin
Richard MacCutchan22-Dec-10 11:18
mveRichard MacCutchan22-Dec-10 11:18 
GeneralRe: How do I transfer files over a local network? Pin
dan!sh 22-Dec-10 20:24
professional dan!sh 22-Dec-10 20:24 
GeneralRe: How do I transfer files over a local network? Pin
Richard MacCutchan22-Dec-10 21:59
mveRichard MacCutchan22-Dec-10 21:59 
GeneralRe: How do I transfer files over a local network? Pin
_Erik_23-Dec-10 3:32
_Erik_23-Dec-10 3:32 
AnswerRe: How do I transfer files over a local network? Pin
jschell23-Dec-10 7:08
jschell23-Dec-10 7:08 
QuestionApproximation Taylor and Furie series with neuro network Pin
elinehsani22-Dec-10 0:12
elinehsani22-Dec-10 0:12 
AnswerRe: Approximation Taylor and Furie series with neuro network Pin
phil.o22-Dec-10 0:23
professionalphil.o22-Dec-10 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.