TCP Chat client

Blitz3D Forums/Blitz3D Beginners Area/TCP Chat client

mrmango(Posted 2004) [#1]
I created a small client to allow TCP chat.. mainly to play with the idea of chating duting games.

Whilst it is quite a nasty through together code wise. This is my first attempt at anything in Blitz since my days of Stos.

The problems I have is that the client seems to crash when I click away from the window and also if you run the client on seperate machines and not loopback, then it seems unstable.

I know its nasty, but where am I going wrong?

Code===

;
AppTitle "Msg Client V 0.1 - Type exit on cmd Line To quit"

;----------->>set main global variables<<----------

;client variables

Global sendpinger$
Global timer
Global username$
Global otherip$
Global startsession$
Global msgsend$
Global msgreceive$

;server variables

Global connectclient$
Global connecturl1$
Global inputentry$
Global timergap$
Global entryfield$

;misc variables

Global linenumber

;----------->>init and set the pinger/server<<-----------

Graphics 640,260,16,2
linenumber=10
connecturl1$=CreateTCPServer(8080)


username$=Input$("Username: ")
otherip$=Input$("IP: ")


sendpinger$=OpenTCPStream(otherip$,8080)
Cls
.startagain

;If linenumber =230 Then
;linenumber=10
;Cls
;EndIf


Locate 0,240
msgsend$=Input$(">: "):msgsent=1
Cls
If msgsend$="exit" Then End

If msgsent=1
WriteString sendpinger$,"Message: "+msgsend$+" from "+username$
Chr$(10)
msgsent=0
EndIf

;CloseTCPStream = sendpinger$

sendpinger$=OpenTCPStream(otherip$,8080)

connectclient$=AcceptTCPStream(connecturl1$)
If connectclient$ Then
inputentry$=ReadString$ (connectclient$)

Locate 0, linenumber
Print inputentry$+" "+CurrentDate()+" "+CurrentTime$()
; linenumber=linenumber+10
EndIf

Goto startagain


rdodson41(Posted 2004) [#2]
Well, I'm making a chat program, maybe someday it will be better than AIM (dream on :) ) and i found out a lot of stuff while making it.

First, it's good to have a central server, you just send the ID of the person your message is for, and then the message, and the server handles getting it there. That way you only have to worry about 1 ip address, since they change often and are hard to find.

Also, textareas are better for displaying and text, or conversation in my case, because they have built in scrolling. Canvases are difficult to scroll, and there is a lib by nullmind that makes textareas uneditable.

Some problems i see are:
- you make a new stream every time. Not a good idea. Keep the same one until program is done executing.
- some bad code, typos, etc. i dont know if thats just while posting. I dont know why you have ';' in front of some commands.

Try telling exactly what goes wrong, ie. error messages, debuging messages, etc.

I can show you some of my code if you want, just ask.

Hope it helps!


eBusiness(Posted 2004) [#3]
Welcome to the forum :)

Don't store your handles as strings, use integers.
Using port 8080 you are likely to collide with some browser stream.

Looks a lot like my first attemp at using TCP streams. If want to see a regular example then download my 5InARow, or look at one of the many examples in the archive.


mrmango(Posted 2004) [#4]
Firstly thanks all for replying :-))

I will see if I can get a better error msg.. sorry.

I was trying to avoid a server. I had built it originally that way, but just wanted to be able to have one .exe. Though I think you are right. I may pull out the server code ;-)

textareas? Are these Blitz Plus commands? If not I will have a play with those. Sounds better than the way I am doing it now.

I will remove the new stream command. Thought I'd gone wrong there...

I have some commands with a ; in front, so if what's wrong with the program isn't that particular command, I can just comment it back in. I use it alot whilst learning.

I have a crack at using integers. ;-)

I thought the port may conflict. I'll have a look for another.

Thanks for you help


eBusiness(Posted 2004) [#5]
I'm afraid textareas can only be found in Blitz Plus. Anyway you won't need two different programs, just let the user choose if want to wait for an incomming stream, or connect to an other ip.
;Make the user choose here
If choice=0 Then
	server=CreateTCPServer(45211);I guess this port is not in use
	While stream=0
		stream=AcceptTCPStream(server)
	Wend
Else
	ip$=Input("Ip: ")
	stream=OpenTCPStream(ip,45211)
End If
;From this point you can use the same code for both computers



rdodson41(Posted 2004) [#6]
Whichever way works for you im sure is good, thats just the way i like to do it. Servers seem easier so that you dont have to deal with multiple ip addresses.

And yes textareas are blitzplus, which i thought you were using. Guess not.

For ports, i use 10000, use something up there and it wont conflict with other progs.

And i c what your doing with the ;'s i just didnt get it.

good luck with your prog! Unfortunatly you wil be competing with my RichChat! ;)


mrmango(Posted 2004) [#7]
Compete? Ahh well, I am not wanting to develop a full APP as it were. I just wanted to apply my knowledge to a task like a tcp client, so I could test how information gets passed via tcp. Its mainly for my multiplayer game I am planning to do.

Yeah, I noticed less problems when allocating to higher ports thanks :-)

Mango