Results 1 to 2 of 2

Thread: Server - Client Communication

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    2

    Unhappy Server - Client Communication

    ok. here, what I want to establish here is these scenario.

    a.the client must connect to the server. and the client can specify values that would be sent.
    b.the server would then receive the data and parse them and place them in the server form.
    c. ten clients must be connected simultaneously to the server.

    my problem is that.
    a. my server only accepts one connection at a time.(should be ten)
    b.when the client is disconnected.. the client can't connect again.
    c.the server can't detect if the client is still connected.
    d.the client is still connected even the server is out.

    here's my code.

    Server:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TCPServer1.LocalPort = 666
            TCPServer1.Listen()
            TCPServer2.LocalPort = 667
            TCPServer2.Listen()
            lblrequest.Text = "Listening at " & TCPServer1.LocalIP & " at port " & TCPServer1.LocalPort & " " & TCPServer2.LocalPort
        End Sub
    
        Private Sub TCPServer1_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles TCPServer1.ConnectionRequest
            If TCPServer1.CtlState <> MSWinsockLib.StateConstants.sckClosed Then
                TCPServer1.Close()
            End If
            TCPServer1.Accept(e.requestID)
        End Sub
    
        Private Sub TCPServer1_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles TCPServer1.DataArrival
            Dim data As String = ""
            Dim opt() As String
            TCPServer1.GetData(data)
            opt = Split(data, "||")
    
            
            If opt(0) = "connected" Then
                Judge1Stat.Text = "Judge 1 Connected!"
            End If
    
            For i As Integer = 0 To opt.Length - 1
                ListBox1.Items.Add("opt" & i & opt(i))
            Next
            If opt(0) = "AScore1" Then
                AScore11.Text = opt(1)
                AScore12.Text = opt(2)
                AScore13.Text = opt(3)
            End If
        End Sub
    
        
        Private Sub TCPServer2_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles TCPServer2.ConnectionRequest
            If TCPServer2.CtlState <> MSWinsockLib.StateConstants.sckClosed Then
                TCPServer2.Close()
            End If
            TCPServer2.Accept(e.requestID)
        End Sub
    
        Private Sub TCPServer2_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles TCPServer2.DataArrival
            Dim data As String = ""
            Dim opt() As String
            TCPServer1.GetData(data)
            opt = Split(data, "||")
    
            
            If opt(0) = "connected" Then
                Judge2Stat.Text = "Judge 2 Connected!"
            End If
    
            For i As Integer = 0 To opt.Length - 1
                ListBox1.Items.Add("opt" & i & opt(i))
            Next
            If opt(0) = "AScore1" Then
                Score1.Text = opt(1)
                Score2.Text = opt(2)
                Score3.Text = opt(3)
            End If
        End Sub

    Client
    Code:
    Public Class frmClient
        Inherits System.Windows.Forms.Form
        Public StatConn As Boolean = False
    
        Private Sub frmClient_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
            Dim a As String
            a = InputBox("Enter Password")
            If a = "password" Then
                Me.Visible=false
                frmServer.Visible = True
            Else
            End If
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tcpClient.Enabled = False
            tcpClient.Close()
            tcpClient.LocalPort = 1223
            tcpClient.Listen()
    
            Dim HostIP As String = txtHostIP.Text
            Select Case StatConn
                Case True
                    StatLabel.Text = "Connected to " & HostIP
                Case Else
                    StatLabel.Text = "Disconnected from server"
            End Select
    
            frmServer.Visible = True
        End Sub
        Private Sub panelStat_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles panelStat.Paint
            Dim p As Pen
            Dim r As Rectangle
            Dim stat As Control
            stat = panelStat
            r = New Rectangle(0, 0, 8, 8)
            Dim colorbrush As SolidBrush
            Select Case StatConn
                Case True
                    p = New Pen(System.Drawing.Color.Green)
                    colorbrush = New SolidBrush(Color.Green)
                Case Else
                    p = New Pen(System.Drawing.Color.Red)
                    colorbrush = New SolidBrush(Color.Red)
            End Select
            e.Graphics.DrawEllipse(p, r)
            e.Graphics.FillEllipse(colorbrush, r)
    
        End Sub
    
        Private Sub tmTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmTime.Tick
            If tcpClient.CtlState <> MSWinsockLib.StateConstants.sckClosed Then
                StatConn = False
            Else
                StatConn = True
            End If
    
            lblLocal.Text = "IP Address : " & tcpClient.LocalIP
            lblLocalName.Text = "Computer Name : " & tcpClient.LocalHostName
    
    
            Dim HostIP As String = txtHostIP.Text
            Select Case StatConn
                Case True
                    StatLabel.Text = "Connected to " & HostIP
                Case Else
                    StatLabel.Text = "Disconnected from server"
            End Select
    
            panelStat.Top = Me.Height - 43
            panelStat.Left = 5
        End Sub
        Private Sub tcpclient_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles tcpClient.CloseEvent
            StatConn = False
        End Sub
    
        Private Sub statStrip_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles statStrip.ItemClicked
        End Sub
        Private Sub tcpClient_ConnectEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles tcpClient.ConnectEvent
            tcpClient.SendData("connected")
        End Sub
    
        Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend1.Click
            tcpClient.SendData("AScore1" & "||" & textbox0.Text & "||" & "TextBox1.Text" & "||" & "TextBox2.Text")
        End Sub
    
        Private Sub tmPosition_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmPosition.Tick
            Grp2.Top = Grp1.Top + Grp1.Height + 10
            Grp3.Top = Grp2.Top + Grp2.Height + 10
            Grp4.Top = Grp3.Top + Grp3.Height + 10
            'Grp5.Top = Grp4.Top + Grp4.Height + 10
            'Grp6.Top = Grp5.Top + Grp5.Height + 10
            'Grp7.Top = Grp6.Top + Grp6.Height + 10
            'Grp8.Top = Grp7.Top + Grp7.Height + 10
            'Grp9.Top = Grp8.Top + Grp8.Height + 10
            'Grp10.Top = Grp9.Top + Grp9.Height + 10
            'Grp11.Top = Grp10.Top + Grp10.Height + 10
            'Grp12.Top = Grp11.Top + Grp11.Height + 10
            'Grp13.Top = Grp12.Top + Grp12.Height + 10
            'Grp14.Top = Grp13.Top + Grp13.Height + 10
        End Sub
    
        Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
            If cmdConnect.Text = "Connect" Then
                '//Initialize Connection
                On Error Resume Next
                tcpClient.Close()
                tcpClient.Connect(txtHostIP.Text, txtPort.Text)
                cmdConnect.Text = "Disconnect"
                GroupConn.Enabled = False
            Else
                tcpClient.Close()
                cmdConnect.Text = "Connect"
                GroupConn.Enabled = True
            End If
        End Sub
    End Class

    P.S. disregard other code as it is for GUI purposes only.

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    2

    Re: Server - Client Communication

    I have tried to make the client to listen from server request.

    but. how can I detect that a client is available for connecting and a port is open.

    and. after connection, the clients and server must be closed down and reopen to be able to connect again..

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