PDA

View Full Version : SendMessag توی LAN



sm
یک شنبه 12 مهر 1383, 17:55 عصر
برای ارسال پیام توی یک شبکه محلی چطوری باید عمل کرد؟
از چی و چطوری باید استفاده کرد؟
ممنون

arshia_
یک شنبه 12 مهر 1383, 18:51 عصر
می تونی از گزینه Services توی قسمت aministrative tools کمک بگیری
. توی اونجا کلیک راست کنی و send message ... رو انتخاب کنی

sh
یک شنبه 12 مهر 1383, 22:17 عصر
Send a Broadcast Message
Problem
You want to send a message to every user on the local subnet.

Solution
Use the UdpClient class with the appropriate broadcast address.

Discussion
Broadcasts are network messages that are forwarded to all the devices on a local subnet. When a broadcast message is received, the client decides whether to discard (depending on whether any application is monitoring the appropriate port). Broadcasts can't travel beyond a subnet because routers block all broadcast messages. Otherwise, the network could be swamped in traffic.

To send a broadcast message, you use a broadcast IP address, which is the IP address that identifies the network and has all the host bits set to 1. For example, if the network is identified by the first three bytes (140.80.0), the broadcast address would be 140.80.0.255. Alternatively, you can set all bits to 1 (the address 255.255.255.255), which specifies the entire network. In this case, the broadcast message will still travel only inside the local subnet because routers won't allow it to pass.

Dim IP As String = "255.255.255.255"
Dim Port As Integer = 8800

Dim RemoteEndPoint As New IPEndPoint(IPAddress.Parse(IP), _
Port)

Dim Client As New UdpClient()
Dim Data() As Byte = System.Text.Encoding.UTF8.GetBytes("Broadcast Message")

' Send the broadcast message.
Client.Send(Data, Data.Length, RemoteEndPoint)




Send a Custom Message Object Through Message Queuing
Problem
You want to send a custom object to another application via a message queue.

Solution
Create a serializable class that has a zero-argument constructor and includes no read-only properties. Wrap an instance of this class in a Message object and pass the Message object to the MessageQueue.Send method.

Discussion
Message queues accept simple text messages and custom .NET objects. In order to use a custom object, it must satisfy three criteria:

The class must have a public constructor with no arguments. .NET uses this constructor to re-create the object when the message is received.

The class must be marked with the Serializable attribute.

All class properties must be readable and writable. Read-only properties won't be serialized because .NET won't be able to restore the property values when re-creating the object.

Here's an example of a class that can be serialized and sent in the body of a message:

<Serializable()> _
Public Class OrderMessage

Private _OrderCode As String
Private _ClientName As String

Public Property OrderCode()
Get
Return _OrderCode
End Get
Set(ByVal Value)
_OrderCode = Value
End Set
End Property

Public Property ClientName() As String
Get
Return _ClientName
End Get
Set(ByVal Value As String)
_ClientName = Value
End Set
End Property

Public Sub New()
' No actions are required in the default constructor.
End Sub

Public Sub New(ByVal code As String, ByVal client As String)
OrderCode = code
ClientName = client
End Sub

End Class

The following example shows the full code for a simple test form that sends and receives instances of the OrderMessage object. Sending the object is effortless—you simply need to wrap it in a Message object and pass the Message object to the MessageQueue.Send method. Retrieving the message is equally easy, provided you make sure to specify the expected object types in the constructor for the XmlMessageFormatter. If you attempt to deserialize an unrecognized object (one that you have not configured through the MessageQueue.AllowedTypes property), an exception will be thrown.

Public Class QueueForm
Inherits System.Windows.Forms.Form

' (Designer code omitted.)
Private QueuePath As String = ".\Private$\OrderQueue"
Private Queue As MessageQueue

Private Sub Form_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Create the queue if needed.
If MessageQueue.Exists(QueuePath) Then
Queue = New MessageQueue(QueuePath)
Else
Queue = MessageQueue.Create(QueuePath)
End If

' Messages are formatted using XML (not binary) encoding.
Queue.Formatter = New XmlMessageFormatter()
End Sub

Private Sub cmdSend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdSend.Click
Dim Ord As New OrderMessage(Guid.NewGuid().ToString(), "Test Client")
Dim Msg As New Message(Ord)

' (Configure other Message properties here.)
Msg.Label = "Test Order"

Queue.Send(Msg)
End Sub

Private Sub cmdReceive_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdReceive.Click
Me.Cursor = Cursors.WaitCursor

Dim ReceivedMessage As Message

' Specify all the allowed object types in an array.
Dim AllowedTypes() As Type = {GetType(OrderMessage)}
Queue.Formatter = New XmlMessageFormatter(AllowedTypes)

Try
' If no message is found after five seconds, an exception occurs.
' You can also explicitly retrieve all messages from the queue
' using the Queue.GetAllMessages method.
ReceivedMessage = Queue.Receive(TimeSpan.FromSeconds(5))

' Check if it is the expected type.
If TypeOf ReceivedMessage.Body Is OrderMessage Then
' Process message here.
Dim ReceivedOrder As OrderMessage
ReceivedOrder = CType(ReceivedMessage.Body, OrderMessage)
MessageBox.Show("Received order: " & ReceivedOrder.OrderCode)
End If
Catch Err As MessageQueueException
MessageBox.Show("No messages.")
End Try

Me.Cursor = Cursors.Default
End Sub

End Class

In order to use this code as written, you must add a reference to the System.Messaging.dll assembly and import the System.Messaging namespace.

sm
دوشنبه 13 مهر 1383, 17:36 عصر
آقا شهریار ممنون
امتحان می کنم نتیجه اش رو خدمتتون عرض می کنم
ممنون

ali_kolahdoozan
جمعه 07 تیر 1387, 07:16 صبح
نتیجه چی شده ؟