PDA

Click to See Complete Forum and Search --> : Send messages with the winsock control


Jakys
Dec 16th, 1999, 04:15 PM
I'd like to send messages over a network(LAN) with the winsock control, but i can't figure out how to do that. If any of you have som samples, or can help with code, it would be great.

smalig
Dec 16th, 1999, 04:41 PM
I use NT and the NET SEND command:

For sample:

Target$ = username
OR
Target$ = computername
Message$ = "This server will shut down in 5 minutes."
Shell ("net send " & Target & " " & Message)

You can send a message only to a name that is active on the network. If the message is sent to a username, that user must be logged on and running the Messenger service to receive the message.
Look help about NET SEND.

------------------
smalig
smalig@hotmail.com
http://vbcode.webhostme.com/

Jakys
Dec 17th, 1999, 04:38 AM
I got Windows 98 Service Pack 2, do I have the NetSend command here?

Clunietp
Dec 18th, 1999, 12:10 PM
There is a file NET.EXE and SEND is one of it's parameters. You can also use the NetMessageBufferSend API of NetAPI32 library, I don't have the code handy though....

smalig
Dec 18th, 1999, 09:20 PM
OK, here is a code with NetMessageBufferSend function: http://vbcode.webhostme.com/en/click.asp?id=115

Best Regards.

------------------
smalig
smalig@hotmail.com
http://vbcode.webhostme.com/

Clunietp
Dec 19th, 1999, 12:05 AM
Thank you Smalig. Your site is great, keep it up!

vbsquare
Dec 20th, 1999, 03:48 AM
You could even try downloading my DLL which allows two computers to connect and send pictures, text, files and binary over a winsock connection: http://www.vbsquare.com/internet/

------------------
"To the glory of God!"

Dec 20th, 1999, 05:17 AM
NET SEND is fine and dandy, if you have it. The problem is, how do you know that your end user has it? You'd have to write a detection function. And are you allowed to distribute it? I doubt it. So what are you to do? It's simple. Write a small chat client.

you'll need a form with two text boxes, two command buttons and a winsock control.

Sub Form_Load()
Namer = InputBox("Enter Username for this session:", "Username", "User")
'Set the port to listen on
winsock1.LocalPort = 1002
'Begin listening
winsock1.Listen
End Sub

Sub Form_Resize()
Text1.Width = Me.ScaleWidth
Text2.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight - text2.Height
Text2.Top = Text1.Height
End Sub

Sub Command1_Click()
Dim strRemoteHost As String
'Get the name of a computer to connect to
strRemoteHost = InputBox("Enter name or IP address of computer " & _
"to connect to.", vbOKCancel)
'Exit if cancelled
If strRemoteHost = "" Then Exit Sub
'Close any open connections
winsock1.Close
'Set the name of the computer to connect to
winsock1.RemoteHost = strRemoteHost
'Specify a port number on remote host
winsock1.RemotePort = 1002
'This seems to prevent some TCP errors
DoEvents
'Request the connection
winsock1.Connect
End Sub

Sub Command2_Click
winsock1.Close
DoEvents
winsock1.Listen
End Sub

Sub winsock1_Close()
'When connection by remote machine, go back to listening
winsock1.Close
winsock1.Listen
End Sub

Sub winsock1_ConnectionRequest(ByVal requestID As Long)
winsock1.Close
winsock1.Accept requestID
End Sub

Sub winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strText As String
'Get data
winsock1.GetData strText
'Display data received
Text1 = Text1 & Chr(13) & Chr(10) & strText & vbCrLf
'Move cursor to end
Text1.SelStart = Len(Text1)
End Sub

Sub Text2_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Is = 13
sendMessage ("" & Text2.Text)
txtMsg.Text = ""
End Select

End Sub

Sub sendMessage(Msg As String)
Static strSend As String
'Make sure there is a connection
If winsock1.State <> sckConnected Then Exit Sub
'Send the string
winsock1.SendData Namer & ": " & Msg
Text1.Text = Text1.Text & Chr(13) & Chr(10) & Namer & ": " & Msg
'Clear the variable
strSend = ""
'Keep track of what is being typed
strSend = strSend & Msg
End Sub

and in the globals section:
Option Explicit
Dim Namer as String

nrholder
Dec 21st, 1999, 05:56 AM
To use the NetMessageBufferSend API call you must be running Windows NT on the host. The recipient can be on Windows NT or Windows 95/98(as long as Winpopup is running).

In response to Rippleman, NetApi32.dll is on every Windows NT 4.0 system.

Clunietp
Dec 22nd, 1999, 11:46 AM
Rippleman

the problem with a chat client is that the user has to have the app installed an running. If you were to use NetMessageBufferSend over a LAN, the only thing the recipient has to have running is the Messenger Service (which by default is running)

Dec 23rd, 1999, 07:34 AM
ok, i guess that would work then. I'd never heard of that particular API, which is why i figured it was a special dll. In response to everyone, I think that this method would still work, but the NET SEND would work also, so i guess its how much you want to code.