locked
Socket Receive Data Error Why? RRS feed

  • Question

  • Hi I am writing a client and a server program and This exception happens. What can I do?

    ((SocketException was unhandled

    A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied ))

    Client

    Public Class Form1
        Dim client_socket As System.Net.Sockets.TcpClient
        Dim server_stream As System.Net.Sockets.NetworkStream
        Dim Server_sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Server_sock.Connected Then
            Else
                Server_sock.Connect("127.0.0.1", 8888)
                MsgBox("Success")
            End If
            Dim out As Byte() = System.Text.Encoding.ASCII.GetBytes("Hello")
            Server_sock.Send(out, SocketFlags.None)
            MsgBox("sent")
        End Sub
    End Class

    And Server

    Public Class Server_Form1
        Public Server_sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Public host_ip As IPAddress = IPAddress.Loopback
        Public ep As New IPEndPoint(host_ip, 8888)
    
        Private Sub Button1_Click(sender As Object, ed As EventArgs) Handles Button1.Click
            Server_sock.Bind(ep)
            Server_sock.Listen(10)
            Server_sock.Accept()
            MsgBox("Success-Server")
    
            Dim data(1024) As Byte
            Dim recv As Int16
            Server_sock.ReceiveTimeout = 50
            recv = Server_sock.ReceiveFrom(data, ep) 'EXCEPTION Thrown at this line'
            MsgBox(recv)
        End Sub
    End Class
    What is the Problem?




    • Edited by Javid5555 Thursday, October 4, 2012 2:19 PM
    • Moved by Mark Liu-lxf Friday, October 5, 2012 4:52 AM (From:Visual Basic General)
    Thursday, October 4, 2012 2:16 PM

All replies

  • Remove the line ReceiveTimeout = 50.  Your code needc to start the server up first and then connect with the client.  The Server will automaticlly close the connection after 50 seconds if you don't start the client.  In many cases a server will listen for days on a port before anybody actually connects.

    The 50 is 50 milliseconds which is very short.  You are probably timing out before the oeration completes, especially if your data size is large.


    jdweng

    Friday, October 5, 2012 11:06 AM
  • The error message is clear; you are accessing a socket that is not connected. Why? Because you are using the listening socket and not the accepted socket...

        Private Sub Button1_Click(sender As Object, ed As EventArgs) Handles Button1.Click
            Server_sock.Bind(ep)
            Server_sock.Listen(10)
            Dim clientSock As Socket = Server_sock.Accept() ' !!!!
            MsgBox("Success-Server")
    
            Dim data(1024) As Byte
            Dim recv As Int16
            clientSock.ReceiveTimeout = 50 ' !!
            recv = clientSock.Receive(data) ' !!
            MsgBox(recv)
        End Sub
    


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Saturday, October 6, 2012 11:10 AM
  • When you tested this -- setting a timeout and having it expire -- did you see that error code? Or did you just guess this answer?

    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Saturday, October 6, 2012 11:15 AM
  • Alan : Did you read my answer!!!  The timeout is only 50msec.

    jdweng

    Saturday, October 6, 2012 11:31 AM
  • I asked: "did you see that error code?"

    Error codes shouldn't just be ignored...

    For your suggestion about ReceiveTimeout, in my testing I always see error code 10060 WSAETIMEDOUT and that matches the documentation. Before you wrote your answer did you do any testing, did you do any reading of the documentation??? Did you just guess?

    * SocketException: TimedOut 10060
    System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
       at System.Net.Sockets.Socket.Receive(Byte[] buffer)
       at Sockets1.Program.Main(String[] args) in C:\Users\alan\Documents\Visual Studio 2008\Projects\Play2b1\Sockets1\Program.cs:line 21


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Saturday, October 6, 2012 2:11 PM
  • alan : I've seen the problem recently and just eliminated the timeout.  I don't just ignore errors, but I understand why they are occocuring. It doesn't make any sense to set a 50msec timeout.  We are now getting into the same discusion we have been having for months about closing and restarting TCP connections.

    jdweng

    Saturday, October 6, 2012 3:11 PM