i have made a console based socket program in vb.net, all it does2 things: 1-server shows a msgbox when the command msg is sent from the client
2- the server opens the cd try when the command open is sent form the client
so my question is : when i run it , i type the command open and works just fine , the cd tray opens , but if i type open again or msg it simply wont do anything(this also applies for the command msg, if type it a msg is shown, but if i type it agine or open , it wont respond. so its like one command per session.
i tried to put the code in a while loop , but didnt work(am not sure if i did it the right way)
so plz help
here is the code for the server:
Imports System.IO
Imports System.Net.Sockets
Module Module1
Private setTrayStatus As Long
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Sub Main()
Console.WriteLine("")
Dim clientListener As New TcpListener(8585)
clientListener.Start()
Console.WriteLine("")
Dim mySocket As Socket = clientListener.AcceptSocket()
Console.WriteLine("")
Dim recieveBuff(10) As Byte
mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
Dim str As String = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0))
If str = "open" Then
setTrayStatus = mciSendString("Set CDAudio Door Open", Nothing, 0, 0)
mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
ElseIf str = "close" Then
setTrayStatus = mciSendString("Set CDAudio Door Closed", Nothing, 0, 0)
mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
ElseIf str = "msg" Then
MsgBox("hello", MsgBoxStyle.OkCancel, "msg form client")
mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
End If
End Sub
End Module
and the code for the client :
Imports System.IO
Imports System.Net.Sockets
Module Module1
Sub Main()
Try
Console.WriteLine("Connecting to 8585 Local Host")
Dim serverListener As New TcpClient("localhost", 8585)
Dim readStream As Stream = serverListener.GetStream
Console.WriteLine("Input Lines:")
Dim str As String = Console.ReadLine()
While True
Dim sendBuff As Byte() = System.Text.Encoding.ASCII.GetBytes(str)
readStream.Write(sendBuff, 0, sendBuff.Length)
If str.StartsWith(".") Then
GoTo Done
End If
str = Console.ReadLine()
End While
Done: Console.WriteLine("Done")
Catch exp As Exception
Console.WriteLine("Exception: " + exp.ToString())
End Try
End Sub
End Module
thanks in advance