Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day to all..

i am having problem regarding my server and client socket forms.
(each form is on separate project)

these scripts are basic form on how they are connecting

SERVER script :

VB
Imports System.Net, System.Net.Sockets
Public Class frmServer
    Dim server As Socket
    Dim client As Socket
    Dim bytes As Byte() = New Byte(1023) {}
    Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        server = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim xEndpoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 6969)
        server.Bind(xEndpoint)
        server.Listen(2)
        server.BeginAccept(New AsyncCallback(AddressOf OnAccept), vbNull)
    End Sub
    Private Sub OnAccept(ByVal ar As IAsyncResult)
        client = server.EndAccept(ar)
        client.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
    End Sub
    Private Sub OnRecieve(ByVal ar As IAsyncResult)
        client = ar.AsyncState
        client.EndReceive(ar)
        client.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
        Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
        MessageBox.Show(message)
    End Sub
End Class



Client Script :

Imports System.Net, System.Net.Sockets
Imports System.Text.ASCIIEncoding
Public Class frmClient
    Dim client As Socket
    Dim host As String = "172.22.4.75"
    Dim port As Integer = "6969"
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim IP As IPAddress = IPAddress.Parse(host)
        Dim xIpEndPoint As IPEndPoint = New IPEndPoint(IP, port)
        client.BeginConnect(xIpEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)
        btnConnect.Enabled = False
    End Sub
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim bytes As Byte() = ASCII.GetBytes(txtMessage.Text)
        client.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
    End Sub
    Private Sub OnConnect(ByVal ar As IAsyncResult)
        client.EndConnect(ar)
        MessageBox.Show("Connected")
    End Sub
    Private Sub OnSend(ByVal ar As IAsyncResult)
        client.EndSend(ar)
    End Sub


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


all connections are doing fine even sending date from client to server but after i hit the button1(client.close) and exiting the client form the server is looping this part of the code

VB
Private Sub OnRecieve(ByVal ar As IAsyncResult)
    client = ar.AsyncState
    client.EndReceive(ar)
    client.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
    Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
    MessageBox.Show(message)
End Sub


and sending many signal even thou the connection is closed. if i removed the this part

client.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)


the connection and client form will close without any looping but the problem that i will encountering is that the server will only be receiving 1 data and will not receive any message anymore.
Posted
Comments
Mr.TMG 27-Oct-13 1:35am    
The work done at this link is very nice and helped me to understand a lot of the issues that i was having. I was even able to use it to make some nice user controls so that all transmissions could be handled in a very thread safe manner. Give it a look.
http://www.codeproject.com/Articles/307315/Reusable-multithreaded-tcp-client-and-server-class
Mr.TMG 27-Oct-13 1:37am    
Actually, I'll go ahead and post my user controls when I figure out how to do it.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900