469,908 Members | 2,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

sending over asynchronous socket connect and disconnect

11
Hi all,

can anybody help me out there? I am developing a client sockect to send transactions form a DB to a server socket. the way i want is to connect, send, recieve , disconnect, connect, send, recieve and disconnect.... like that until all the transactions are uploaded. my problem is when i send, I am shutting down the socket and when i try to connect and send again i get the following error "Once the socket has been disconnected, you can only reconnect again asynchronously, and only to a different EndPoint. BeginConnect must be called on a thread that won't exit until the operation has been completed."
Oct 16 '07 #1
13 6848
Plater
7,872 Expert 4TB
Are you using UDP, why are you using BeginConnect and all that business?
Oct 16 '07 #2
trymore
11
Hi all,

can anybody help me out there? I am developing a client sockect to send transactions form a DB to a server socket. the way i want is to connect, send, recieve , disconnect, connect, send, recieve and disconnect.... like that until all the transactions are uploaded. my problem is when i send, I am shutting down the socket and when i try to connect and send again i get the following error "Once the socket has been disconnected, you can only reconnect again asynchronously, and only to a different EndPoint. BeginConnect must be called on a thread that won't exit until the operation has been completed."


anybody please help me. i am stuck
Oct 16 '07 #3
trymore
11
is there any other way around it besides using beginconnect? please help
Oct 16 '07 #4
trymore
11
by the way i am using multithreading also. i have ten threads each has its own beginconnect and send subs
Oct 16 '07 #5
Plater
7,872 Expert 4TB
Shouldn't there be a "server" just listening for connections?
Then your "client" makes a socket, connects, sends data, gets reply, disconnects, closes and disposes of the socket.
????
Oct 16 '07 #6
trymore
11
the server that i am sending to i have no control of how it should recieve my data. It will only take one tran then dissconnects and wait for a connect request again until i push all the transactions
Oct 16 '07 #7
Plater
7,872 Expert 4TB
that sounds fine. so just do the client part
Oct 16 '07 #8
trymore
11
any suggestions guys to the error i am getting?
Oct 17 '07 #9
Plater
7,872 Expert 4TB
Yes, don't use the same socket object in all of your threads.
Give each thread their own socket.
Oct 17 '07 #10
trymore
11
what do you mean creating a socket fopr each connection? do you mean a different endpoint or just a different socket name but connecting to the same EP(IP addr and port)?
i can see we are getting closer bro.
Oct 17 '07 #11
trymore
11
this is the code i am using. have a look at it. It is in a while loop and r is the looping variable i am using.

Try
If client1.Connected = False Then
client1.BeginConnect(remoteEP, New AsyncCallback(AddressOf Async.ConnectCallback1), client1)
End If
'System.Threading.Thread.Sleep(3000) ' pause for three secs to allow recieving of ENQ

' Wait for connection.
Async.connectDone.WaitOne()
'If ReceiveENQ(client) = "05" Then

' Send data to the remote device.
System.Threading.Thread.Sleep(5000) ' pause for three secs to allow recieving of ENQ
Async.Send1(client1, buffer1)
Async.sendDone.WaitOne()
Async.Receive1(client1) ' recieve ack
Async.Receive1(client1) ' recieve resp
System.Threading.Thread.Sleep(3000)
' Receive the response from the remote device.
Async.Send1(client1, Chr(6)) ' send ack
Async.sendDone.WaitOne()
Async.Send1(client1, bufferZone1) ' send CONF REQUEST
Async.sendDone.WaitOne()

Async.Receive1(client1) ' recieve ack
System.Threading.Thread.Sleep(2000)
'receiveDone.WaitOne()
Async.Send1(client1, Sringer1)
Async.sendDone.WaitOne()
'System.Threading.Thread.Sleep(5000)
' client1.Close()
'System.Threading.Thread.Sleep(2000)
' Write the response to the console.
client1.Disconnect(True)
Catch e As Exception
Console.WriteLine(e.Message)
Console.WriteLine("error in the sending region of code ")

Finally
r += 1

End Try
Oct 17 '07 #12
trymore
11
its solved now. cheers,
Oct 19 '07 #13
Hi,

I am getting the same error message with below code. What should I modify to get it working?

Private Sub clnt_reboot()
For n = ipcounterstart To ipcounterend
Dim mySocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

Try
Do While (i < Reboot.Length)
j = Reboot.IndexOfAny(reserved, i)
If (j = -1) Then
UrlEncoded.Append(HttpUtility.UrlEncode(Reboot.Sub string(i, (Reboot.Length - i))))
Exit Do
End If
UrlEncoded.Append(HttpUtility.UrlEncode(Reboot.Sub string(i, (j - i))))
UrlEncoded.Append(Reboot.Substring(j, 1))
i = (j + 1)
Loop
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString)
Dim ep As New IPEndPoint(IPAddress.Parse(net & "." & ipcounterstart), Convert.ToInt16(startport))
socket.Connect(ep)
socket.SendTo(SomeBytes, ep)
Dim cnt As Integer = 0
Dim bytBuffer As Byte() = New Byte(&H400 - 1) {}
Do
cnt = socket.Receive(bytBuffer)
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30)
Loop While (cnt <> 0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
mySocket.close()

Next
MessageBox.Show("Reboot has been send")
ipcounterstart = startoktett
End Sub
Oct 29 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Matthew King | last post: by
6 posts views Thread by Silby | last post: by
4 posts views Thread by carl_bevil | last post: by
1 post views Thread by DaTurk | last post: by
4 posts views Thread by Engineerik | last post: by
11 posts views Thread by atlaste | last post: by
2 posts views Thread by Nicolas Le Gland | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.