未连接的插座上不允许进行操作

未连接的插座上不允许进行操作

本文介绍了未连接的插座上不允许进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个服务器/客户端文件传输应用程序,其中只有服务器可以通过套接字将文件发送到客户端.我成功发送了文件,但问题是服务器只能发送一个文件&;当我在客户端上传递第一个文件后尝试发送下一个文件时,出现以下错误"Operation not allowed on non-connected sockets".

这是我的服务器代码:

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.


Code is blocked in "pre" tags[/Edited]

推荐答案


这篇关于未连接的插座上不允许进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:19