Can this be done (TCP Client/Server)? [solved]

Posts 1–15 of 24 · Page 1 of 2
Can this be done (TCP Client/Server)? [solved]
Hello there guys i stumbled across this forum while searching for help on my visual basic problem and realized that i had an old account here. So Lets move on to the important part.

I'm currently "trying" to make an app that gets a certain text from the client and sends it to a server and the server sorts it out every text that he gets from the client into a different label every time he gets a text from a client. For example :

Client1 sends certain text to Server and server puts it in label1.text
Client2 Sends different text to Server and server puts it in label2.text
Client3 sends different text than 1 and 2 to Server and server puts it in label3.text
and so on.

Can that be done?

If it can could you please post a quick source for both the server and client because i cannot seem to find anything on google.
Thanks
Yes. It is very easy. Have a look at the following thread. Contains source for both Server and Client. I am sure you have adequate knowledge to make changes to map the response to multiple Textbox controls.

http://www.mpgh.net/forum/378-open-s...ient-chat.html

If you don't understand how to do it, post the problem here and I'll try to help ya.
This is exactly what i wanted but on connect i want in this case their names to be put in a label.So if the label's text is bigger than 0 i want it to be put in a different label.That's the part i cannot figure out

EDIT : I figured it out but now i'm getting this error : Cross-thread operation not valid: Control 'n1' accessed from a thread other than the thread it was created on. n1 is the label i want the username to be in.
Quote Originally Posted by miksa555 View Post
This is exactly what i wanted but on connect i want in this case their names to be put in a label.So if the label's text is bigger than 0 i want it to be put in a different label.That's the part i cannot figure out

EDIT : I figured it out but now i'm getting this error : Cross-thread operation not valid: Control 'n1' accessed from a thread other than the thread it was created on. n1 is the label i want the username to be in.
You need to use delegates to access controls from a different thread. I am too lazy to write that so just put the following line in the Form1_Main():

[highlight=vbnet]Me.CheckForIllegalCrossThreadCalls = False[/highlight]

Will solve it for you.
Hehe you no longer have to use explicitly defined delegates for every control @Hassan. I haven't checked in VB yet but in C# I found out about a nifty trick. Don't have a Pc at the moment so it's not full code.

[highlight=VB.net]
label1.invoke(new MethodInvoker(can't remember constructor))
[/highlight]

I'll post a proper example when I get home
Quote Originally Posted by Jason View Post
Hehe you no longer have to use explicitly defined delegates for every control @Hassan. I haven't checked in VB yet but in C# I found out about a nifty trick. Don't have a Pc at the moment so it's not full code.

[highlight=VB.net]
label1.invoke(new MethodInvoker(can't remember constructor))
[/highlight]

I'll post a proper example when I get home
Hehe, its similar to the static Threading.Thread.
MethodInvoker constructor still requires a delegate.

Tried to implement it:

[highlight=vbnet]Private Sub UpdateLabel()
If Me.n1.InvokeRequired Then
Me.n1.Invoke(New MethodInvoker(AddressOf UpdateLabel))
Else
'n1.DoSomething...
End If
End Sub[/highlight]

Dunno if it can be done better than that.
Quote Originally Posted by Hassan View Post


Hehe, its similar to the static Threading.Thread.
MethodInvoker constructor still requires a delegate.

Tried to implement it:

[highlight=vbnet]Private Sub UpdateLabel()
If Me.n1.InvokeRequired Then
Me.n1.Invoke(New MethodInvoker(AddressOf UpdateLabel))
Else
'n1.DoSomething...
End If
End Sub[/highlight]

Dunno if it can be done better than that.
No you're missing the whole point. INLINE EXPRESSIONS (lambda)

I was actually quite a bit off with my first code, been a while but now I have a computer:

This is C# by the way, cbf converting to VB

[highlight=C#]
Label1.Invoke((MethodInvoker)delegate { Label1.Text = "lolololol"; });
[/highlight]


Quote Originally Posted by Jason View Post


No you're missing the whole point. INLINE EXPRESSIONS (lambda)

I was actually quite a bit off with my first code, been a while but now I have a computer:

This is C# by the way, cbf converting to VB

[highlight=C#]
Label1.Invoke((MethodInvoker)delegate { Label1.Text = "lolololol"; });
[/highlight]
Damn, didn't thought of them. Naice !!
VB Conversion is so gay.
Actually, I don't think there is any way to emulate that in VB, I just tried ( with no errors either at compile or runtime)

[highlight=vb.net]
Label1.Invoke(Ctype(Function() Label1.Text = "lolololol", MethodInvoker))
Label1.Invoke(Ctype(Function() Label1.Text = "LOLOLOLOL", [Delegate]))
Label1.Invoke(New MethodInvoker(Function() Label1.Text = "FUFUUFU"))
[/highlight]

No errors, they just dont do shit because of the way lambda functions work.

EDIT: And also because VB is fucking gay with it's multi-purpose "=" sign. Pretty sure the function works, but is instead performing an "is this equal to this" rather than a "assign this to this"
Quote Originally Posted by Jason View Post
Actually, I don't think there is any way to emulate that in VB, I just tried ( with no errors either at compile or runtime)

[highlight=vb.net]
Label1.Invoke(Ctype(Function() Label1.Text = "lolololol", MethodInvoker))
Label1.Invoke(Ctype(Function() Label1.Text = "LOLOLOLOL", [Delegate]))
Label1.Invoke(New MethodInvoker(Function() Label1.Text = "FUFUUFU"))
[/highlight]

No errors, they just dont do shit because of the way lambda functions work.

EDIT: And also because VB is fucking gay with it's multi-purpose "=" sign. Pretty sure the function works, but is instead performing an "is this equal to this" rather than a "assign this to this"
It is possible. I wrote Function(), pressed enter and it added End Function. Here's the implementation:

[highlight=vbnet]Label1.Invoke(New MethodInvoker(Function()
Label1.Text = "Fucking Gay, But Works !!"
End Function))[/highlight]
@Hassan by any chance do you use msn? Because i don't know if i can explain enough here and you guys are going a bit off topic.
I tried this from another thread, it worked:
[highlight=vb.net]Label1.Invoke(New MethodInvoker(Sub() Label1.Text = "If you see this, it works!"))[/highlight]
Quote Originally Posted by master131 View Post
I tried this from another thread, it worked:
[highlight=vb.net]Label1.Invoke(New MethodInvoker(Sub() Label1.Text = "If you see this, it works!"))[/highlight]
Shame "Sub()" wasn't introduced as a lambda expression until .NET 4.
I tried your methods but i couldn't get any of them to work,my app still got an error. Anyways this is the code i implemented so the text that is gotten from the client is implemented into labels.

Code:
 If n1.Text = "" Then
                n1.Text = c.nick

            Else
                If n2.Text = "" Then
                    n2.Text = c.nick

                Else
                    If n3.Text = "" Then
                        n3.Text = c.nick

                    Else
                        If n4.Text = "" Then
                            n4.Text = c.nick

                        Else
                            If n5.Text = "" Then
                                n5.Text = c.nick


                            End If
                        End If
                    End If
                End If
            End If
What it is supposed to do is in this case take the note which in the client i did to be as the name and send it to a label. I don't know if this is what i should keep or change it.
Posts 1–15 of 24 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us