Link to home
Start Free TrialLog in
Avatar of JDipper
JDipper

asked on

Sending a message from a server to a client

Hi All!

I'm currently on a network, with my computer sharing the internet connection through a software proxy. However, I want to be able to send users on the network an administrative notice saying the internet will be turned off, before I actually turn off the computer.

I don't want to use the netsend command, but instead us tcpip or udp or something along those lines. My idea was to have a server program which could send a message to a client program, which would be running on all of the machines. The client would run quietly in the background, and once it receives the message, pop-up an message box.

The problem I'm having is the sending and receiving part of each program. This is fairly urgent so I'm giving this question 500 points.

Many Thanks

JDipper
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

>> The problem I'm having is the sending and receiving part of each program.

Well...what do you have so far?!

Are you using the Winsock Control?  Winsock API's? Third Part Socket Control?

Throw us a bone here...

Idle_Mind
ASKER CERTIFIED SOLUTION
Avatar of mmusante
mmusante

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this

Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_EXECUTE = &H20000000
Private Const GENERIC_ALL = &H10000000
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80

Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFileName As Long, ByVal lpBuff As Any, ByVal nNrBytesToWrite As Long, lpNrOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwAccess As Long, ByVal dwShare As Long, ByVal lpSecurityAttrib As Long, ByVal dwCreationDisp As Long, ByVal dwAttributes As Long, ByVal hTemplateFile As Long) As Long
           
Function SendToWinPopUp(PopFrom As String, PopTo As String, MsgText As String) As Long

    Dim rc As Long
    Dim mshandle As Long
    Dim msgtxt As String
    Dim byteswritten As Long
    Dim mailslotname As String
    ' name of the mailslot
    mailslotname = "\\" + PopTo + "\mailslot\messngr"
    msgtxt = PopFrom + Chr(0) + PopTo + Chr(0) + MsgText + Chr(0)
    mshandle = CreateFile(mailslotname, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
    rc = WriteFile(mshandle, msgtxt, Len(msgtxt), byteswritten, 0)
    rc = CloseHandle(mshandle)
End Function

Private Sub Command1_Click()

    SendToWinPopUp "name1", "name2", "3333"
End Sub

Avatar of AndyAmess
AndyAmess


A simple way is to have a  text file on the server that holds the message you want to send.

You don't even need anything server side if you use a text file on the server that the clients can see.

On the client side you would have a timer that checked for the existence of the text file. If it exists then display the message. Put a message number on the first line and the message on the second line. This way the client will only display the message once unless the number changes.

private sub Timer1_Timer()
  Dim lsMessageFile as string
  Dim lsTextIn as string
  dim lsMessageNumDisplayed as string
  lsMessageFile = "Z:\Messages\Message.txt"

  if dir(lsMessageLocation)<>"" then
      Open lsMessageFile For Input As InFile
      if Not EOF(InFile)
          Line Input #InFile, lsTextIn
          if lsTextIn  <> lsMessageNumDisplayed
               lsMessageNumDisplayed=lstextin
                Line Input #InFile, lsTextIn
                MsgBox lsTextIn
          end if
      end if
     Close InFile
  end if
end sub

NB: Not tested this code but quietly confident..
A. ;-)
Avatar of JDipper

ASKER

OK, I'm using the winsock control here with the following code for the server application:

Private Sub Form_Load()
With Winsock1
    .LocalPort = 1701
    .Listen
End With
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
With Winsock1
    .Close
    .Accept requestID
End With
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim TempData As String ' Can be any name
    'Recieve the data
    Winsock1.GetData TempData
    'Display the data in a message box
    MsgBox TempData
End Sub

However, when I try and combine it together into one command as shown:

Private Sub Send_Click()
With Winsock1
Dim data As String
data = "Testing 123"
    .RemoteHost = "127.0.0.1"
    .RemotePort = 1701
    .Connect
    .SendData (data)
End With
End Sub

I get the following error message:

Run-time error '40006':

Wrong protocol or connection state for the requested transaction or request.

Any ideas?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First of all your server needs to have its winsock control in a control array so that it can handle more than one connection.

Secondly, your server should not be connecting to the clients and sending messages.  The server should only listen, accept incoming connections, and send your message to whomever is connected.

It looks like you are quite new to winsock programming...

Do you need a step by step complete run down on how to create both the server and the client?

Idle_Mind
I'm not sure which solution best suits your purpose, but there is another possibility... The server computer could run an ActiveX daemon, and the clients could each communicate with it over DCOM... This automatically uses whichever communications protocol is best ("best" according to MS) and available, and will let you respond to events.

If you need help with ActiveX servers, check out:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconcreatingoleserver.asp
The methods described there will delete the file the next time the computer boots up... If that's acceptable, I believe you can also add the command to the RunOnce key in the registry:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce\
For the item name, put anything you want... For the value, I THINK you can just put the delete command ("del C:\wherever\try.exe").
Ack... Sorry, responded to the wrong question... Ignore that last post :)
Avatar of JDipper

ASKER

Ok, sorted.

Many thanks to mmusante, and also xxx x xxx

Split points, 400/100 respectivly