Click here to Skip to main content
12,747,078 members (29,634 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 3 Jun 2005

No RFC Chatserver With .NET Client, Or Java Client

, 3 Jun 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
A chat server that can be setup as a service or application. Opens listening port and awaits connections from Java clients or .NET clients. You could setup even a HTML refresh with a little JavaScript knowhow. I've included the server only because I would like some feedback on the way I do threads.


Hello, name's Bobby. Was curious about the way I am doing threading on this app? I know we've all seen by now the Cassini web server Microsoft put out. I have worked in the internet website/video/chat industry for years. I was curious when .NET first came out, if it were possible to create a video server, and chat server in VB.NET. This would become my first dealings with doing threading outside of Java or really crappy VB controls that did threading.

I have since completed both servers in VB.NET. The first was the video server using server push JPG images streamed to a simple Java applet. This server was a better solution to video as my prior solution required an Apache build for Windows, and a friend who knew a lot of C++ but little MFC. :) However in the end I believe both the Apache server and my video server had one fatal problem, the way they are threaded?

This project contains one chat server currently inside a .NET application. It of course will end its run in a service form but I am still testing. Unlike Apache, and my previous servers, this one does not preload 100-200-800 threads to serve clients, store images/chat text. This one uses Socket.BeginRecieve and Socket.BeginConnect and Socket.BeginAccept etc... As I assume you all know, these are asynchronous calls. For those who don't know, these socket functions don't block, but rather use a callback function/sub when they are done with executing. Supposedly there is some sort of Windows API for IO W/ sockets and drives and even memory. I am curious to see if this will hold up to 1000 clients all queuing server for new lines, bans, kicks, words etc.....

Please if you are interested in this, feel free to contact me, I am also working on a ASYNC Video server and video CAPTURE program and have written the Java clients for both, including a Java client that receives and displays live video but allows you to TIVO like rewind and pause etc...

Here are some source snips:

Private Sub Recieved(ByVal AR As IAsyncResult)
    Dim inBytes As Long
        inBytes = mySock.EndReceive(AR)
    Catch 'Socket Error 'We Close f***er...
        Console.WriteLine("Socket Closed")
        myState = ClientStates.Idle
        mySock.Close() : LeaveRoom()
        Exit Sub
    End Try
    rBufLen += inBytes
    rString = System.Text.ASCIIEncoding.ASCII.GetString(rBuffer, _
                                                     0, rBufLen)
    If inBytes <= 0 Then
        Console.WriteLine("Socket Closed")
        mySock.Close() : LeaveRoom()
        Exit Sub
    End If
    If myState = ClientStates.Sending Then myState = myStateLast
    If Not rString.IndexOf("<") >= 0 And _
                             rString.IndexOf(">") >= 2 Then
        Exit Sub
    End If
    SyncLock Me
        LastTimeHeard = Now
        If Not IsNothing(myUser) Then 
           Console.WriteLine("Setting " & myUser.Username & Now)
    End SyncLock
    Select Case myState
        Case ClientStates.WaitingCmd
            If IsCmdDone() Then
                If DEBUGME Then
                    Console.WriteLine("RECIEVED: " & rString)
                End If
                If DEBUGME Then
                    'Console.WriteLine("Got Some Data: " & rString)
                End If
                mySock.BeginReceive(rBuffer, rBufLen, 2048, 
                                      SocketFlags.None, RecievedCB, Me)
            End If
        Case Else
    End Select
End Sub
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Runtime.InteropServices
Public Class ServerListen
    Private mySock As Socket
    Private SockConnectCB As AsyncCallback
    Private sockMode As Long
    Public Sub New(ByVal IP As String, ByVal Port As Integer, _ 
                                        ByVal Mode As Long)
        mySock = New Socket(AddressFamily.InterNetwork, _
                           SocketType.Stream, ProtocolType.Tcp)
        mySock.SetSocketOption(SocketOptionLevel.Socket, _
                                 SocketOptionName.KeepAlive, 1)
        mySock.Bind(New IPEndPoint(IPAddress.Parse(IP), Port))
        SockConnectCB = AddressOf SockConnect
        sockMode = Mode
        mySock.BeginAccept(SockConnectCB, Me)
    End Sub
    Public Sub SockConnect(ByVal AR As IAsyncResult)
            Dim S As Socket
            Dim C As Client
            'Dim sIP As String
            S = mySock.EndAccept(AR)
            S.SetSocketOption(SocketOptionLevel.Socket, _
                                 SocketOptionName.DontLinger, 1)
            S.SetSocketOption(SocketOptionLevel.Socket, _
                                  SocketOptionName.KeepAlive, 1)
            C = New Client
            Select Case sockMode
                Case ClientSockModes.modeADMIN
                    C.isADMIN = True
                Case ClientSockModes.modeHTTP
                    C.isHTTP = True
                Case ClientSockModes.modeCHAT
                    C.isCHAT = True
            End Select
            mySock.BeginAccept(SockConnectCB, mySock)
            SyncLock ezClients.SyncRoot
                If Not ezClients.ContainsKey(C.ThreadId) Then _
                         'This Guys f***ING WIth You, DUMP This Thread
                    ezClients.Add(C.ThreadId, C)
                End If
            End SyncLock
            'End If
        Catch ex As Exception
            If DEBUGME Then
                MsgBox("ERROR In ServerListen: " & ex.ToString())
            End If
        End Try
    End Sub
End Class

I would be happy to share any code with those who have comments, wish to see it, or are developers working in the same field. If the code I post here is good enough to steal without contacting me in any way, then let's just say I am flattered, and happy stealing. However those looking for a quick download and work solution to internet chat are in for a surprise :) This server uses a non-RFC way of two way communication so it requires a client.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
Programmer, C#, VB.Net/VB, ASP.Net/ASP, PHP, PERL, Java

You may also be interested in...

Comments and Discussions

QuestionServer less LAN Chat Pin
fred033020-Apr-14 21:10
memberfred033020-Apr-14 21:10 
SuggestionWould've been helpfull.... Pin
Bill Doerfler30-Jul-12 6:38
memberBill Doerfler30-Jul-12 6:38 
NewsFor any that still read this article. Pin
supernova566627-Feb-11 22:11
membersupernova566627-Feb-11 22:11 
Generalcode for voice and text chat private and conference Pin
irfa.146963-Mar-10 4:15
memberirfa.146963-Mar-10 4:15 
GeneralChat Server pr0ject source code Need in J2SE Pin
sur20-Feb-08 18:16
membersur20-Feb-08 18:16 
GeneralSample System based projects inJava / J2EE Pin
sanu_8213-Nov-07 21:14
membersanu_8213-Nov-07 21:14 
GeneralHi Dude Pin
kompellakishore4-Jan-07 5:32
memberkompellakishore4-Jan-07 5:32 
GeneralSettings.xml is missing Pin
andreiisro20067-Dec-06 7:49
memberandreiisro20067-Dec-06 7:49 
GeneralSettings.xml is missing Pin
megadream20067-Aug-06 2:55
membermegadream20067-Aug-06 2:55 
QuestionA Video Chat Server... Pin
TintinLille17-May-06 9:39
memberTintinLille17-May-06 9:39 
Generalcreating chat server application in C# or Asp.Nrt Pin
vivekdeshpande7-Mar-06 1:15
membervivekdeshpande7-Mar-06 1:15 
GeneralSocketOptionName.KeepAlive Pin
Vitoto26-Jan-06 20:30
memberVitoto26-Jan-06 20:30 
QuestionHow did the test go? Pin
Thomas Lykke Petersen5-Jun-05 22:53
memberThomas Lykke Petersen5-Jun-05 22:53 
AnswerRe: How did the test go? Pin
supernova56665-Jun-05 23:12
membersupernova56665-Jun-05 23:12 
GeneralRe: How did the test go? Pin
TheDarkMan6-Jun-05 21:41
memberTheDarkMan6-Jun-05 21:41 
GeneralRe: How did the test go? Pin
supernova56666-Jun-05 21:56
membersupernova56666-Jun-05 21:56 
GeneralRe: How did the test go? Pin
TheDarkMan6-Jun-05 22:54
memberTheDarkMan6-Jun-05 22:54 
GeneralRe: How did the test go? Pin
supernova56666-Jun-05 23:32
membersupernova56666-Jun-05 23:32 
GeneralRe: How did the test go? Pin
erman.olca29-Sep-05 3:48
membererman.olca29-Sep-05 3:48 
GeneralRe: How did the test go? Pin
rjn30-Nov-05 6:27
memberrjn30-Nov-05 6:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 4 Jun 2005
Article Copyright 2005 by supernova5666
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid