473,549 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vb networking problem

Ben
I modified the logmonitor sdk example so it would work over a network. It
works great when the client and server are running on the same PC and have
administrator privileges. So I have two problems:
1) I cannot run the client without administrator privileges. If I try I get
the following message:
Unhandled Exception: System.Security .SecurityExcept ion: Requested registry
access is not allowed.
at System.ThrowHel per.ThrowSecuri tyException(Exc eptionResource resource)
at Microsoft.Win32 .RegistryKey.Op enSubKey(String name, Boolean writable)
at System.Diagnost ics.EventLog.Ex ists(String logName, String machineName)
at LogMonitor_Clie nt.Microsoft.Sa mples.LogMonito r.Main()
The Zone of the assembly that failed was:
MyComputer
I have a try catch in the client so I don't know what the problem is. What
am I doing wrong?

2) My server crashes when a remote client disconnects. Here is its message:
Unhandled Exception: System.Net.Sock ets.SocketExcep tion: An existing
connection was forcibly closed by the remote host
at System.Net.Sock ets.Socket.Begi nReceive(Byte[] buffer, Int32 offset,
Int32size, SocketFlags socketFlags, syncCallback callback, Object state)
at LogMonitor_Serv er.Module1.Acce ptCallback(IAsy ncResult ar)
at System.Net.Lazy AsyncResult.Com plete(IntPtr userToken)
at System.Net.Cont extAwareResult. CaptureOrComple te(ExecutionCon text&
cachedContext, Boolean returnContext)
at System.Net.Cont extAwareResult. FinishPostingAs yncOp(CallbackC losure&
closure)
at System.Net.Sock ets.Socket.Begi nAccept(AsyncCa llback callback, Object
state)
at LogMonitor_Serv er.Module1.Main ()

I don't know why the server always crashes when a remote client leaves. If
I run the client and server on the same machine and have the client leave
the server stays running. What am I doing wrong?

Thanks,

Here is the client:
Public Class StateObject
Public workSocket As System.Net.Sock ets.Socket = Nothing
Public Const BufferSize As Integer = 256
Public buffer(BufferSi ze) As Byte
Public sb As New System.Text.Str ingBuilder
End Class

Namespace Microsoft.Sampl es
Module LogMonitor
Dim WithEvents evtApp As New EventLog("Appli cation")
Dim WithEvents evtSys As New EventLog("Syste m")
Dim WithEvents evtSec As New EventLog("Secur ity")

Private connectDone As New System.Threadin g.ManualResetEv ent(False)
Private sendDone As New System.Threadin g.ManualResetEv ent(False)
Private receiveDone As New System.Threadin g.ManualResetEv ent(False)
Private response As String = String.Empty

Public Sub Main()
If (Not EventLog.Exists ("system", ".")) Then
MsgBox("The sytem log does not exist!", , "Error")
Exit Sub
End If
If (Not EventLog.Exists ("applicatio n", ".")) Then
MsgBox("The application log does not exist!", , "Error")
Exit Sub
End If
If (Not EventLog.Exists ("security", ".")) Then
MsgBox("The security log does not exist!", , "Error")
Exit Sub
End If

evtApp.MachineN ame = System.Net.Dns. GetHostName
evtSys.MachineN ame = System.Net.Dns. GetHostName
evtSec.MachineN ame = System.Net.Dns. GetHostName
AddHandler evtApp.EntryWri tten, AddressOf OnEntryWritten
evtApp.EnableRa isingEvents = True
AddHandler evtSys.EntryWri tten, AddressOf OnEntryWritten
evtSys.EnableRa isingEvents = True
AddHandler evtSec.EntryWri tten, AddressOf OnEntryWritten
evtSec.EnableRa isingEvents = True

While (Console.Read() <> 113)
System.Threadin g.Thread.Sleep( 14500)
End While
End Sub

Sub OnEntryWritten( ByVal source As Object, ByVal e As EntryWrittenEve ntArgs)
Handles evtApp.EntryWri tten, evtSec.EntryWri tten, evtSys.EntryWri tten
Dim checkinfo As String
Dim checkwarn As String
Dim checkerror As String
Dim checksucc As String
Dim checkfail As String
Dim checkserver As String
Dim checkport As String
Try
checkinfo =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet", "info",
Nothing)
checkwarn =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet", "warn",
Nothing)
checkerror =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet", "error",
Nothing)
checksucc =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet", "succ",
Nothing)
checkfail =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet", "fail",
Nothing)
checkserver =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet",
"server", Nothing)
checkport =
My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\D ravet",
"portnum", Nothing)
If (checkinfo.ToUp per = "TRUE") And (e.Entry.EntryT ype =
EventLogEntryTy pe.Information) Then
sendinfo(e, checkserver, checkport)
Exit Sub
End If
If (checkwarn.ToUp per = "TRUE") And (e.Entry.EntryT ype =
EventLogEntryTy pe.Warning) Then
sendinfo(e, checkserver, checkport)
Exit Sub
End If
If (checkerror.ToU pper = "TRUE") And (e.Entry.EntryT ype =
EventLogEntryTy pe.Error) Then
sendinfo(e, checkserver, checkport)
Exit Sub
End If
If (checksucc.ToUp per = "TRUE") And (e.Entry.EntryT ype =
EventLogEntryTy pe.SuccessAudit ) Then
sendinfo(e, checkserver, checkport)
Exit Sub
End If
If (checkfail.ToUp per = "TRUE") And (e.Entry.EntryT ype =
EventLogEntryTy pe.FailureAudit ) Then
sendinfo(e, checkserver, checkport)
Exit Sub
End If

Catch se As System.Security .SecurityExcept ion
Catch se2 As System.Unauthor izedAccessExcep tion
MsgBox("A Security Exception has occurred", , "LogMonitor Error")
End Try
End Sub

Sub sendinfo(ByVal e As EntryWrittenEve ntArgs, ByVal servername As String,
ByVal portnum As String)
Dim sendbuffer As String
Dim ipHostInfo As System.Net.IPHo stEntry =
System.Net.Dns. GetHostEntry(se rvername)
Dim ipAddress As System.Net.IPAd dress = ipHostInfo.Addr essList(0)
Dim remoteEP As New System.Net.IPEn dPoint(ipAddres s, portnum)
Dim client As New
System.Net.Sock ets.Socket(Syst em.Net.Sockets. AddressFamily.I nterNetwork,
System.Net.Sock ets.SocketType. Stream, System.Net.Sock ets.ProtocolTyp e.Tcp)
Dim hostname As String = System.Net.Dns. GetHostName()
client.BeginCon nect(remoteEP, New AsyncCallback(A ddressOf ConnectCallback ),
client)
connectDone.Wai tOne()
sendbuffer = hostname & "<BREAK>" & e.Entry.Message & "<EOF>"
Send(client, sendbuffer)
sendDone.WaitOn e()
client.Shutdown (System.Net.Soc kets.SocketShut down.Both)
client.Close()
End Sub

Private Sub ConnectCallback (ByVal ar As IAsyncResult)
Dim client As System.Net.Sock ets.Socket = CType(ar.AsyncS tate,
System.Net.Sock ets.Socket)
client.EndConne ct(ar)
connectDone.Set ()
End Sub

Private Sub Receive(ByVal client As System.Net.Sock ets.Socket)
Dim state As New StateObject
state.workSocke t = client
client.BeginRec eive(state.buff er, 0, StateObject.Buf ferSize, 0, New
AsyncCallback(A ddressOf ReceiveCallback ), state)
End Sub

Private Sub ReceiveCallback (ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncS tate, StateObject)
Dim client As System.Net.Sock ets.Socket = state.workSocke t
Dim bytesRead As Integer = client.EndRecei ve(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append (System.Text.En coding.ASCII.Ge tString(state.b uffer, 0,
bytesRead))
' Get the rest of the data.
client.BeginRec eive(state.buff er, 0, StateObject.Buf ferSize, 0, New
AsyncCallback(A ddressOf ReceiveCallback ), state)
Else
' All the data has arrived; put it in response.
If state.sb.Length > 1 Then
response = state.sb.ToStri ng()
End If
receiveDone.Set ()
End If
End Sub

Private Sub Send(ByVal client As System.Net.Sock ets.Socket, ByVal data As
String)
Dim byteData As Byte() = System.Text.Enc oding.ASCII.Get Bytes(data)
client.BeginSen d(byteData, 0, byteData.Length , 0, New
AsyncCallback(A ddressOf SendCallback), client)
End Sub

Private Sub SendCallback(By Val ar As IAsyncResult)
Dim client As System.Net.Sock ets.Socket = CType(ar.AsyncS tate,
System.Net.Sock ets.Socket)
Dim bytesSent As Integer = client.EndSend( ar)
sendDone.Set()
End Sub
End Module
End Namespace

SERVER ----------------------------------------------------------------------------
Public Class StateObject
Public workSocket As System.Net.Sock ets.Socket = Nothing
Public Const BufferSize As Integer = 1024
Public buffer(BufferSi ze) As Byte
Public sb As New System.Text.Str ingBuilder
End Class

Module Module1
Dim portnum As String
Dim SBuffer(4096) As Byte
Dim SBytes As Integer
Public allDone As New System.Threadin g.ManualResetEv ent(False)

Sub Main()
Dim args As String()
args = Environment.Get CommandLineArgs ()
portnum = args(1)
If Not IsNumeric(portn um) Then
Console.WriteLi ne("Usage: LogMonitor.exe portnumber")
Console.WriteLi ne()
Console.WriteLi ne("Press Enter to continue...")
Console.ReadLin e()
Exit Sub
End If
If (args.Length <> 2) Then
Console.WriteLi ne("Usage: LogMonitor.exe portnumber")
Console.WriteLi ne()
Console.WriteLi ne("Press Enter to continue...")
Console.ReadLin e()
Exit Sub
End If

Dim theIPEndPoint As System.Net.IPEn dPoint = New
System.Net.IPEn dPoint(System.N et.IPAddress.An y, portnum)
Dim ServerSocket As System.Net.Sock ets.Socket = New
System.Net.Sock ets.Socket(Net. Sockets.Address Family.InterNet work,
Net.Sockets.Soc ketType.Stream, Net.Sockets.Pro tocolType.Tcp)
ServerSocket.Bl ocking = True
ServerSocket.Bi nd(theIPEndPoin t)
ServerSocket.Li sten(10)
While True
allDone.Reset()
Console.WriteLi ne("Waiting for a connection...")
ServerSocket.Be ginAccept(New AsyncCallback(A ddressOf
AcceptCallback) , ServerSocket)
allDone.WaitOne ()
End While
End Sub

Public Sub AcceptCallback( ByVal ar As IAsyncResult)
Dim listener As System.Net.Sock ets.Socket = CType(ar.AsyncS tate,
System.Net.Sock ets.Socket)
Dim handler As System.Net.Sock ets.Socket = listener.EndAcc ept(ar)
Dim state As New StateObject
state.workSocke t = handler
handler.BeginRe ceive(state.buf fer, 0, StateObject.Buf ferSize, 0, New
AsyncCallback(A ddressOf ReadCallback), state)
End Sub

Public Sub ReadCallback(By Val ar As IAsyncResult)
Dim content As String = String.Empty
Dim state As StateObject = CType(ar.AsyncS tate, StateObject)
Dim handler As System.Net.Sock ets.Socket = state.workSocke t
Dim bytesRead As Integer = handler.EndRece ive(ar)
Dim dotposition As Integer
Dim buffer As String
Dim hostname As String
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append (System.Text.En coding.ASCII.Ge tString(state.b uffer,
0, bytesRead))
' Check for end-of-file tag. If it is not there, read more data.
content = state.sb.ToStri ng()
If content.IndexOf ("<EOF>") > -1 Then
' All the data has been read from the client. Display it on the
console.
dotposition = InStr(content, "<BREAK>") - 1
hostname = Left(content, dotposition)
buffer = Replace(content , "<EOF>", "")
buffer = Right(buffer, buffer.Length - hostname.Length - 7)
MsgBox(buffer, , "Eventlog Alert from " & hostname)
' Echo the data back to the client.
Send(handler, content)
Else
' Not all data received. Get more.
handler.BeginRe ceive(state.buf fer, 0, StateObject.Buf ferSize, 0,
New AsyncCallback(A ddressOf ReadCallback), state)
End If
End If
End Sub

Private Sub Send(ByVal handler As System.Net.Sock ets.Socket, ByVal data As
String)
Dim byteData As Byte() = System.Text.Enc oding.ASCII.Get Bytes(data)
handler.BeginSe nd(byteData, 0, byteData.Length , 0, New
AsyncCallback(A ddressOf SendCallback), handler)
End Sub

Private Sub SendCallback(By Val ar As IAsyncResult)
Dim handler As System.Net.Sock ets.Socket = CType(ar.AsyncS tate,
System.Net.Sock ets.Socket)
Dim bytesSent As Integer = handler.EndSend (ar)
Console.WriteLi ne("Sent {0} bytes to client.", bytesSent)
handler.Shutdow n(System.Net.So ckets.SocketShu tdown.Both)
handler.Close()
allDone.Set()
End Sub
End Module
Jun 5 '06 #1
0 1297

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1667
by: hyperbob | last post by:
Hello, I'm in the design stage of an application that will consist of two or more processes that will be either on different computers in a network or on the same computer and will have to communicate with each other. I don't have much experience with networking, inter-process communication or client-server architectures, and I'm wondering what...
6
2822
by: James Egan | last post by:
I've seen some networking toolkits for C++, but was wondering if there were (or plans for) a standard set of networking classes for C++? Also, can someone tell me where I can find a comprehensive class reference? -Thanks
6
1835
by: Gary | last post by:
I have recently placed an access database (back end) on a network folder. The standalone pc's accessing the network have in their own respective C drives a copy of the database (minus the table). I have linked the backend source table to these front end database copies. The networking has been working nicely but the other day, I got a message...
7
8913
by: Brian Keogh | last post by:
Hi, I'm a student learning TCP/IP networking at University. Although I understand all about TCP/IP Networking in Java I am expected to understand the basics of C with regard to these programs as all the examples in my course notes and Exams refer to C. Can someone please point me in the right direction to where I might find answers on the...
46
2064
by: H.A. Sujith | last post by:
Why doesn't the standard library provide (at least basic) networking facilities using TCP/IP ?
4
1748
by: Raj | last post by:
I want to program C with networking. Can anyone tell me the How to start with a small simple program can it possible in turbo/borland c 3+. or i should go for another verson. i am using windows 98/2000/xp
1
7152
by: NickP | last post by:
Hi there, I've been experiencing a strange issue for a short while now and have managed to make a tiny replication of the bug. The uploaded a VB.NET 2005 project which implements a single instance application, it's very basic. http://nickpateman.m6.net/Files/xpbug.zip
1
1891
by: chinaemerem ibeawuchi | last post by:
What is the difference between URL networking and Socket networking? with an example of each type and state why the choosen networking technique is most appropriate for the example.
4
1982
prometheuzz
by: prometheuzz | last post by:
Hello all, There's this problem I have, which I am unable to find a solution for. The problem is this: I cannot post messages with a specific size on (certain) forums. I hear you thinking: what has this to do with networking? Before I get burned to the stake, please hear me out. I have recently plugged a wireless router on my (wireless)...
5
2241
by: Nefrit | last post by:
Ok, I can't figure it out, please someone else help if you know what to do. I have a Linksys Wireless G Router and Wireless G USB network adapter. My host computer is using Windows XP and there is no problems. My downstairs computer, which I'm trying to connect to the internet, is running on Windows ME. My problem is, the client computer...
0
7471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
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. ...
0
7985
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...
1
7503
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...
1
5387
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3517
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...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
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
1
1082
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.