.Net Send Message using Socket to multiple LAN Computer as well as receive
Im new to this socket thing so please bear with me a little understanding.
I can send/recieve message on One - to - One computer using this code
Private Function GetLocalIP() As String
Dim myHost As IPHostEntry
myHost = Dns.GetHostEntry(Dns.GetHostName())
For Each ip As IPAddress In myHost.AddressList
If ip.AddressFamily = AddressFamily.InterNetwork Then
Return ip.ToString()
End If
Next
Return "127.0.0.1"
End Function
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles
btnConnect.Click
epLocal = New IPEndPoint(IPAddress.Parse(GetLocalIP()),
Convert.ToInt32("800"))
mySocket.Bind(epLocal)
'Connecting To Remote IP TextBox1 is IP of the other PC
epRemote = New IPEndPoint(IPAddress.Parse(TextBox1.Text),
Convert.ToInt32("800"))
mySocket.Connect(epRemote)
'Listening TO specific Port
buffer = New Byte(1499) {}
mySocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None,
epRemote, New AsyncCallback(AddressOf MessageCallBack), _
buffer)
'Changing btnCOnnect text
btnConnect.Text = "Connected"
btnConnect.Enabled = False
End Sub
Private Sub MessageCallBack(ByVal aResult As IAsyncResult)
Try
Dim RecivedData As Byte() = New Byte(1499) {}
RecivedData = DirectCast(aResult.AsyncState, Byte())
'converting byte[] into string
Dim aEncoding As New ASCIIEncoding()
Dim RecivedMessage As String = aEncoding.GetString(RecivedData)
'Adding this message to listbox
ListMessages.Items.Add("Friend : " & RecivedMessage)
buffer = New Byte(1499) {}
mySocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, epRemote, New AsyncCallback(AddressOf
MessageCallBack), _
buffer)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles
btnSend.Click
Dim aEncoding As New ASCIIEncoding()
Dim SendingMessage As Byte() = New Byte(1499) {}
SendingMessage = aEncoding.GetBytes(txtmessage.Text)
'sending Encoding message
mySocket.Send(SendingMessage)
'adding to listbox
ListMessages.Items.Add("ME : " + txtmessage.Text)
txtmessage.Text = Nothing
End Sub
So the way to do this is i have to click the connect button (both) and
send messages
Now what I want to achieve is to send message to all PC inside our
workgroup ive created this
Dim list As ArrayList
And i am quite sure that all of the IP are there. ive tried this code but
it isnt working
Private Sub btnSendAll_Click(sender As Object, e As EventArgs) Handles
btnSendAll.Click
For x As Integer = 0 To list.Count - 1
If (list.Item(x).Equals(GetLocalIP())) Then
Else
mySocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
epLocal = New IPEndPoint(IPAddress.Parse(GetLocalIP()),
Convert.ToInt32("800"))
mySocket.Bind(epLocal)
epRemote = New IPEndPoint(IPAddress.Parse(list.Item(x)),
Convert.ToInt32("800"))
mySocket.Connect(epRemote)
buffer = New Byte(1499) {}
mySocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, epRemote, New AsyncCallback(AddressOf
MessageCallBack), _
buffer)
Dim aEncoding As New ASCIIEncoding()
Dim SendingMessage As Byte() = New Byte(1499) {}
SendingMessage = aEncoding.GetBytes(txtmessage.Text)
'sending Encoding message
mySocket.Send(SendingMessage)
ListMessages.Items.Add("ME : " + txtmessage.Text)
txtmessage.Text = Nothing
End If
Next
End Sub
Can you help me fix this?
Thanks in advance (Ive just returned to .Net from Android)
No comments:
Post a Comment