atikojuane
New member
- Joined
- Aug 1, 2010
- Messages
- 1
- Programming Experience
- Beginner
Hi! Sorry if this is a stupid thread... but well... I'm a starter in Visual Basic 2005...
I'm tring to create a simple chat app, but my code doesn't work...
Here is the code...
Client:
Server:
The problems are:
Thanks for your help
I'm tring to create a simple chat app, but my code doesn't work...
Here is the code...
Client:
VB.NET:
Imports System
Imports System.IO
Imports System.Net
Imports System.TeXT
Imports System.Net.Sockets
Public Class Form1
Public tcpclnt As New TcpClient
Public stm As NetworkStream
'Button1 sends message
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim asen As New ASCIIEncoding
Dim ba(100) As Byte
Application.DoEvents()
ba = asen.GetBytes(TextBox3.Text)
Application.DoEvents()
stm.Write(ba, 0, ba.Length)
Dim bb(100) As Byte
Dim k As Integer
Application.DoEvents()
If stm.DataAvailable = True Then
k = stm.Read(bb, 0, bb.Length)
End If
Dim i As Integer
For i = 0 To k - 1
TextBox2.Text = TextBox2.Text & Chr(13) & Chr(bb(i))
Next
End Sub
'Button2 connects
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Text = TextBox2.Text & "Conected" & Chr(13)
tcpclnt.Connect(TextBox1.Text, 8001)
stm = tcpclnt.GetStream
End Sub
End Class
Server:
VB.NET:
Imports System.Net.Sockets
Imports System.Text
Imports System.Net
Imports System
Public Class Form1
Public s As Socket
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ipAd As IPAddress
ipAd = IPAddress.Parse(TextBox1.Text) 'the ip of textbox1.text
Dim msg As String
msg = "0"
Dim mylist As New TcpListener(ipAd, 8001)
Application.DoEvents()
While (msg <> "fin")
Application.DoEvents()
mylist.Start()
If mylist.Pending = True Then
s = mylist.AcceptSocket
Dim b(100) As Byte
Dim k As Integer
k = s.Receive(b)
Dim i As Integer
For i = 0 To k - 1
TextBox2.Text = TextBox2.Text & Chr(b(i))
Next
TextBox2.Text = TextBox2.Text & ControlChars.CrLf
End If
End While
s.Close()
mylist.Stop()
End Sub
'Button2 sends the message
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim asen As New ASCIIEncoding
s.Send(asen.GetBytes(TextBox3.Text))
TextBox3.Text = ""
End Sub
End Class
The problems are:
- Sorry for the excesive DoEvents, but the app sometimes frozes, I don't know why
- The first message I send from the client, is received by the server, but the rest don't..
- When the client receives a message from the server, it has a strange symbol between each character... for example.. if message = "hello" , the client receives "h¤e¤l¤l¤o¤" , each ¤ is a big rectangle...
- In previous versions the client app, after sending a message,frozes until the server send to the client a new message...
Thanks for your help