未连接的插座上不允许进行操作 [英] Operation not allowed on non-connected sockets

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

问题描述

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

推荐答案

可以.一部分应侦听套接字并接受它,另一部分应进行连接,然后才能进行进一步的操作.同样,您通常需要在侦听(服务器)端创建两个线程:一个正在接受远程套接字连接,另一个正在与所有客户端一起实现您的网络读/写应用程序协议.我建议不要使用原始套接字,而应使用System.Net.Sockets.TcpListener/TcpClient等效操作.

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

您可能需要看一下我对适当设计的简要描述:
来自同一端口号的多个客户端 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆