By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

sockets usage

P: n/a
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events
!?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD****************@TK2MSFTNGP11.phx.gbl...
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events
!?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...


George,

You'll want to look at the Socket Classes BeginReceive method. You'll also want
to look here for information on socket programming in .NET:

http://msdn.microsoft.com/library/de...consockets.asp

This has several sections that explains the usage of both blocking and
non-blocking sockets.

Tom Shelton
Nov 20 '05 #2

P: n/a
If there is nothing in the receive buffer, the BeginReceive will trow an
exception.
"Tom Shelton" <to*@mtogden.com> wrote in message
news:e2*************@TK2MSFTNGP09.phx.gbl...

"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD****************@TK2MSFTNGP11.phx.gbl...
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events !?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...
George,

You'll want to look at the Socket Classes BeginReceive method. You'll

also want to look here for information on socket programming in .NET:

http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconsockets.asp
This has several sections that explains the usage of both blocking and
non-blocking sockets.

Tom Shelton

Nov 20 '05 #3

P: n/a
Use a tcplistener and a timer to check for pending connections

Option Strict On
Option Explicit On

Imports System.Threading
Imports System.net
Imports System.Net.Sockets
Imports System.Windows.forms

Module Module1

Private oListener As TcpListener '
Private bStopListener As Boolean
Private ActiveThreads As Integer
Private ThreadIndex As Integer
Private timer1 As New System.Timers.Timer

Sub Main()
AddHandler timer1.Elapsed, AddressOf Timer1_Tick
Dim strhost As String
strhost = Dns.GetHostName
Dim hostaddr As System.Net.IPAddress =
Dns.Resolve(strhost).AddressList(0)
oListener = New TcpListener(hostaddr, 60000)
oListener.Start()
timer1.Enabled = True
Application.Run()
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.timers.ElapsedEventArgs)
Dim oThreadStart As ThreadStart
Dim oThread As Thread
If Not oListener.Pending() Then
oListener.Start()
Exit Sub
End If
timer1.Enabled = False
oThreadStart = New ThreadStart(AddressOf ProcessRequest)
oThread = New Thread(oThreadStart) '
oThread.Start()
timer1.Enabled = True
End Sub

Private Sub ProcessRequest()
Dim oThread As Thread
Dim oSocket As Socket
Dim Buffer(20) As Byte
Dim bytes As Integer
oThread = System.Threading.Thread.CurrentThread()
oSocket = oListener.AcceptSocket
While Not bStopListener
If oSocket.Available > 0 Then
bytes = oSocket.Receive(Buffer, Buffer.Length, 0)
SyncLock oThread

'do your thing

End SyncLock
Exit While
End If
If Not oSocket.Connected Then
bStopListener = True
End If
End While
End Sub
End Module

Gita George wrote:
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events
!?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...


Nov 20 '05 #4

P: n/a
for the tcpClient you can use:

dim Client as new tcpClient
While Not Client.GetStream.DataAvailable()
Application.DoEvents()
End While
Gita George wrote:
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events
!?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...


Nov 20 '05 #5

P: n/a
In article <#i**************@TK2MSFTNGP09.phx.gbl>, Shawn D Shelton wrote:
for the tcpClient you can use:

dim Client as new tcpClient
While Not Client.GetStream.DataAvailable()
Application.DoEvents()
End While


And drive cpu usage through the roof. Personally, I hate the TcpClient
class. I always end up using the System.Net.Sockets.Socket class. You have
a lot more control.

Using the Async socket model is probably a much better method.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #6

P: n/a
How can I use the Async socket model?
Is there any way to simulate the DataArrival event?


"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events !?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...

Nov 20 '05 #7

P: n/a
On 2003-11-05, Gita George <ge****@vital-heyl.com> wrote:
How can I use the Async socket model?
Is there any way to simulate the DataArrival event?


"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any

events
!?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...



When you call BeginReceive, you can pass an call back function that will
be called when data arives on the socket. Look at the examples in the
docs, the ones pointed to by the links I posted. If you have a simple task,
I can write a little demonstration application...

You can also do this using blocking operations. One way I've done this
in the past using the Winsock API (Sorry, I've used the winsock control
exactly once, and it caused me so much grief, I've always done my socket
stuff using custom classes wrapped around the winsock api) was basically
to use the select function to notify me if there was a change in the
read state. When there was a change, I would then use recv with the
MSG_PEEK flag. I could then determine if the change was triggered
because the remote endpoint was shutdown (recv returns 0), or if their
was data to be read off the socket. Based, on that I would fire an
event. You could do a similar thing in .NET - all of those types of
operations are supported (Socket.Select, SocketFlags.Peek, etc.). It
really depends on the task.

Maybe we should start over, and have you explain more about what your
trying to accomplish - maybe there is an easier or more appropriate
approach for your task. I may have misunderstood your needs. I would
like to say, that though the socket programming model in .NET is a bit
different then it was under VB.CLASSIC (well, at least if you used the
built in winsock control), it is really a much more powerful and
flexable environment. But, sometimes it maybe a little bit of over kill
:)

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #8

P: n/a
I'm trying to write an application that connects to a mail server on port
110 and uses POP3 commands to check if I have any new mail.
It's that simple.
That same application on VB 6 was focused on the DataArrival event of
Winsock control.
I was sending commands and when I received the results I would analyze that.

Thanks.

"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any events !?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...

Nov 20 '05 #9

P: n/a
Hi,

I have a program that sends mail to an smtp server using a network
stream and smtp commands. If you just want a socket connection to a
server and know what commands to send then this could probably be
modified for your needs.

Option Explicit On
Option Strict On
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Module SendMail
Dim NetStrm As NetworkStream
Public Sub GenerateMail(ByVal User As String)
Dim localName As String = System.Environment.MachineName
Dim UserMail As String = User & "@foo.edu"
Dim TheDate As String = CType(Now(), String)
Try
Dim reader As StreamReader
Dim buffer As String
Dim SmtpClient As New TcpClient
SmtpClient.Connect("smtpserver.foo.edu", 25)
NetStrm = SmtpClient.GetStream()
reader = New StreamReader(SmtpClient.GetStream())
buffer = reader.ReadLine()
SendCommand("HELO " & UserMail)
buffer = reader.ReadLine()
SendCommand("MAIL FROM:" & UserMail)
buffer = reader.ReadLine
SendCommand("RCPT TO:" & UserMail)
buffer = reader.ReadLine
SendCommand("DATA")
buffer = reader.ReadLine
SendCommand("Subject: Some Subject")
SendCommand("FROM:" & UserMail)
SendCommand("DATE:" & TheDate)
SendCommand("TO:" & UserMail)
SendCommand("Some message text")
SendCommand(vbCrLf)
SendCommand(vbCrLf)
SendCommand("Some message text")
SendCommand("Some message text")
SendCommand(vbCrLf)
SendCommand("Some message text")
SendCommand("Some message text")
SendCommand(vbCrLf)
SendCommand("Some message text")
SendCommand(vbCrLf)
SendCommand(vbCrLf)
SendCommand(vbCrLf & ".")
buffer = reader.ReadLine
'MsgBox(buffer)
SendCommand("QUIT")
buffer = reader.ReadLine
'MsgBox(buffer)
NetStrm.Close()
reader.Close()
SmtpClient.Close()
buffer = Nothing
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.Source)
End Try
End Sub

Private Sub SendCommand(ByVal text As String)
Dim szData() As Byte
text = text & vbCrLf
szData = System.Text.Encoding.ASCII.GetBytes(text.ToCharArr ay)
NetStrm.Write(szData, 0, szData.Length)
End Sub
End Module

Gita George wrote:
I'm trying to write an application that connects to a mail server on port
110 and uses POP3 commands to check if I have any new mail.
It's that simple.
That same application on VB 6 was focused on the DataArrival event of
Winsock control.
I was sending commands and when I received the results I would analyze that.

Thanks.

"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
I'm trying to write a program (a pop3 mail checker) and I'm having a
problem.
I'm using the socket control and I've noticed that I do not have any


events
!?
How do I use the socket to receive data?
How do I simulate the DataArrival event from Winsock (VB 6.0)?
How can I create and alternate event?

This also is the case for the tcpClient control.
If you have any solution please tell me if they can be applied to the
tcpClient control.

Any input would be apreciated

Thanks in advance ...



Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.