LAN/Internet Chat Help

Blitz3D Forums/Blitz3D Programming/LAN/Internet Chat Help

Ked(Posted 2007) [#1]
Is it better to use TCP or UDP for chat programs? I use TCP and wonder if there is any way to create a server where you don't have to use Types. I have found no way of creating a server without having a MAV error when a client sends a message. I have looked at Semar's TCP Chat example in the code arcs but it doesn't help. And my Blitz is up to date. Does anyone have any help for me?


bytecode77(Posted 2007) [#2]
udp is easier to use. tcp is more secure and lets you know if a package arrived or not. if you dont know much about internet programming, user udp. if you know more, use tcp. a friend of mine made an upd chat using php. :)


semar(Posted 2007) [#3]
I have looked at Semar's TCP Chat example in the code arcs but it doesn't help

Actually, that Chat example works very well, and it's full documented. In which way it does not help you ? Which department would you like to be cleared ?

Please let me know,
Sergio.


Ked(Posted 2007) [#4]
@Semar:
Yours is good, don't get me wrong. But everytime I make a TCP chat there is ALWAYS a MAV error. No matter what I do. I don't have any idea of what I'm doing wrong. But when I copy and paste your code it works perfect.


semar(Posted 2007) [#5]
Well, show us your code and let's try to find out where the problem is. Do you declare the tcp stream as global ?

If my code works for you too, then you may compare it with the offending one, and try to analyze the differences. It may help you to sort out the MAV you're getting.

Sergio.


Ked(Posted 2007) [#6]
Hold on... I'll repost.


Ked(Posted 2007) [#7]
SERVER:
Graphics 400,200,32,2
;SERVER;

Global t
Global msg$,port=49494,ready=False

Type user
	Field name$
	Field stream
End Type

server=CreateTCPServer(port)
If server=0 End

While Not KeyHit(1)
t=AcceptTCPStream(server)

If t
	msg$=ReadLine(t)
	If msg$="[LOGIN]"
		msg$=ReadLine(t)
		u.user=New user
		u\name$=msg$
		u\stream=t
		
		For u.user=Each user
			WriteLine u\stream,"[SERVER]"
			WriteLine u\stream,"--------------------------------------"
			WriteLine u\stream,"SERVER: "+Upper(msg$)+" HAS LOGGED ON!"
			WriteLine u\stream,"--------------------------------------"
		Next
	EndIf
EndIf

For u.user=Each user
	If Not Eof(u\stream)
		If (u\stream)
			msg$=ReadLine(u\stream)
			If msg$="[MSG]"
				msg$=ReadLine(u\stream)
				ready=True
			ElseIf msg$="[LOGOUT]"
				the_name$=u\name$
				the_stream=u\stream
				For u.user=Each user
					If u\stream=the_stream Then Delete u:Exit
				Next
				For u.user=Each user
					WriteLine u\stream,"[SERVER]"
					WriteLine u\stream,"--------------------------------------------"
					WriteLine u\stream,"SERVER: "+Upper(the_name$)+" HAS LOGGED OFF!"
					WriteLine u\stream,"--------------------------------------------"
				Next
			EndIf
		EndIf
	Else
		the_name$=u\name$
		the_stream=u\stream
		For u.user=Each user
			If u\stream=the_stream Then Delete u:Exit
		Next
		For u.user=Each user
			WriteLine u\stream,"[SERVER]"
			WriteLine u\stream,"--------------------------------------------"
			WriteLine u\stream,"SERVER: "+Upper(the_name$)+" HAS LOGGED OFF!"
			WriteLine u\stream,"--------------------------------------------"
		Next
	EndIf

If ready=True
	ready=False
	For u.user=Each user
		WriteLine u\stream,"[USER]"
		WriteLine u\stream,msg$
	Next
EndIf

Next

Delay 2

If KeyHit(1)
For u.user=Each user
	WriteLine u\stream,"[SERVER]"
	WriteLine u\stream,"--------------------------------------------"
	WriteLine u\stream,"SERVER: THE SERVER IS OFFLINE!"
	WriteLine u\stream,"--------------------------------------------"
Next
EndIf

Wend
End


CLIENT:
Graphics 640,480,32,2

Global t,msg$,mesg$,name$

font=LoadFont("courier new",16):SetFont font

Print "Connecting..."

t=OpenTCPStream("127.0.0.1",49494)
If t=0
	Print "COULD NOT CONNECT"
	WaitKey()
	End
Else
	Print "CONNECTED"
	WaitKey()
EndIf

name$=Input$("Name: ")
If name$=""
	name$="UNKNOWN"
EndIf

WriteLine t,"[LOGIN]"
WriteLine t,name$
Cls
Locate 0,0
Print "WELCOME!!"

While Not KeyHit(1)
If t
	msg$=ReadLine(t)
	If msg$="[USER]"
		msg$=ReadLine(t)
		Print msg$
	ElseIf msg$="[SERVER]"
		Color 255,0,0
		msg$=ReadLine(t)
		Print msg$
		msg$=ReadLine(t)
		Print msg$
		msg$=ReadLine(t)
		Print msg$
		Color 255,255,255
	EndIf
EndIf

If KeyDown(29)
	While KeyDown(29)
	Wend
	
	mesg$=Input$(name$+": ")
	If mesg$<>""
		mesg$=name$+": "+mesg$
		WriteLine t,"[MSG]"
		WriteLine t,mesg$
	EndIf
EndIf

Delay 2
Wend
WriteLine t,"[LOGOUT]"
End



semar(Posted 2007) [#8]
Hi Ked,
bare in mind that you need a ReadAvail(stream) command in order to check if there is Available data on a stream, before to read from it:

strStream=AcceptTCPStream(svrGame)

If strStream Then ;a new client has joined the chat; his ID stream is strStream

;memorize the current connection stream
received_stream = strStream

;reads info from the client just connected
If ReadAvail(strStream) Then
;read the stream here
.
.

If you donīt check for available data, and try to read from the stream, you may get errors.

In your server code that you posted above:
While Not KeyHit(1)
t=AcceptTCPStream(server)

If t
	msg$=ReadLine(t)


you read from the stream t without perform any check with ReadAvail(stream) command.

Please read the ReadAvail command description in your help file, and change your code accordingly. It may also help if you compare your code with my internet-chat example in code archive.

Hope it helps,
Sergio.


Ked(Posted 2007) [#9]
I tried it and it didnt work. it doesn't read the stream if I have a ReadAvail() in there. When I tried it with the ReadAvail() it didn't read the information so it didn't make the user so your not able to chat. Any other tips? I would really like to create my own TCP chat.


semar(Posted 2007) [#10]
I tried it and it didnt work.
it doesn't read the stream if I have a ReadAvail() in there.
When I tried it with the ReadAvail() it didn't read the
information so it didn't make the user so your not able to
chat. Any other tips? I would really like to create my own
TCP chat.

You must use the ReadAvail command in order to read from the TCP stream. If it des not work, and it should, then there's something wrong in your code.

The client code needs too such a command, and in your client code you don't use it.

If your chat program doesn't work, while my example does, then I suggest you to carefully compare both and find out the differences; work on it until you understand where and why they differ, and then modify your code so that behavies in the same way.

TCP is not that hard, but you must ensure you program it in the right way. Remember, your code *must* use the ReadAvail command. It's mandatory to work with TCP commands.

Sergio.


Ked(Posted 2007) [#11]
Semar. You are my new best friend. I figured it out and everything works! I am now using it for my LAN. Thanks!


semar(Posted 2007) [#12]
Semar. You are my new best friend. I figured it out and everything works! I am now using it for my LAN. Thanks!

Ked,
many thanks for you kind words.

I'm glad you've sorted it out :)

Have fun,
Sergio.


Blitzplotter(Posted 2007) [#13]
I've tried Ked's client & Server and Semar's client and Server. Tried to implement the solution Semar was giving to Ked, no joy. So ran semar's client and server, and for whatever reason my client fails to connect to the server despite the server is running. Went to DOs prompt, did a netstat and found that, yes the server had created a TCP port at 0.0.0.0 on the relvanr port. Despite this, my client failed to connect. Any suggestions for diagnosing the problem ?

regards,

BP


semar(Posted 2007) [#14]
@Blitzplotter,
that's strange. If you want to use chat on your local pc, then the server should open a port at 127.0.0.1, which is usually the local address.
By your reporting, seems that tha local address is set to 0.0.0.0, which puzzles me.

The client usually attempts to connect to the same port at 127.0.0.1, wo no wonder if it can't connect.

I *guess* you may should investigate on your IP configuration, and check that your pc has got a valid IP address.

Are you behind a router perhaps ? Anyway, try the following: within a DOS prompt, type IPCONFIG. It should show you your actually IP address. Use that instead of 127.0.0.1 or whatever. Remember to do the same also in the client code.

Please post your progresses, I'm interested to know if you have it sorted.

Regards,
Sergio.


Blitzplotter(Posted 2007) [#15]
Investigated inline with your suggestions. I created the Server exe with the default code.

I then populated my client code with my IP address from the IPCONFIG. The client successfully connected to the server.

regards, Mark.