需要有关套接字的帮助 [英] Need help with Socket

查看:74
本文介绍了需要有关套接字的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用Socket和FileStream将文件上传到Web的程序.这是代码

I am writing a program to upload file to the web using Socket and FileStream. Here is the code

    Private Sub Read(ByVal state As StateObject)
        Dim filestream As FileStream = state.fileStream
        filestream.BeginRead(state.readBuffer, 0, StateObject.readBufferSize, New AsyncCallback(AddressOf ReadCallBack), state)
    End Sub

    Private Sub ReadCallBack(ByVal ar As IAsyncResult)
        Dim state As StateObject = CType(ar.AsyncState, StateObject)
        Dim client As Socket = state.workSocket
        Dim fileStream As FileStream = state.fileStream

        Dim bytesRead As Integer = fileStream.EndRead(ar)

        Console.WriteLine("Read: " & (bytesRead))
        If bytesRead > 0 Then
            Send(state, bytesRead)
        Else
            fileStream.Close()
            sendDone.Set()
            readDone.Set()
        End If
    End Sub

    Private Sub Send(ByVal state As StateObject, ByVal bytesread As Integer)
        ' Console.WriteLine("Send")
        ' Begin sending the data to the remote device.
        Dim client As Socket = state.workSocket
        state.totalbytesSend += bytesread
        Console.WriteLine("Send: " & bytesread)
        Console.WriteLine("Send Total: {0}", state.totalbytesSend)
        client.BeginSend(state.readBuffer, 0, bytesread, 0, New AsyncCallback(AddressOf SendCallback), state)
    End Sub 'Send

    
    Private Sub SendCallback(ByVal ar As IAsyncResult)
        ' Console.WriteLine("SendCallBack")
        ' Retrieve the socket from the state object.
        Dim state As StateObject = CType(ar.AsyncState, StateObject)
        Dim client As Socket = state.workSocket
        Dim filestream As FileStream = state.fileStream

        ' Complete sending the data to the remote device.
        Dim bytesSent As Integer = client.EndSend(ar)

        ' Console.WriteLine("Sent {0} bytes to server.", bytesSent)

        ' Signal that all bytes have been sent.

        If filestream.CanRead Then
            Read(state)
        Else
            filestream.Dispose()
            sendDone.Set()
        End If
    End Sub 'SendCallback

问题是,如果我总是收到远程主机强行关闭现有连接"的消息, (错误代码10054)错误.
如果我更改线路

The problem is that if i always get "An existing connection was forcibly closed by the remote host" (error code 10054) error.
If i change the line

client.BeginSend(state.readBuffer, 0, bytesread, 0, New AsyncCallback(AddressOf SendCallback), state)





to

client.BeginSend(state.readBuffer, 0, bytesread + 1, 0, New AsyncCallback(AddressOf SendCallback), state)

然后它不会给我错误,但是这弄乱了我的二进制文件.有人知道如何解决这个问题吗?

谢谢!

then it wont give me the error, but this mess up my binary file. Does anyone know how to fix this??

Thanks!!

推荐答案




尝试引用以下URL进行套接字编程.希望能帮助您理解原因.
Hi,


Try to refer the following URL for socket programming. I hope it will help to understand the reason.

http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/ada4855b-efb2-4a6a-9c5f-cde498fae59f/








Thanks and Regards,


这篇关于需要有关套接字的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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