Click here to Skip to main content
14,574,881 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am creating a server/client file transferring application in which only server can send file to client through socket. I succeeded in sending the file but the problem is that server can send only one file & when I try to send the next file after the first file is delivered on the client I get the following error "Operation not allowed on non-connected sockets".

Here is my server code:

''#sends filename, filelength, filebytes

        Dim info As New IO.FileInfo("D:\setup_ais.exe")

        ''#writes a String and a Long with binarywriter (wrapping networkstream)
        Dim bw As New IO.BinaryWriter(clientSocket.GetStream)
        bw.Write(info.Name)
        bw.Write(info.Length)
        ''#using filestream to read file, writes this directly to networkstream
        Using fs As New IO.FileStream("D:\setup_ais.exe", IO.FileMode.Open, IO.FileAccess.Read)
            Dim buffer(8092) As Byte, reads As Integer = -1
            Do Until reads = 0
                reads = fs.Read(buffer, 0, buffer.Length)
                clientSocket.GetStream.Write(buffer, 0, reads)
            Loop
        End Using
        bw.Close()
        clientSocket.Close()
        serverSocket.Stop()


Here is my client code:

''#receives filename, filelength, filebytes
        Dim filename, filepath As String, filelen As Long
        ''#using binaryreader (wrapped networkstream) to read a String and a Long
        Dim br As New IO.BinaryReader(clientSocket.GetStream)
        filename = br.ReadString
        filelen = br.ReadInt64
        ''#filepath = IO.Path.Combine(Application.StartupPath, filename)

        filepath = IO.Path.Combine("C:\", filename)
        Dim buffer(8092) As Byte, readstotal As Long = 0
        Dim reads As Integer = -1
        ''#using filestream to write read filebytes directly from networkstream
        Using fs As New IO.FileStream(filepath, IO.FileMode.Create, IO.FileAccess.Write)
            Do Until readstotal = filelen
                reads = clientSocket.GetStream.Read(buffer, 0, buffer.Length)
                fs.Write(buffer, 0, reads)
                readstotal += reads
            Loop
        End Using
        MsgBox("received: " & filename)
        br.Close()
        clientSocket.Close()

Please give me some solution.


[Edited]Code is blocked in "pre" tags[/Edited]
Posted
Updated 1-May-11 17:41pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Sure. One part should listen on the socket and accept it, another part should connect, then you can do further operations. Also, you normally will need to create two threads on listening (server's) side: one is accepting for remote socket connections, another one is implementing your network read/write application protocol with all the clients. Instead of raw sockets, I recommend using equivalent operations using System.Net.Sockets.TcpListener/TcpClient.

See
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx[^].

You may want to look at my skeleton description of proper design:
Multple clients from same port Number[^].

My collection of links to my past answer on related threading topic can also be helpful:
Control events not firing after enable disable + multithreading[^].

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100