Question socket receive event

Jamescow

New member
Joined
Jul 13, 2011
Messages
3
Programming Experience
Beginner
I am beginer of VB.NET. There is a small project want to be in VB.NET. Almost finish it but have problem of receiving datas event in socket.
This small project requires below:
1. Server sends a serial,name to all of client's PCs.
2. All of Client's PCs show a label display this serial number and name. After 5 seconds, this label disappeare.
3. If server sends next serial/name to PC within 5 second by last serial/name, it will still show 5 seconds.

Then server side, codes :
----------------
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sends("192.168.1.70")
End Sub
Public Sub sends(ByVal str)
Try
Dim bytes(1024) As Byte
Dim s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'socket = new Socket(hostEP.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp)
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2)
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1)
Dim localEndPoint As New IPEndPoint(IPAddress.Parse(str), 1024)
s.Connect(localEndPoint)
s.SendTimeout = 2
If s.Connected Then
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2)
s.Send(Encoding.Unicode.GetBytes(TextBox1.Text))
s.ReceiveTimeout = 2
s.Close()
End If

Catch ex As Exception
End Try
End Sub
End Class
-----------------------------

Client side:

--------------

Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.IO


Public Class Form1
Dim s As Socket = Nothing
Dim t As Thread
Declare Sub Sleep Lib "kernel32 " (ByVal milliseconds As Long)
Public Sub WaitData()
s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim strHostName As String = Net.Dns.GetHostName()
Dim ipEntry As Net.IPHostEntry = Net.Dns.GetHostEntry(strHostName)
Dim strAddr As String = ipEntry.AddressList(0).ToString() 'XP

Dim localEndPoint As New IPEndPoint(IPAddress.Parse(strAddr), 1024)
s.Bind(localEndPoint)
s.Listen(100)
While True
Try
Dim bytes(1024) As Byte
Dim ss As Socket = s.Accept()
ss.Receive(bytes)
ListBox1.Items.Insert(0, Encoding.Unicode.GetString(bytes))
If Len(CStr(Encoding.Unicode.GetString(bytes))) > 5 Then

Me.Timer1.Interval = 5000
Me.Timer1.Enabled = True
Me.Timer1.Start()

End If
End While

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t = New Thread(AddressOf WaitData) 'start listen
t.Start()
Button1.Enabled = False
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
s.Close()
t.Abort()
Catch
Finally
Button1.Enabled = True
End Try
End Sub

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Try
s.Close()
t.Abort()
Catch
End Try
End Sub



Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Visible = False
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Timer1.Interval = 5000
Me.Timer1.Enabled = True
Me.Timer1.Start()
End Sub

End Class

-----------------------------------------------


Now, there are 2 questions here:

1. when server sends words, PC does receive it and display label1. Problem is that LABEL1 doesn't disappear after 5 seconds. Why? it seems it keeps running timer1 then it doesnt stop. Any ideal how to make this work?

2. If client PC is offline/shutdown, server side will stop running for 20-30 seconds. How can I make it sooner?


It's better that you can provide full code and comments since I am new at this.
Thanks a lot for any help!
 
Last edited by a moderator:
Back
Top