Results 1 to 2 of 2

Thread: Client/Server Application

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    1

    Cool Client/Server Application

    Hello,

    Im new to .Net, I am wanting to create a Client and Server Application.
    Client on a Hand Held Device and Server on a desktop computer.

    I have some code I have already seen on the internet and modified. I am having some problems with this code.

    First Question is just to make sure I am going about this right way?
    Is using TCP connections the best way to do this?

    Second question is I can get the client side to connect to the server, once there is a period of inactivity for around 5 - 10 mins the server crashes.

    Is there anything I can do about this?




    Server Code
    -----------------------
    Imports System.Threading

    Module Module1

    Public Sub Main()
    Dim mySrv As sserver = New sserver(2001)
    Dim tsrv As Thread = New Thread(AddressOf mySrv.startServer)
    tsrv.Start()
    End Sub

    End Module




    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Text
    Imports Microsoft.VisualBasic.ControlChars

    Public Class sserver
    Private ASCII As Encoding = Encoding.ASCII
    Private SListener As TcpListener
    Private port As Integer
    Private ReadOnly MAXCLIENT As Integer = 10
    Private Shared ClientCount As Integer
    Private ServerSocketForClientThread As Socket
    Public Sub New(ByVal port As Integer)
    MyBase.New()
    Me.port = port
    End Sub
    Public Sub startServer()
    Console.WriteLine("Trying to Start Server...")
    SListener = New TcpListener(port)
    'Start the Server
    SListener.Start()
    'Wait for Client to Connect
    While (True)
    Try
    'Socket on which Server will Connect and Talk with Client
    Console.WriteLine("Server Started waiting for Client Connection...")
    Dim ServerSocketForClient As Socket = SListener.AcceptSocket
    'If the Client Starts the Communication
    'Create a NEW Thread for Each Client
    'Increase the ClientCount
    ClientCount += 1
    If ClientCount <= MAXCLIENT Then
    'Get the Socket on which
    'Server and Client will Talk Within ClientThread
    ServerSocketForClientThread = ServerSocketForClient
    Console.WriteLine("Client Connection {0} from IP {1}...", ClientCount, ServerSocketForClient.RemoteEndPoint())
    Dim ClientThread As Thread = New Thread(AddressOf CreateClientThread)
    'Start Processing of client Thread
    ClientThread.Start()
    Else
    'Make the Count Stable So the Operation of Existing Client is Performed
    ClientCount -= 1
    End If
    Catch e As NotSupportedException
    Console.WriteLine(e.ToString)
    End Try
    End While

    End Sub

    Private Sub CreateClientThread()
    Dim ServerSocketForClient As Socket
    ServerSocketForClient = ServerSocketForClientThread
    'Wait for client to finish
    While (True)
    'Wait for Client Command on this socket

    Dim byteArray(2048) As Byte
    Try
    Dim iBytes As Integer = ServerSocketForClient.Receive(byteArray, byteArray.Length, 0)
    Dim MessageFromClient As String = Encoding.ASCII.GetString(byteArray)

    If iBytes > 0 Then
    SendMessageToClient(ServerSocketForClient, "From #" & ServerSocketForClient.RemoteEndPoint().ToString & "# " & MessageFromClient & CrLf)
    Console.WriteLine("From #" & ServerSocketForClient.RemoteEndPoint().ToString & "# " & MessageFromClient)
    Else
    Exit While
    End If
    Catch e As NotSupportedException
    Console.WriteLine(e.ToString)
    End Try
    End While
    Console.WriteLine("Client Thread {0} From {1} Exited ", ClientCount, ServerSocketForClient.RemoteEndPoint())
    'Decrement the Global Client Count
    ClientCount -= 1
    'Close the Socket
    ServerSocketForClient.Close()
    End Sub

    Private Sub SendMessageToClient(ByVal SocketToUse As Socket, ByVal MessageToSend As String)
    Dim ByteArray() As Byte = ASCII.Getbytes(MessageToSend.ToCharArray())
    Try
    SocketToUse.Send(ByteArray, ByteArray.Length, 0)
    Catch e As SocketException

    End Try
    End Sub
    End Class




    Client Code
    ---------------------------
    Imports System.Text
    Imports System.Net.Sockets
    'Imports VB = Microsoft.VisualBasic

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Private tClient As New sclient

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    tClient.sendmessagetoserver(TextBox2.Text)
    If Not tClient.connected Then
    Label1.Text = "disconnected"
    Button1.Enabled = True
    Button2.Enabled = False
    Button3.Enabled = False
    End If

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try
    tClient.connect(CInt(TextBox3.Text), TextBox1.Text)
    Label1.Text = "Connected"
    Button1.Enabled = False
    Button2.Enabled = True
    Button3.Enabled = True
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try

    End Sub
    Protected Overrides Sub Finalize()
    MyBase.Finalize()
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    Application.Exit()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Application.Exit()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Try
    tClient.disconnect()
    Label1.Text = "Disconnected"
    Button1.Enabled = True
    Button2.Enabled = False
    Button3.Enabled = False
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    End Sub
    End Class

    Thank you in advanced for any help.

  2. #2
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: Client/Server Application

    you should place your code snippets inside vbcode tags so it's easier to read the code.

    perhaps you should look into Asynchronous Sockets
    Asynchronous Server Socket Example
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width