Click here to Skip to main content
15,917,568 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: system.thread.sleep Pin
Colin Angus Mackay28-Jan-08 5:58
Colin Angus Mackay28-Jan-08 5:58 
GeneralRe: system.thread.sleep Pin
solarthur0128-Jan-08 6:29
solarthur0128-Jan-08 6:29 
GeneralFormating Excel sheet by VB.Net Pin
Pradip Kishore28-Jan-08 1:52
Pradip Kishore28-Jan-08 1:52 
GeneralRe: Formating Excel sheet by VB.Net Pin
Justin Perez28-Jan-08 3:16
Justin Perez28-Jan-08 3:16 
QuestionVBA for Outlook Question Pin
wjtaylor28-Jan-08 1:31
wjtaylor28-Jan-08 1:31 
Questionhow to control the left button of the mouse Pin
saadmechiche28-Jan-08 1:05
saadmechiche28-Jan-08 1:05 
AnswerRe: how to control the left button of the mouse Pin
Johan Hakkesteegt28-Jan-08 6:26
Johan Hakkesteegt28-Jan-08 6:26 
QuestionAsynchronous socket problem? Pin
Benny_Lava28-Jan-08 0:19
Benny_Lava28-Jan-08 0:19 
Hi,

I'm building a data exchange app using sockets, using asynchronous sockets one app listens for incoming connections, and the other one connects to the first and sends some data to the other.

The code for the listener app:

Private Sub startListening()
Dim iphostinfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim localEP As New IPEndPoint(iphostinfo.AddressList(0), 8000)

listener = New Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)

Try
listener.Bind(localEP)
listener.Listen(5)

listener.BeginAccept(New AsyncCallback(AddressOf acceptCallback), listener) 'Pocni asinkrono primat nadolazece podatke

While True
If abort = True Then
cleanUp()
Application.Exit()
End If
Application.DoEvents()
End While

Catch ex As Exception

End Try
End Sub

Private Sub acceptCallback(ByVal ar As IAsyncResult)
Try
listener.EndAccept(ar)
RaiseEvent status("Konekcija uspostavljena")
Application.DoEvents()
timeout.Stop()
timeout.Enabled = False
'Velicina Buffera
Dim bytes(1024) As Byte

listener.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf receiveCallback, bytes) ' HERE IT CRASHES : the error is written bellow
Catch ex As Exception
RaiseEvent status(ex.Message)
Application.DoEvents()
End Try
End Sub

Private Sub receiveCallback(ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numBytes As Int32 = listener.EndReceive(ar)

If numBytes = 0 Then 'Ako nema više podataka onda iskljuci socket i spremi dobiveni file
listener.Shutdown(SocketShutdown.Both)
listener.Close()
spremiFile(data)
Else
If prviPodatak = False Then
ReDim Preserve data(data.Length + 1024)
prviPodatak = False
End If
prviPodatak = False
Array.Copy(bytes, 0, data, data.Length, bytes.Length)
'--Ocisti buffer
Array.Clear(bytes, 0, bytes.Length)

'-- Opet pocni primat podatke
listener.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf receiveCallback, bytes)
End If
End Sub


You will see a comment in the code where it crashes. The apps conenct to each other but when it's time to beginReceive (marked in the code) it crashes with the error:

"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.

Any clues?


The server part (the part that sends data)

Private Function salji(ByVal port As Integer, ByVal data() As Byte, ByVal ipAdresa As String) As Boolean
Dim addr As IPAddress = Dns.GetHostEntry(ipAdresa).AddressList(0)

If Not addr Is Nothing Then
Dim ep As New IPEndPoint(addr, port)

cSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
cSocket.BeginConnect(ep, AddressOf connectCallback, Nothing)
End If

Do
If spoj = stateConn.nijeSpojen Then

ElseIf spoj = stateConn.greska Then
Return False
Else
cSocket.Send(data)
Return True
End If
Loop
End Function

Private Sub connectCallback(ByVal ar As IAsyncResult)
Try
cSocket.EndConnect(ar) ' It passes this part and exits the subs like everything is OK . There is no error on this part only on the listener part (of course, when it tries to send data it can't bacause there is an error in the listening app)
spoj = stateConn.spojen
Catch ex As Exception
spoj = stateConn.greska
End Try
End Sub


Before this I tried Synchronous method. And it works. But for many reasons It must be asynchronous.

Any help greatly appreciated,
Thank you!
GeneralImages in My.settings Pin
plural27-Jan-08 23:54
plural27-Jan-08 23:54 
QuestionRe: Images in My.settings Pin
newc128-Jan-08 1:34
newc128-Jan-08 1:34 
GeneralRe: Images in My.settings Pin
plural28-Jan-08 4:22
plural28-Jan-08 4:22 
QuestionRe: Images in My.settings Pin
Steven J Jowett28-Jan-08 5:13
Steven J Jowett28-Jan-08 5:13 
Generalrepeat columns in grid view Pin
hepsy.i27-Jan-08 22:38
hepsy.i27-Jan-08 22:38 
Generalwanna check folder exist or not Pin
phoopwint27-Jan-08 20:56
phoopwint27-Jan-08 20:56 
GeneralRe: wanna check folder exist or not Pin
The ANZAC27-Jan-08 21:37
The ANZAC27-Jan-08 21:37 
QuestionHow to Change back color in selected row in that data grid view table [modified] Pin
Francis K Antony27-Jan-08 20:14
Francis K Antony27-Jan-08 20:14 
GeneralDate Conversion Problem Pin
danasegaranea27-Jan-08 19:59
danasegaranea27-Jan-08 19:59 
GeneralRe: Date Conversion Problem Pin
VB Prog29-Jan-08 10:47
VB Prog29-Jan-08 10:47 
Generalprohibit virtual printer to create pdf document Pin
eyes200727-Jan-08 19:56
eyes200727-Jan-08 19:56 
GeneralRe: prohibit virtual printer to create pdf document Pin
Johan Hakkesteegt2-Feb-08 10:34
Johan Hakkesteegt2-Feb-08 10:34 
QuestionHow to raise a event to another control in the same form Pin
learnVB27-Jan-08 17:13
learnVB27-Jan-08 17:13 
AnswerRe: How to raise a event to another control in the same form Pin
CKnig27-Jan-08 18:16
CKnig27-Jan-08 18:16 
GeneralRe: How to raise a event to another control in the same form Pin
learnVB28-Jan-08 3:58
learnVB28-Jan-08 3:58 
GeneralVB.NET / ADO question Pin
Tom Wright27-Jan-08 15:40
Tom Wright27-Jan-08 15:40 
GeneralRe: VB.NET / ADO question Pin
VB Prog29-Jan-08 10:51
VB Prog29-Jan-08 10:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.