Arokh
Well-known member
- Joined
- Apr 11, 2006
- Messages
- 124
Hi
Since I made myself comfortable in VB 05 and it is going well so far,
I want to give a try to make some chatclient & -server.
So far I accomplished, by reading through the forum and the msdn, to make a TcpListener and TcpClient by copying the code. I already made some experiments with it and changed the code to make it more useful for my purposes.
[VB]Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Public Class frmClient
Public Client As New TcpClient
Public Stream As NetworkStream
Private Sub Connect(ByVal server As [String], ByVal port As Int32)
Try
Client.Connect(server, port)
Stream = Client.GetStream
Catch e As ArgumentNullException
Debug.Print("ArgumentNullException: {0}", e)
Catch e As SocketException
Debug.Print("SocketException: {0}", e)
End Try
Debug.Print(ControlChars.Cr + " Press Enter to continue...")
End Sub
Private Sub SendMsg(ByVal Message As String)
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Message)
Stream.Write(data, 0, data.Length)
Debug.Print("Sent: {0}", Message)
End Sub
Private Sub CloseConnection()
Client.Close()
End Sub
Private Function RecieveMsg() As String
Dim Data = New [Byte](256) {}
Dim responseData As [String] = [String].Empty
Dim bytes As Int32 = Stream.Read(Data, 0, Data.Length)
responseData = System.Text.Encoding.ASCII.GetString(Data, 0, bytes)
Debug.Print("Received: {0}", responseData)
RecieveMsg = responseData
End Function
Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Connect("127.0.0.1", 13000)
SendMsg("Bla")
RecieveMsg()
CloseConnection()
End Sub
End Class[/VB]
[VB]
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Public Class frmServer
Public stream As NetworkStream
Public Sub Main()
Try
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
Dim server As New TcpListener(localAddr, port)
server.Start()
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
While True
Debug.Print("Waiting for a connection... ")
Dim client As TcpClient = server.AcceptTcpClient()
Debug.Print("Connected!")
data = Nothing
stream = client.GetStream()
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Debug.Print([String].Format("Received: {0}", data))
data = data.ToUpper()
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
stream.Write(msg, 0, msg.Length)
Debug.Print([String].Format("Sent: {0}", data))
i = stream.Read(bytes, 0, bytes.Length)
End While
End While
Catch e As SocketException
Debug.Print("SocketException: {0}", e)
End Try
Debug.Print(ControlChars.Cr + "Hit enter to continue...")
Console.Read()
End Sub Main
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Main()
End Sub
End Class[/VB]
With this I could connect to the TCPListener send some message,
then the Listener would send a message back and the client recieves it.
But this only works when each side gets an answer otherwise it waits forever.
Since I have to be able to get and send messages, for a Chatclient, simutanously
this isnt the best solution .
Ive read that I have to make an asynchronous connection,
but I couldnt find enough information to get me started.
So besides some tutorials you could link me to, I have some questions:
1. If I make an asynchronous connection can I recieve and send data/text with only one open port from more then one IP?
I guess thats the case since for programs like mIRC,
Since I made myself comfortable in VB 05 and it is going well so far,
I want to give a try to make some chatclient & -server.
So far I accomplished, by reading through the forum and the msdn, to make a TcpListener and TcpClient by copying the code. I already made some experiments with it and changed the code to make it more useful for my purposes.
[VB]Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Public Class frmClient
Public Client As New TcpClient
Public Stream As NetworkStream
Private Sub Connect(ByVal server As [String], ByVal port As Int32)
Try
Client.Connect(server, port)
Stream = Client.GetStream
Catch e As ArgumentNullException
Debug.Print("ArgumentNullException: {0}", e)
Catch e As SocketException
Debug.Print("SocketException: {0}", e)
End Try
Debug.Print(ControlChars.Cr + " Press Enter to continue...")
End Sub
Private Sub SendMsg(ByVal Message As String)
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Message)
Stream.Write(data, 0, data.Length)
Debug.Print("Sent: {0}", Message)
End Sub
Private Sub CloseConnection()
Client.Close()
End Sub
Private Function RecieveMsg() As String
Dim Data = New [Byte](256) {}
Dim responseData As [String] = [String].Empty
Dim bytes As Int32 = Stream.Read(Data, 0, Data.Length)
responseData = System.Text.Encoding.ASCII.GetString(Data, 0, bytes)
Debug.Print("Received: {0}", responseData)
RecieveMsg = responseData
End Function
Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Connect("127.0.0.1", 13000)
SendMsg("Bla")
RecieveMsg()
CloseConnection()
End Sub
End Class[/VB]
[VB]
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Public Class frmServer
Public stream As NetworkStream
Public Sub Main()
Try
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
Dim server As New TcpListener(localAddr, port)
server.Start()
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
While True
Debug.Print("Waiting for a connection... ")
Dim client As TcpClient = server.AcceptTcpClient()
Debug.Print("Connected!")
data = Nothing
stream = client.GetStream()
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Debug.Print([String].Format("Received: {0}", data))
data = data.ToUpper()
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
stream.Write(msg, 0, msg.Length)
Debug.Print([String].Format("Sent: {0}", data))
i = stream.Read(bytes, 0, bytes.Length)
End While
End While
Catch e As SocketException
Debug.Print("SocketException: {0}", e)
End Try
Debug.Print(ControlChars.Cr + "Hit enter to continue...")
Console.Read()
End Sub Main
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Main()
End Sub
End Class[/VB]
With this I could connect to the TCPListener send some message,
then the Listener would send a message back and the client recieves it.
But this only works when each side gets an answer otherwise it waits forever.
Since I have to be able to get and send messages, for a Chatclient, simutanously
this isnt the best solution .
Ive read that I have to make an asynchronous connection,
but I couldnt find enough information to get me started.
So besides some tutorials you could link me to, I have some questions:
1. If I make an asynchronous connection can I recieve and send data/text with only one open port from more then one IP?
I guess thats the case since for programs like mIRC,