Page 1 of 1

LAN Chat 1.0

Posted: Sun Oct 09, 2005 4:13 am
by Joakim Christiansen
I'm making a little chat program just for fun.
It's not full of stupid features, it's just a super simple chat program for use over a local area network.
Feel free to test it and report bugs! :wink:
Click here to download it.
Click here to download the source code.

Some screenshots: (i'm talking with myself on those :lol:)
Image
Image

1.0 Beta 1:
Fist release.
1.0:
Fixed: When receive new message; scroll all the way down if it's not.

Re: LAN Chat 1.0 Beta 1

Posted: Sun Oct 09, 2005 4:53 am
by Phoenix
No source? :(

Posted: Sun Oct 09, 2005 6:10 am
by okasvi
too simple to give source :P

if you want i can make similar chat-server/client for you...

Re: LAN Chat 1.0 Beta 1

Posted: Sun Oct 09, 2005 2:13 pm
by Joakim Christiansen
Phoenix wrote:No source? :(
Here, download the source code! :wink:

Re: LAN Chat 1.0 Beta 1

Posted: Mon Oct 10, 2005 12:10 pm
by Phoenix
Joakim Christiansen wrote:
Phoenix wrote:No source? :(
Here, download the source code! :wink:
Thank-you, I shall study this well!

Posted: Tue Oct 11, 2005 8:58 pm
by Joakim Christiansen
Well, since none of you reported any bugs and I didn't find any myself, I now release version 1.0.

New since beta version:
Fixed: When receive new message; scroll all the way down if it's not.

Edit:
I hust removed the size gadget, I don't know how it got there... :oops:

Posted: Sun Feb 26, 2006 1:32 am
by Dare2
Hi Joakim,

Just found this, neat! Thank you.

Did a quick and makeshift convert to PB4 b4 and it ran sweet.

Couple of (perhaps dumb) questions:

1: How do you stop the server?
2: Is there a simple way to transfer files from one comp to another
.... Send (You dump to their disk in, say, a special folder)
..... Fetch (You get from a folder in their disk)

PB 4.0 Final Code

Posted: Fri May 12, 2006 2:34 am
by Shannara
PB 4.0 Final/jaPBe v3 code posted for those in need :)

Code: Select all

; -------------------------------------------------
; Name:        LAN Chat 1.0
; Author:      Joakim L. Christiansen
; Last edited: 2005-10-11
; Comment:     Just a simple chat program for a LAN
; Updated:     05.11.2006 for PB 4.0 Final and jaPBe v3
; -------------------------------------------------

;-## Program start ##
;-Enumeration
Enumeration ;Windows
  #Main
EndEnumeration
Enumeration ;Gadgets
  #Chat
  #Message
  #Send
  #Options
  #Host
  #Join
  #ServerIP
  #JoinIP
  #TextNickname
  #Nickname
  #Frame1
  #oHost
  #oJoin
  #oNone
EndEnumeration
Enumeration ;Menues
  #Menu
EndEnumeration
Enumeration ;Menu items
  #Show
  #Exit
EndEnumeration
Enumeration ;Systray icons
  #SysIcon
EndEnumeration
Enumeration ;Fonts
  #CourierNew12Bold
EndEnumeration
Enumeration ;Status
  #None
  #Server
  #Client
EndEnumeration
#NewLine = Chr(13)+Chr(10)

;-Fonts
LoadFont(#CourierNew12Bold,"Courier New",10,#PB_Font_Bold)

;-Open window
If OpenWindow(#Main,0,0,230,135,"LAN Chat v1.0",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
  If CreateGadgetList(WindowID(#Main))
    EditorGadget(#Chat,5,5,200,100)
    SendMessage_(GadgetID(#Chat),#EM_SETREADONLY,#True,0)
    StringGadget(#Message,5,110,175,20,"")
    ; No longer Available -> ActivateGadget(#Message)
    ButtonGadget(#Send,185,110,40,20,"Send")
    
    ButtonGadget(#Options,210,5,15,100,"OPTIONS",#PB_Button_Default|#PB_Button_MultiLine|#PB_Button_Toggle)
    
    ButtonGadget(#Host,230,85,90,20,"Host chat")
    ButtonGadget(#Join,230,110,90,20,"Join Chat")
    StringGadget(#ServerIP,320,85,90,20,"",#PB_String_ReadOnly)
    IPAddressGadget(#JoinIP,320,110,90,20)
    
    TextGadget(#TextNickname,230,60,85,20,"Nickname:",#PB_Text_Center)
    SetGadgetFont(#TextNickname,FontID(#CourierNew12Bold))
    StringGadget(#Nickname,320,60,90,20,"Nicname")
    
    Frame3DGadget(#Frame1,230,5,180,45,"Startup action:")
    OptionGadget(#oHost,240,25,45,15,"Host")
    OptionGadget(#oJoin,295,25,45,15,"Join")
    OptionGadget(#oNone,350,25,45,15,"None")
    
    ;Tooltips
    GadgetToolTip(#Message,"Enter your message here.")
    GadgetToolTip(#Send,"Click to send your message.")
    GadgetToolTip(#Options,"Click to show/hide options.")
    GadgetToolTip(#oHost,"Host a chat server on startup.")
    GadgetToolTip(#oJoin,"Join a chat server on startup.")
    GadgetToolTip(#oNone,"Do nothing on startup.")
    GadgetToolTip(#Nickname,"Enter your nickname here.")
    GadgetToolTip(#Host,"Click to create your own chat server.")
    GadgetToolTip(#Join,"Click to join an existing chat server.")
    GadgetToolTip(#ServerIP,"This is your local ip address.")
    GadgetToolTip(#JoinIP,"The ip address you would like to join.")
  Else
    MessageRequester("WTF!","CreateGadgetList() Failed!",#MB_ICONERROR)
    End
  EndIf
  If CreatePopupMenu(#Menu)
    MenuItem(#Show,"Show")
    MenuItem(#Exit,"Exit")
  EndIf
Else
  MessageRequester("WTF!","OpenWindow() Failed!",#MB_ICONERROR)
  End
EndIf

;-Initialize the network
If InitNetwork() = #False
  MessageRequester("WTF!","InitNetwork() Failed!"+#NewLine+"No TCP/IP stack available on the system.",#MB_ICONERROR)
  End
EndIf

;-Get local ip address
ExamineIPAddresses()
SetGadgetText(#ServerIP,IPString(NextIPAddress()))

;-Variables
Global NewList ClientID.l()
Global Status.b, Port.w, ServerID.l
*Buffer.l = AllocateMemory(1000)
DataLength.l
String.s
Icon.l = ExtractIcon_(WindowID(#Main),"LAN Chat.exe",0)
Minimized.b
EndProgram.b
Global mServer.l

Procedure Disconnect()
  Select Status
    Case #Server
      ForEach ClientID()
        SendNetworkString(ClientID(),GetGadgetText(#Nickname)+" (server) disconnected.")
      Next
      CloseNetworkServer(mServer)
    Case #Client
      SendNetworkString(ServerID,GetGadgetText(#Nickname)+" disconnected.")
      CloseNetworkConnection(ServerID)
  EndSelect
EndProcedure
Procedure Host()
  Disconnect()
  mServer = CreateNetworkServer(#PB_Any, Port)
  If mServer
    Status = #Server
  Else
    MessageRequester("WTF!","Can't create server! (port in use?)",#MB_ICONWARNING)
  EndIf
EndProcedure
Procedure Join()
  Disconnect()
  ServerID = OpenNetworkConnection(GetGadgetText(#JoinIP),Port)
  If ServerID
    Status = #Client
    SendNetworkString(ServerID,GetGadgetText(#Nickname)+" connected.")
  Else
    MessageRequester("WTF!","Can't join the server!"+#NewLine+"Is the server running?",#MB_ICONWARNING)
  EndIf
EndProcedure

;-Read config file
OpenPreferences("Config.ini")
  SetGadgetText(#Nickname,ReadPreferenceString("Nickname","MyName"))
  String = ReadPreferenceString("JoinIP","0.0.0.0")
  SetGadgetState(#JoinIP,MakeIPAddress(Val(StringField(String,1,".")),Val(StringField(String,2,".")),Val(StringField(String,3,".")),Val(StringField(String,4,"."))))
  Port = ReadPreferenceLong("Port",6010)
  Select ReadPreferenceLong("StartUpAction",0)
    Case 0: SetGadgetState(#oNone,#True)
    Case 1
      SetGadgetState(#oHost,#True)
      Host()
    Case 2
      SetGadgetState(#oJoin,#True)
      Join()
  EndSelect
ClosePreferences()

Repeat
  Delay(4)
  
  ;-Minimized/not minimized
  If IsIconic_(WindowID(#Main)) = #True And Minimized = #False
    Minimized = #True
    HideWindow(#Main,#True)
    AddSysTrayIcon(#SysIcon,WindowID(#Main),Icon)
    SysTrayIconToolTip(#SysIcon,"LAN Chat")
  ElseIf IsIconic_(WindowID(#Main)) = #False And Minimized = #True
    Minimized = #False
    RemoveSysTrayIcon(#SysIcon)
  EndIf
  
  WindowEvent = WindowEvent()
  
  ;-## Window events ##
  Select WindowEvent
    Case #PB_Event_SizeWindow
      
    Case #PB_Event_CloseWindow ;-Close window
      EndProgram = #True
    Case #PB_Event_SysTray ;-Systray click
      If EventType() = #PB_EventType_RightClick
        DisplayPopupMenu(#Menu,WindowID(#Main))
      ElseIf EventType() = #PB_EventType_LeftDoubleClick
        ShowWindow_(WindowID(#Main),#SW_MINIMIZE)
        Delay(100)
        ShowWindow_(WindowID(#Main),#SW_RESTORE)
      EndIf
    Case #PB_Event_Menu ;-Systray menu
      Select EventMenu()
        Case #Show
          ShowWindow_(WindowID(#Main),#SW_MINIMIZE)
          Delay(100)
          ShowWindow_(WindowID(#Main),#SW_RESTORE)
        Case #Exit
          EndProgram = #True
      EndSelect
    Case #WM_KEYDOWN ;-Enter key pressed inside #Message
      If EventwParam()=#VK_RETURN And GetFocus_()=GadgetID(#Message)
        PostMessage_(WindowID(#Main),#WM_COMMAND,#PB_EventType_LeftClick,GadgetID(#Send)) 
      EndIf
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Options ;-Options button
          If GetGadgetState(#Options)
            ResizeWindow(#Main, WindowX(#Main), WindowY(#Main), 415,135)
          Else
            ResizeWindow(#Main, WindowX(#Main), WindowY(#Main), 230,135)
          EndIf
        Case #Host: Host() ;-Host button
        Case #Join: Join() ;-Join button
        Case #Send ;-Send button
          If GetGadgetText(#Message) > ""
            Select Status
              Case #Server
                AddGadgetItem(#Chat,-1,GetGadgetText(#Nickname)+": "+GetGadgetText(#Message))
                SendMessage_(GadgetID(#Chat),#EM_SCROLL,#SB_BOTTOM,0)
                ForEach ClientID()
                  SendNetworkString(ClientID(),GetGadgetText(#Nickname)+": "+GetGadgetText(#Message))
                Next
              Case #Client
                SendNetworkString(ServerID,GetGadgetText(#Nickname)+": "+GetGadgetText(#Message))
            EndSelect
            SetGadgetText(#Message,"")
          EndIf
      EndSelect
  EndSelect
  
  ;-## Network events ##
  Select Status
    Case #Server
      Select NetworkServerEvent()
        Case 1 ;-Client connected
          AddElement(ClientID())
          ClientID() = EventClient()
        Case 2 ;-Server receive data
          DataLength = ReceiveNetworkData(EventClient(),*Buffer,1000)
          AddGadgetItem(#Chat,-1,PeekS(*Buffer,DataLength))
          SendMessage_(GadgetID(#Chat),#EM_SCROLL,#SB_BOTTOM,0)
          ForEach ClientID()
            SendNetworkString(ClientID(),PeekS(*Buffer,DataLength))
          Next
          If Minimized
            ShowWindow_(WindowID(#Main),#SW_MINIMIZE)
            Delay(100)
            ShowWindow_(WindowID(#Main),#SW_RESTORE)
          EndIf
        Case 4 ;-Client disconnected
          ForEach ClientID()
            If ClientID() = EventClient()
              DeleteElement(ClientID())
            EndIf
          Next
      EndSelect
    Case #Client
      Select NetworkClientEvent(ServerID)
        Case 2 ;-Client receive data
          DataLength = ReceiveNetworkData(ServerID,*Buffer,1000)
          AddGadgetItem(#Chat,-1,PeekS(*Buffer,DataLength))
          SendMessage_(GadgetID(#Chat),#EM_SCROLL,#SB_BOTTOM,0)
          If Minimized
            ShowWindow_(WindowID(#Main),#SW_MINIMIZE)
            Delay(100)
            ShowWindow_(WindowID(#Main),#SW_RESTORE)
          EndIf
      EndSelect
  EndSelect
Until EndProgram

;-## End program ##
;-Disconnect
Disconnect()
;-Save config file
If CreatePreferences("Config.ini")
  If GetGadgetState(#oNone)
    WritePreferenceLong("StartUpAction",0)
  ElseIf GetGadgetState(#oHost)
    WritePreferenceLong("StartUpAction",1)
  ElseIf GetGadgetState(#oJoin)
    WritePreferenceLong("StartUpAction",2)
  EndIf
  WritePreferenceString("Nickname",GetGadgetText(#Nickname))
  WritePreferenceString("JoinIP",GetGadgetText(#JoinIP))
  WritePreferenceLong("Port",Port)
  ClosePreferences()
Else
  MessageRequester("WTF!","Can't save config file! (write protected?)",#MB_ICONWARNING)
EndIf
 
; jaPBe Version=3.6.4.503
; FoldLines=000A000C000D001C001D001F002000230024002600270029002A002E
; Build=1
; Language=0x0000 Language Neutral
; FirstLine=224
; CursorPosition=278
; EnableAsm
; EnableThread
; UseIcon=Icon.ico
; ExecutableFormat=Windows
; SaveDeclare
; EOF

Posted: Fri May 12, 2006 2:56 am
by Joakim Christiansen
Shannara :D

I actually have a updated version of it on my computer, but I didn't finish it so I wont share it...
But i'm making a new one, a much bether one! :D

Posted: Fri May 12, 2006 2:57 am
by Shannara
I'll definately be interested in viewing that when ready :) I'm currently working on the socket tools version of the code above, will post if there is an interest :)

Posted: Fri May 12, 2006 3:02 am
by Straker
You say this is for a LAN, but why wont this work over a WAN? As long as the Ports are open, why not? Just asking.

Posted: Fri May 12, 2006 3:20 am
by Shannara
Well, he did mention a WAN, or internet it would work, as long as the ports are not blocked :) Thats mainly the culpit especially with Windows XP firewall automatically turned on for network connections. :)

That is my guess, anyways.

Posted: Fri May 12, 2006 3:20 am
by Joakim Christiansen
Straker wrote:You say this is for a LAN, but why wont this work over a WAN? As long as the Ports are open, why not? Just asking.
It's made to be used over a local area network, but it also works over internet. But certainly not for everyone!
The mess with todays routers and stuff makes it a pain in the ass!
If I want it to be used over internet then I would have to make it easy for everyone (like msn), now only "experts" can make it work.
And I myself couldn't make it work even with my ports open...

Posted: Fri May 12, 2006 3:30 am
by Shannara
oo, there are Pix as well, those dreadfull buggers and routers who like holding on to packets so long, that everything timesout ... a real pain. Some 3rd party libraries help overcome this :) Thus the reason for me redoing the code for use with ST. Thanks for the answer :)