Socket in Window Application

onepieceking

Well-known member
Joined
Sep 20, 2006
Messages
64
Programming Experience
1-3
Hi,

How do I create a server for a window application? I can create a console application and the console works good. But when I import my code into a window application, the whole application just hangs there.

VB.NET:
Public sub Form1_load(Byval sender as object, byval e as system.eventargs) handles mybase.load
' Must listen on correct port- must be same as port client wants to connect on.
        Const portNumber As Integer = 8000
        Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
        ServerStatus.text = "Waiting for connection..."
        Try
            'Accept the pending client connection and return 
            'a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            ClientDatas.text = Connection accepted."
            ' Get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            ' Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Return the data received from the client to the console.
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            ClientDatas.text = clientdata
            Dim responseString As String = "Connected to server."
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ServerString.Text = responseString
            'Any communication with the remote client using the TcpClient can go here.
            'Close TcpListener and TcpClient.
            tcpClient.Close()
            tcpListener.Stop()
            Console.WriteLine("exit")
            Console.ReadLine()
        Catch e As Exception
            Console.WriteLine(e.ToString())
            Console.ReadLine()
        End Try
end sub
 
Loose the Console class in winforms applications, use Debug class instead for debugging output. Else there don't seem to be any problem, looks like the code is just waiting for a client to connect. The form is not shown until its Load event handler is finished executing.
 
Hi John,

How do I implement the window application to listen for connection and at the same time, the user is able to do some other tasks with the application?

From my example, the tcplistener is waiting to accept an incoming connection, which also block other execution.

Please advise me on this. Thank you so much!!
 
Hi,

Code for my window application:

VB.NET:
Public Class Form1
Dim tListener as New TcpListener
... ... ...
 
Private Sub Form1_load(byval sender as object, byval e as eventargs) handles mybase.load
tlistener.start()
me.show()
end sub
 
Private Sub Form1_show(byval sender as object, byval e as eventargs) handles mybase.activate
TestListener()
end sub
 
Sub TestListener()
if tListener.Pending() then
   #Read from networkstream and display to textbox1#
   #Write something to networkstream and display on textbox1#
end if
end sub
end class

The client is from a web page which will refresh every 2 seconds and at every refresh, it will write to the window application and getting the data from window application.

the problem is the textbox1 text doesn't seem to update. I am testing everything on the same computer. I have to minimise the window application and maximise it to get the client's data. If I leave it running alone, it will not update.

Please advise me on this. thanks
 
I have solved it. I have to use a timer to make the tcplistener to keep polling for new incoming connections. In this way, it will work fine
 
Asynchronous Sockets

The reason it works in a Timer is because the Timer runs in a separate thread from the main application thread. You can accomplish the same thing using any threading techniques. Check out the MSDN documentation on asynchronous sockets, they have some examples of servers that don't block program execution.
 
Back
Top