Jump to content

Help with Sockets!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
I've been playing around with some socket programming in .NET recently, and I'm a bit stuck with getting a couple of things working correctly.

At the moment I can connect to an IP, given that it is actually available, and I can transfer data across to the server, and back, using the System.Net.Sockets namespace.

However there are a couple of things I'm struggling with. The most annoying of which is trying to deal with connection timeouts. Is there a way I can set the timeout length (ideally don't want it too long, as I don't want it to freeze my program). I have tried using socketname.SendTimeout and socketname.RecieveTimeout, but am not sure of the correct ways to use these.

Second problem on the same note... how do I find a timeout once my program has connected to the server already?

Thanks in advance, here is the connection part of my code (client side).


Private Sub cmdConnect_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles cmdConnect.Click

        If Connected = False Then

            [b]clientSocket.SendTimeout(2000) 'unsure of quite how to use[/b]

            clientSocket.Connect("192.168.1.67", 8888)

            Connected = True

            readData = LocalUserAlias & " Connected to Chat Server ..."

            msg()

            txtChat.Text = "Chramy Messenger - Server Connected ..."

            serverStream = clientSocket.GetStream()


            Dim outStream As Byte() = _

            System.Text.Encoding.ASCII.GetBytes(LocalUserAlias + "$")

            serverStream.Write(outStream, 0, outStream.Length)

            serverStream.Flush()


            Dim ctThread As Threading.Thread = _

       New Threading.Thread(AddressOf getMessage)

            ctThread.Start()

            cmdConnect.Text = "Disconnect from Chramy Messenger!"

        Else

            clientSocket.Close()

            Connected = False

        End If

    End Sub



#2
debtboy

debtboy

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 916 posts
Edited:
Sorry, I was thinking of something very different...
Thanks for your patience

Edited by debtboy, 12 October 2009 - 06:10 AM.


#3
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Ok, thanks debtboy. So are you saying that before I change any socket timeouts in my code, I need to change it in my localhost?

Basically, I'm stuck on how to handle a connection that is interrupted/cannot be made. At the moment my program crashes, but obviously what I would like is a timeout period, then be able to let the user know it cannot connect.