473,671 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sockets usage

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
9 2451

"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD******** ********@TK2MSF TNGP11.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
If there is nothing in the receive buffer, the BeginReceive will trow an
exception.
"Tom Shelton" <to*@mtogden.co m> wrote in message
news:e2******** *****@TK2MSFTNG P09.phx.gbl...

"Gita George" <ge****@vital-heyl.com> wrote in message
news:uD******** ********@TK2MSF TNGP11.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.as p
This has several sections that explains the usage of both blocking and
non-blocking sockets.

Tom Shelton

Nov 20 '05 #3
Use a tcplistener and a timer to check for pending connections

Option Strict On
Option Explicit On

Imports System.Threadin g
Imports System.net
Imports System.Net.Sock ets
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.T imer

Sub Main()
AddHandler timer1.Elapsed, AddressOf Timer1_Tick
Dim strhost As String
strhost = Dns.GetHostName
Dim hostaddr As System.Net.IPAd dress =
Dns.Resolve(str host).AddressLi st(0)
oListener = New TcpListener(hos taddr, 60000)
oListener.Start ()
timer1.Enabled = True
Application.Run ()
End Sub

Private Sub Timer1_Tick(ByV al sender As Object, ByVal e As
System.timers.E lapsedEventArgs )
Dim oThreadStart As ThreadStart
Dim oThread As Thread
If Not oListener.Pendi ng() Then
oListener.Start ()
Exit Sub
End If
timer1.Enabled = False
oThreadStart = New ThreadStart(Add ressOf ProcessRequest)
oThread = New Thread(oThreadS tart) '
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.Threadin g.Thread.Curren tThread()
oSocket = oListener.Accep tSocket
While Not bStopListener
If oSocket.Availab le > 0 Then
bytes = oSocket.Receive (Buffer, Buffer.Length, 0)
SyncLock oThread

'do your thing

End SyncLock
Exit While
End If
If Not oSocket.Connect ed 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
for the tcpClient you can use:

dim Client as new tcpClient
While Not Client.GetStrea m.DataAvailable ()
Application.DoE vents()
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
In article <#i************ **@TK2MSFTNGP09 .phx.gbl>, Shawn D Shelton wrote:
for the tcpClient you can use:

dim Client as new tcpClient
While Not Client.GetStrea m.DataAvailable ()
Application.DoE vents()
End While


And drive cpu usage through the roof. Personally, I hate the TcpClient
class. I always end up using the System.Net.Sock ets.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
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******** ******@TK2MSFTN GP11.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
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******** ******@TK2MSFTN GP11.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.Pee k, 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
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******** ******@TK2MSFTN GP11.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
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.Sock ets
Module SendMail
Dim NetStrm As NetworkStream
Public Sub GenerateMail(By Val User As String)
Dim localName As String = System.Environm ent.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.Conn ect("smtpserver .foo.edu", 25)
NetStrm = SmtpClient.GetS tream()
reader = New StreamReader(Sm tpClient.GetStr eam())
buffer = reader.ReadLine ()
SendCommand("HE LO " & UserMail)
buffer = reader.ReadLine ()
SendCommand("MA IL FROM:" & UserMail)
buffer = reader.ReadLine
SendCommand("RC PT TO:" & UserMail)
buffer = reader.ReadLine
SendCommand("DA TA")
buffer = reader.ReadLine
SendCommand("Su bject: Some Subject")
SendCommand("FR OM:" & UserMail)
SendCommand("DA TE:" & TheDate)
SendCommand("TO :" & UserMail)
SendCommand("So me message text")
SendCommand(vbC rLf)
SendCommand(vbC rLf)
SendCommand("So me message text")
SendCommand("So me message text")
SendCommand(vbC rLf)
SendCommand("So me message text")
SendCommand("So me message text")
SendCommand(vbC rLf)
SendCommand("So me message text")
SendCommand(vbC rLf)
SendCommand(vbC rLf)
SendCommand(vbC rLf & ".")
buffer = reader.ReadLine
'MsgBox(buffer)
SendCommand("QU IT")
buffer = reader.ReadLine
'MsgBox(buffer)
NetStrm.Close()
reader.Close()
SmtpClient.Clos e()
buffer = Nothing
Catch ex As Exception
MsgBox(ex.Messa ge & vbCrLf & ex.Source)
End Try
End Sub

Private Sub SendCommand(ByV al text As String)
Dim szData() As Byte
text = text & vbCrLf
szData = System.Text.Enc oding.ASCII.Get Bytes(text.ToCh arArray)
NetStrm.Write(s zData, 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******** ******@TK2MSFTN GP11.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4813
by: Anatoly | last post by:
In our application I need to detemine if there is a internet connection valid. So I build a windows service which every minute creates telnet connection against some host and port. If I do connect to this host I know that internet connection is OK. The problem: after few days CPU usage growing up to 50% for this service. After restart it's problem gone for another 2,3 days. This is a code: public static bool IsBrowseable(string IP, int...
11
2200
by: Bonj | last post by:
I've been following a socket programming tutorial to make a simple TCP communication program, seemingly without hitches, it appears to work fine. However the structure of it is to have a server listening for requests from a client using listen(), and when one connects, it communicates with that client but only that one. It doesn't listen for more requests. What I'm wondering is can I have a server that continually listens for requests...
15
2481
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <signal.h> #include <unistd.h>
8
21909
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At that time, I get a SocketException with the message "Only one usage of each socket address (protocol/network address/port) is normally permitted". This happen if you use localhost or between two distinct computers. And, for a period of time after the...
10
1986
by: altaf.sunesara | last post by:
Hello ! I have some devices which communicate trough LAN to the server the old form i was using was FTP as a communication between Server and Client i have decided to use personal TCP sockets instead of using the FTP protocol. Well im familar with sockets but some concepts im not able to get it. I would be using Vb.Net as a server program but the question pampering my brain is for eg .. ip 192.168.0.0.1 tries to communicate on the...
2
3066
by: Cornald Kruyt | last post by:
Hi, I've an database import script written in PHP which used ADO via COM. It makes thousands of queries to a MS-SQL server. The problem is that the process runs out of sockets. The MSSQL server is used in the following way: // In the beginning of the script, the connection is set up $dbc=new COM("ADODB.Connection"); $dbc->ConnectionString="Provider=sqloledb;Network
3
11577
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.ReceiveFrom(Byte buffer, Int32 offset, Int32 s
5
2244
by: Dan Ritchie | last post by:
I've got a client/server app that I used to send large amounts of data via UDP to the client. We use it in various scenarios, one of which includes rendering a media file on the client as it is transferred via the underlying UDP transport. In this scenario it is very important to keep CPU usage as low as possible. Both the client and server were originally written in C++, but I've re-written the client in C#, partly to simplify it, but...
2
1438
by: Vlad Dogaru | last post by:
Hello, I am trying to write a simple program to teach myself sockets. The following bit of code fails with: "connect: Socket operation on non-socket". What am I missing? #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>
0
8919
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8599
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7439
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2813
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.