-
1 Attachment(s)
[2005] tcp client
i found a place where i could download a tcp server. what really surprised me was that it appears to work just fine. i wont know if it really works until i get a client to connect to it. for about the last 3 hours i have been working diligently on making my own client but it hasn't been going well. so since i am not getting anywhere i was hoping someone on here could help me out. i know how hard it is to find a working tcp server/client that sends messages so it would be great to get this working. below i have uploaded the origanal copy of the tcpserver. i would really appreciate the help on this because i need it as a major part of my current project.
-
Re: [2005] tcp client
TCP's pretty easy with the TcpListener and TcpClient classes.
1. Call TcpListener.AcceptTcpClient at the server.
2. Call TcpClient.Connect at the client.
3. Get the underlying streams and start communicating.
-
Re: [2005] tcp client
ok well i have gotten it to where the client will connect to the server but i don't know how to send the data or text or login info. so again i am stuck and i am looking forward to anyones help. until then i will be google-ing like crazy and thanks jmc
-
Re: [2005] tcp client
Once you've connected you call the GetStream method of the TcpClient to get a NetworkStream object. You read and write data to and from that stream in essentially the same way as you do with any other stream, e.g. a FileStream. If you want to send and receive raw binary data then you'd use the stream itself. If you want to send and receive text then, just like a FileStream, you can lay a StreamReader or StreamWriter on top of the NetworkStream. If you want to send and receive binary objects you can lay a BinaryReader or BinaryWriter on top. If you wanted to encrypt the communication you could lay a CryptoStream on top.
You should read the documentation for the NetworkStream class and then ask any specific questions you have on its use.
-
Re: [2005] tcp client
thanks for the help again. i have been looking around on the forums all day and i have been playing around with atheist's example and i can get the client and server to connect and then i can send data but i am having a problem with displaying the data in a richtextbox when it is received on the server side and then i need to do the same thing on the client side. everything works fine except for that. i think it might be because they are on different threads but i have tried invoking them and it hasn't worked.So any help will again be welcome.
-
Re: [2005] tcp client
If you're receiving data in a background thread then you will need to use delegation in order to display that data in a control. Follow the Controls & Multi-threading link in my signature to see how that's done. If you follow the steps I've outlined there and still can't get it to work then post your code here and we'll see what we can do.
-
Re: [2005] tcp client
Code:
Public Sub messageReceived(ByVal sender As ConnectedClient, ByVal message As String)
'A message has been received from one of the clients.
'To determine who its from, use the sender object.
'sender.SendMessage can be used to reply to the sender.
Dim strg As String
Dim data() As String = message.Split("|"c) 'Split the message on each | and place in the string array.
Select Case data(0)
Case "CONNECT"
'We use GetClientByName to make sure no one else is using this username.
'It will return Nothing if the username is free.
'Since the client sent the message in this format: CONNECT|UserName, the username will be in the array on index 1.
If GetClientByName(data(1)) Is Nothing Then
'The username is not taken, we can safely assign it to the sender.
sender.Username = data(1)
End If
Case "DISCONNECT"
removeClient(sender)
Case "MSG"
'Dim message1 As String = data(1)
'MsgBox(messag)
'TextBox1.Text = message1
strg = richtextbox1.text + (Chr(13) & Chr(10)) & data(1)
trackchat(strg)
End Select
End Sub
Private Delegate Sub trackchatinvoker(ByVal text As String)
Private Sub trackchat(ByVal text As String)
If RichTextBox1.InvokeRequired Then
Me.RichTextBox1.Invoke(New trackchatinvoker(AddressOf trackchat), text)
Else
Me.RichTextBox1.Text = text
End If
End Sub
well i have looked at your code and i have tried it and didnt works so as usual i degub it.
i found out that in the sub trackchat the red line does not execute.
Code:
Private Delegate Sub trackchatinvoker(ByVal text As String)
Private Sub trackchat(ByVal text As String)
If RichTextBox1.InvokeRequired Then
Me.RichTextBox1.Invoke(New trackchatinvoker(AddressOf trackchat), text)
Else
Me.RichTextBox1.Text = text
End If
End Sub
i am sure that there is some small thing that i am missing and that is why it isnt working.
-
Re: [2005] tcp client
Here's where you're calling trackchat:
Code:
Case "MSG"
'Dim message1 As String = data(1)
'MsgBox(messag)
'TextBox1.Text = message1
strg = richtextbox1.text + (Chr(13) & Chr(10)) & data(1)
trackchat(strg)
What are you doing wrong in the line just above it? You're accessing a member of a control. In fact, a member of the very same RichTextBox. That is highly likely to be the issue. You should do the get and set in the delegated method.
Also, use the RichtextBox's AppendText method.
-
Re: [2005] tcp client
changed it to this and still skips the invoke line
Code:
Private Delegate Sub trackchatinvoker(ByVal text As String)
Private Sub trackchat(ByVal text As String)
If RichTextBox1.InvokeRequired Then
Me.RichTextBox1.Invoke(New trackchatinvoker(AddressOf trackchat), text)
Else
text = RichTextBox1.Text + (Chr(13) & Chr(10)) & text
Me.RichTextBox1.AppendText(text)
End If
End Sub
and yes i removed this line of code
Code:
strg = richtextbox1.text + (Chr(13) & Chr(10)) & data(1)
-
Re: [2005] tcp client
When you say that it skips that line, do you mean that RichTextBox1.InvokeRequired is False so it goes straight to the Else block?
-
Re: [2005] tcp client
yes i placed a watch on every line in that sub and it goes from
Code:
If RichTextBox1.InvokeRequired Then
straight to
Code:
Else
text = RichTextBox1.Text + (Chr(13) & Chr(10)) & text
Me.RichTextBox1.AppendText(text)
so yes invoke required=false
-
Re: [2005] tcp client
Then you are already on the thread that owns the RTB.
-
Re: [2005] tcp client
then why wont it update the RTB?
-
Re: [2005] tcp client
i have narrowed it down to where the problem is happening. it is not updating/appending the text to the rich text box. so if that helps then there it is.