473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Data receive through socket

shweta123
692 Expert 512MB
Hey,
Happy New Year to all of you!

I have one query,
I am trying to send and receive the data through sockets.
When I send it for the first time I get the data from the server.But from second time onwards I dont receive anything. Any idea?

Shweta
Jan 2 '07 #1
4 2810
kenobewan
4,871 Expert 4TB
Here is an article that may help:
VB.NET TCP Client - Server Socket Communications

Otherwise you may want to post code. Any error message?
Jan 3 '07 #2
Hi Shweta,

I am not so familiar with the Socket programming.
But i have found follwing link which might help you.

http://www.codeproject.com/cs/internet/mailclient.asp

Best Regards,
Vivek Rathore.
Jan 3 '07 #3
shweta123
692 Expert 512MB
Thanks for the help!
I am stuck up at some other place.These 3 functions I am using for sending and receiving the data.

Private Sub sockDataArrival(ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncState, StateObject)
'Dim state As New StateObject
Dim client As Socket = state.workSocket
Dim bytesRead As Integer

Try
bytesRead = sockPOP3.EndReceive(ar)
Catch
Exit Sub
End Try

Try
Dim Data() As Byte = state.buffer
strmsg = Encoding.ASCII.GetString(Data)

If bytesRead = 0 Then
client.Shutdown(SocketShutdown.Both)
client.Close()
RaiseEvent onDisconnect()
Exit Sub
End If

ReDim state.buffer(32767)
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
RaiseEvent onDataArrival(Data, bytesRead)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub

--------------------------------------------------------------------------

Public Sub SendData(ByVal Data() As Byte)
Try
Dim byteData As Byte() = Data
sockPOP3.BeginSend(byteData, 0, byteData.Length, 0, AddressOf sockSendEnd, sockPOP3)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub

------------------------------------------------------------------------------------------
Private Sub sockSendEnd(ByVal ar As IAsyncResult)
Try
Dim sockPOP3 As Socket = CType(ar.AsyncState, Socket)
Dim bytesSent As Integer = sockPOP3.EndSend(ar)
' Dim state As New StateObject
' state.workSocket = sockPOP3
' sockPOP3.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
RaiseEvent onSendComplete(bytesSent)
Dim state As New StateObject
state.workSocket = sockPOP3
sockPOP3.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
'Call sockDataArrival(state.buffer)
RaiseEvent onDataArrival(state.buffer, state.BufferSize)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub


My Problem is that when I send data for the first time I receive it properly.But next time onwards It does not hit sockDataArrival function at all and I dont get any error also.
Any Idea?

Shweta
Jan 3 '07 #4
I think the problem lies here:

You are declaring Dim client as socket in your first function i.e sockDataArrival.
But in same function rather than using client as socket you are using sockPOP3.
else in other function you are consistent with sockPOP3 declared as socket.

Vivek Rathore.


Thanks for the help!
I am stuck up at some other place.These 3 functions I am using for sending and receiving the data.

Private Sub sockDataArrival(ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncState, StateObject)
'Dim state As New StateObject
Dim client As Socket = state.workSocket
Dim bytesRead As Integer

Try
bytesRead = sockPOP3.EndReceive(ar)
Catch
Exit Sub
End Try

Try
Dim Data() As Byte = state.buffer
strmsg = Encoding.ASCII.GetString(Data)

If bytesRead = 0 Then
client.Shutdown(SocketShutdown.Both)
client.Close()
RaiseEvent onDisconnect()
Exit Sub
End If

ReDim state.buffer(32767)
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
RaiseEvent onDataArrival(Data, bytesRead)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub

--------------------------------------------------------------------------

Public Sub SendData(ByVal Data() As Byte)
Try
Dim byteData As Byte() = Data
sockPOP3.BeginSend(byteData, 0, byteData.Length, 0, AddressOf sockSendEnd, sockPOP3)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub

------------------------------------------------------------------------------------------
Private Sub sockSendEnd(ByVal ar As IAsyncResult)
Try
Dim sockPOP3 As Socket = CType(ar.AsyncState, Socket)
Dim bytesSent As Integer = sockPOP3.EndSend(ar)
' Dim state As New StateObject
' state.workSocket = sockPOP3
' sockPOP3.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
RaiseEvent onSendComplete(bytesSent)
Dim state As New StateObject
state.workSocket = sockPOP3
sockPOP3.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
'Call sockDataArrival(state.buffer)
RaiseEvent onDataArrival(state.buffer, state.BufferSize)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub


My Problem is that when I send data for the first time I receive it properly.But next time onwards It does not hit sockDataArrival function at all and I dont get any error also.
Any Idea?

Shweta
Jan 3 '07 #5

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

Similar topics

5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
5
by: Sharon | last post by:
I have encountered with a most disturbing TCP problem: I have cases (too many of them) that result in data loss. Some inforamation on my test configuration: I have to PC's which are the same...
7
by: Crirus | last post by:
Hi all! I use a webClient for requesting data from a server of mine. Should I worry about long ammount of data sent by server in the client side? Or, another way, should I send some kind of a...
7
by: Nuno Magalhaes | last post by:
I've got a problem which relates to reading HTTP data. I've got the socket connected to a web site and then I send "GET / HTTP/1.1\n\n" and the page is received after a while but not all of the...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
16
by: s0suk3 | last post by:
I wanted to ask for standard ways to receive data from a socket stream (with socket.socket.recv()). It's simple when you know the amount of data that you're going to receive, or when you'll receive...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.