Click here to Skip to main content
15,884,237 members
Articles / Multimedia / GDI+
Article

No RFC Chatserver With .NET Client, Or Java Client

Rate me:
Please Sign up or sign in to vote.
2.18/5 (6 votes)
3 Jun 2005CPOL2 min read 70.3K   646   15   20
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.

Introduction

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:

VB
Private Sub Recieved(ByVal AR As IAsyncResult)
    Dim inBytes As Long
    Try
        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)
                    DoCmd()
                End If
            Else
                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
VB
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))
        mySock.Listen(7)
        SockConnectCB = AddressOf SockConnect
        sockMode = Mode
        mySock.BeginAccept(SockConnectCB, Me)
    End Sub
    Public Sub SockConnect(ByVal AR As IAsyncResult)
        Try
            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
            C.Accept(S)
            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.

License

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


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

Comments and Discussions

 
QuestionServer less LAN Chat Pin
fred033020-Apr-14 20:10
fred033020-Apr-14 20:10 
SuggestionWould've been helpfull.... Pin
Bill Doerfler30-Jul-12 5:38
Bill Doerfler30-Jul-12 5:38 
NewsFor any that still read this article. Pin
supernova566627-Feb-11 21:11
supernova566627-Feb-11 21:11 
Generalcode for voice and text chat vb.net private and conference Pin
irfa.146963-Mar-10 3:15
irfa.146963-Mar-10 3:15 
GeneralChat Server pr0ject source code Need in J2SE Pin
sur20-Feb-08 17:16
sur20-Feb-08 17:16 
hiiii


I need Chat Server Project source code in J2SE for Project Summison in my College.
PLEASE HELP ME.

SUR
GeneralSample System based projects inJava / J2EE Pin
sanu_8213-Nov-07 20:14
sanu_8213-Nov-07 20:14 
GeneralHi Dude Pin
kompellakishore4-Jan-07 4:32
kompellakishore4-Jan-07 4:32 
GeneralSettings.xml is missing Pin
andreiisro20067-Dec-06 6:49
andreiisro20067-Dec-06 6:49 
GeneralSettings.xml is missing Pin
megadream20067-Aug-06 1:55
megadream20067-Aug-06 1:55 
QuestionA Video Chat Server... Pin
TintinLille17-May-06 8:39
TintinLille17-May-06 8:39 
Generalcreating chat server application in C# or Asp.Nrt Pin
vivekdeshpande7-Mar-06 0:15
vivekdeshpande7-Mar-06 0:15 
GeneralSocketOptionName.KeepAlive Pin
Vitoto26-Jan-06 19:30
Vitoto26-Jan-06 19:30 
QuestionHow did the test go? Pin
Thomas Lykke Petersen5-Jun-05 21:53
Thomas Lykke Petersen5-Jun-05 21:53 
AnswerRe: How did the test go? Pin
supernova56665-Jun-05 22:12
supernova56665-Jun-05 22:12 
GeneralRe: How did the test go? Pin
TheDarkMan6-Jun-05 20:41
TheDarkMan6-Jun-05 20:41 
GeneralRe: How did the test go? Pin
supernova56666-Jun-05 20:56
supernova56666-Jun-05 20:56 
GeneralRe: How did the test go? Pin
TheDarkMan6-Jun-05 21:54
TheDarkMan6-Jun-05 21:54 
GeneralRe: How did the test go? Pin
supernova56666-Jun-05 22:32
supernova56666-Jun-05 22:32 
GeneralRe: How did the test go? Pin
erman.olca29-Sep-05 2:48
erman.olca29-Sep-05 2:48 
GeneralRe: How did the test go? Pin
rjn30-Nov-05 5:27
rjn30-Nov-05 5: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.