By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

simple client/server problem. server stops responding

P: n/a
Hello,
I am just learning the tcp/ip functions etc under vb.net so please
look over me if this is obviouse. I have been all over looking into
any functions that I didn't totaly understand and can't see that I am
doing anything wrong (i obviousely am though)

everything "works", client connects to server and when anything is
sent to the server it starts the mp3 and passes all messages the way i
ment for it to except that my server is supposed to display the data
it recieves in a window (that worked untill i changed some things) and
my server will hang after I pass a few strings to it from the client.

The hanging is what I can't figure out. I can debug the other no
problem.

Anyway here is the code, please look over how sloppy it has gotten
after hours of changeing things arround.
Server :
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
Inherits System.Windows.Forms.Form

'Windows Form Designer Generated Code was here "removed for size of
post"

Const port As Int32 = 8353
Dim localIp As IPAddress = IPAddress.Parse("127.0.0.1")
Dim server As New TcpListener(localIp, port)
Dim netStream As NetworkStream

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'start tcplistener on localhost: 8353
server.Start()

Try
'accept connections
Dim client As TcpClient = server.AcceptTcpClient()

'get stream
netStream = client.GetStream

While netStream.CanRead And netStream.CanWrite
'read stream into a byte array
Dim reciveBufferSize(client.ReceiveBufferSize) As Byte
netStream.Read(reciveBufferSize, 0,
CInt(client.ReceiveBufferSize))

'convert data from the client into a usable string
variable
Dim dataFromClient As String =
Encoding.ASCII.GetString(reciveBufferSize)

'do something with the data
TextBox2.Text = dataFromClient

'send a response to the client
Dim serverResponse As String = "Transmision completed"
Dim sendBufferSize As [Byte]() =
Encoding.ASCII.GetBytes(serverResponse)
netStream.Write(sendBufferSize, 0,
sendBufferSize.Length)

'show what was sent
TextBox1.Text = serverResponse

If dataFromClient.ToString > "" Then
System.Diagnostics.Process.Start("C:\Documents and
Settings\devan\My Documents\my music\christian\Did You Feel the
Mountains Tremble_ (1).mp3")
End If
End While
Catch exc As Exception
MessageBox.Show(exc.ToString)

End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
server.Stop()
End Sub
End Class



Client:

Imports System.net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form

'Windows Form Designer Generated Code was here "removed for size of
post"
Dim client As New TcpClient
Dim netStream As NetworkStream
'If theres a problem check if this should be public for both
client and server
Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'establish connection

client.Connect("127.0.0.1", 8353)

netStream = client.GetStream

'show connection status
If netStream.CanRead And netStream.CanWrite Then
StatusBar1.Text = "connected"
''do simple write
'Dim clientCommand As String
'clientCommand = TextBox1.Text.ToString
'Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(clientCommand)
'netStream.Write(sendBytes, 0, sendBytes.Length)

'' Read the NetworkStream into a byte buffer.
'Dim bytes(client.ReceiveBufferSize) As Byte
'netStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))

''Do something with data from server
'Dim dataFromServer As String =
Encoding.ASCII.GetString(bytes)
'Label2.Text = dataFromServer.ToString

Else
If netStream.CanRead = False Then
StatusBar1.Text = "cannot write to stream"
Else
If netStream.CanWrite = False Then
StatusBar1.Text = "cannot read stream"
End If
End If

End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
'do simple write
Dim clientCommand As String
clientCommand = TextBox1.Text.ToString
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(clientCommand)
netStream.Write(sendBytes, 0, sendBytes.Length)

' Read the NetworkStream into a byte buffer.
Dim bytes(client.ReceiveBufferSize) As Byte
netStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))

'Do something with data from server
Dim dataFromServer As String = Encoding.ASCII.GetString(bytes)
Label2.Text = dataFromServer.ToString
End Sub
End Class


Thanks in advance if anyone can help me figure this out.
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.