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.
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