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.
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
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.
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.
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.
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.
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.
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
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.