-
Dec 6th, 2017, 09:41 AM
#1
Thread Starter
Member
Getting simultaneous messages from different clients - TCP/IP communication
Hi! Please, can anyone help me? I'm doing a test by creating a chat program that receives data from multiple clients. Everything is working well. My problem is that when two clients send messages at the same time, I just get the message from one of the clients. Is there a way to get incoming messages at the same time sorted by order of arrival?
below, part of the Server code.
Code:
Public Sub LISTEN()
Dim CLIENT As New NEWCLIENT
While True
Try
CLIENT.SOCKETCLIENT = SERVER.AcceptSocket
CLIENTIP = CLIENT.SOCKETCLIENT.RemoteEndPoint
CLIENT.THREADCLIENT = New Thread(AddressOf READ)
CLIENTS.Add(CLIENTIP, CLIENT)
NEWCONEXION(CLIENTIP)
CLIENT.THREADCLIENT.Start()
Catch ex As Exception
End Try
End While
End Sub
Public Sub READ()
Dim CLIENT As New NEWCLIENT
Dim DATA() As Byte
Dim IP As IPEndPoint = CLIENTIP
CLIENT = CLIENTS(IP)
While True
If CLIENT.SOCKETCLIENT.Connected Then
DATA = New Byte(1024) {}
Try
If CLIENT.SOCKETCLIENT.Receive(DATA, DATA.Length, 0) > 0 Then
CLIENT.MSG = Encoding.UTF7.GetString(DATA)
CLIENTS(IP) = CLIENT
Datareceived(IP)
Else
Exit While
End If
Catch ex As Exception
Exit While
End Try
End If
End While
Call ENDTHREAD(IP)
End Sub
Private Sub DATARECEIVED(ByVal IDTerminal As IPEndPoint)
ListBox1.Items.Add(DATARECEIVED)
End Sub
-
Dec 6th, 2017, 11:01 AM
#2
Re: Getting simultaneous messages from different clients - TCP/IP communication
Wow. This coding style is very archaic. Please consider looking at the examples on MSDN and adopting a style like them. TYPING IN ALL CAPS MAKES IT VERY DIFFICULT TO READ. THERE ARE PSYCHOLOGICAL STUDIES THAT PROVE IT. AndAlternatingCapsMakeItEasier TOPICKOUTWORDSIFTHEREARENOSPACES. I know it's Visual "BASIC", but I think we've moved on from all-caps.
I can't answer definitively, because I don't know what the type "NEWCLIENT" is. If you don't show me the types your code uses, I can't make a good assertation of where problems lie.
This bit is very bad:
Code:
Catch ex As Exception
End Try
Your program throws exceptions to tell you something went wrong. This code says, "If something goes wrong, don't bother me". So if something is going wrong with the message you "aren't receiving", you've politely asked your program to shove it. That means it's probably happening and you don't know. The error message would be useful when it comes to understanding what is happening.
I can't make much progress. Your code looks like pseudocode because you aren't using any types I'm familiar with, and you're also making decisions that don't make any sense from an expert perspective. For example:
Code:
If CLIENT.SOCKETCLIENT.Receive(DATA, DATA.Length, 0) > 0 Then
If this is any Socket.Receive() I'm familiar with, the function is returning how many bytes it read. That may be only 10 or 15 bytes, but the array 'DATA' is 1024 bytes. So you're supposed to make sure you only try to decode that many bytes. But then:
Code:
CLIENT.MSG = Encoding.UTF7.GetString(DATA)
UTF7 is a very, very strange encoding. Almost no one uses it. Why did you choose this encoding?
That little bit of code, if written with familiar APIs, should look something like this:
Code:
Try
Dim bytesRead = clientInfo.Client.Receive(buffer, buffer.Length, 0)
If bytesRead > 0 Then
Dim message As String = Encoding.UTF8.GetString(buffer, bytesRead, 0)
clientInfo.Message = message
...
Like above, you don't do anything reasonable if this throws an exception, so you don't have a clue if it's working. Never, ever, ever write a "Catch ex As Exception" if you aren't logging or displaying what the exception does.
TL;DR:
Don't try to write your own chat client just yet. There are hundreds of tutorials online with examples. Download one of them, study it, and try transcribing it so you can learn how it works. After you've played with a few tutorials for a few days, try this again and see if it isn't easier.
Also, seriously. Lose the all-caps. I almost decided to ignore this thread because of it.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|