Udp client used for receiving and sendin not working - vb.net

I succeded creating a UDP client-server connection and sending a large image over it. I split and sent the image into packets of 1500 bytes each .I got at the other end 330025 bytes of 353723 (which I think is pretty good). But as you know UDP packets are not in order so I have to id every packet it's sent (I can't use tcp because I need speed for my project-game). Now I want to ask the 'server' for the missing packets.
This is the server's code:
Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding
Public Class Form1
Dim ep_client As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)
Dim ep_server As New IPEndPoint(IPAddress.Any, 60000)
Dim publisher As New UdpClient(ep_client)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sendbytes() As Byte = ASCII.GetBytes(txt1.Text)
Dim img As Image, img_stream As MemoryStream, buffer As Byte()
Dim packet_size As Integer = 1024, sent_size As Long
Try
img_stream = imgToBytes(txt1.Text)
Debug.Print(img_stream.Length)
ReDim buffer(packet_size)
While Not img_stream.Position = img_stream.Length
sent_size += img_stream.Read(buffer, 0, packet_size)
publisher.Send(buffer, buffer.Length)
End While
Debug.Print(100 * sent_size / img_stream.Length & "%")
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
''When I press this button it is supposed to 'listen'
Try
'Silence...Nothing here..
publisher.BeginReceive(New AsyncCallback(AddressOf receive), publisher)
Debug.Print("listening")
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
Sub receive(ByVal ar As IAsyncResult)
''It should take the packets from coming from 'any' ip.
publisher.EndReceive(ar, ep_server)
Debug.Print("received")
publisher.BeginReceive(New AsyncCallback(AddressOf receive), publisher)
End Sub
Function imgToBytes(ByVal file_name As String) As MemoryStream
Dim img As Image = Image.FromFile(file_name)
Dim stream As New MemoryStream
img.Save(stream, Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Return stream
End Function
End Class
This is the code for the client(which becomes a server...):
Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding
Public Class Form2
Dim ep_server As IPEndPoint = New IPEndPoint(IPAddress.Any, 60000)
Dim ep_client As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)
Dim client As New UdpClient(ep_client)
Dim stream As New MemoryStream
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'I belive it's listening for packets comming from ep_client ->
'because I initialized the 'client' object with it
'How can I make it listen for ep_server?(from all ip's)
client.BeginReceive(New AsyncCallback(AddressOf receive), client)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
Public Sub receive(ByVal ar As IAsyncResult)
Dim buffer As Byte()
Try
'Now I use ep_server ->the proper way
buffer = client.EndReceive(ar, ep_server)
stream.Write(buffer, 0, buffer.Length)
client.BeginReceive(New AsyncCallback(AddressOf receive), client)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Dim img As Image = Image.FromStream(stream)
Debug.Print(stream.Length)
PictureBox1.Image = img ''Weird image because the packets are not arranged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Debug.Print("sent")
client.Send(ASCII.GetBytes("a"), 1, ep_client) ''Send something ->not working
End Sub
End Class
No error..nothing. I can send a picture from server to client but not the other way around. And there's another weird thing. I've put the client's code on another machine...It's gives an error when I write the ipv4(of the other machine) in ep_client like this Dim ep_client As New IPEndPoint(IPAddress.Parse("192.168.1.100"), 60000). I get no error. And if I initialize both of the 'udp-clients' on server and client code like this : Dim client as new UdpClient(ep_client) it gives an error :
Only one usage of each socket address (protocol/network address/port)
is normally permitted
But if I write it like this (on the server side) Dim client as new UdpClient("127.0.0.1",60000) it gives no error.What's the difference?
Where is the problem?

Your immediate problem is you can only have one Socket bound to a port number on one machine at any given time - so there must already be something bound to port 60000 to give you the "Only one usage of each socket address..." Exception - perhaps another instance of your program is already running?
Aside, and I am a game developer and understand the need to use UDP, UDP is not a good choice here instead of TCP. UDP is good for small "real-time" data not large one-off data. If using UDP you have to worry about: reliability - the datagram may not get through at all, duplicates - the datagram may be duplicate, congestion control - sending too many datagrams in quick succession without checking previous ones are getting through will just result in them being dropped... Really sending a large file is much better suited to TCP.

Related

How to Get IP Addresses in LAN Network Using VB.Net

enter image description hereI want to get IP addresses in That are connected with LAN network. I seen a youtube tutorial Youtube Video Got the addresses but only last address show here in my dataGridView
I want to get all addresses in datagridviewlist using VB.Net
Maybe my english is bit lower but I need help
This is My Code
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strHost As String
Dim strIp As String
strHost = Dns.GetHostName()
strIp = Dns.GetHostByName(strHost).AddressList(0).ToString()
TxtForIp.Text = strIp
End Sub
Private Sub BgScanningForIps_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BgScanningForIps.DoWork
Dim ping As Ping
Dim addr As IPAddress
Dim pinRpl As PingReply
Dim host As IPHostEntry
Dim name As String
Thread.Sleep(500)
Parallel.For(0, 254, Sub(i, loopState)
ping = New Ping()
pinRpl = ping.Send(TxtForIp.Text + i.ToString())
Me.BeginInvoke(CType(Sub()
If pinRpl.Status = IPStatus.Success Then
Try
addr = IPAddress.Parse(TxtForIp.Text + i.ToString())
host = Dns.GetHostEntry(addr)
name = host.HostName
DataGridView1.Rows.Add()
Dim nRowIndex As Integer = DataGridView1.Rows.Count - 1
DataGridView1.Rows(nRowIndex).Cells(0).Value = TxtForIp.Text + i.ToString()
DataGridView1.Rows(nRowIndex).Cells(1).Value = name
DataGridView1.Rows(nRowIndex).Cells(2).Value = "active"
Catch ex As Exception
name = "?"
End Try
End If
End Sub, Action))
End Sub)
MessageBox.Show("Scanned")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BgScanningForIps.RunWorkerAsync()
End Sub
End Class

VB LAN Messenger having issues

I have made a small LAN messenger and I tried it with two pc's and it didnt work however running two of the applications with one listening to port 40004 and writing to 40005 and one listening to 40005 and writing to 40004 works.
The problem seems to be in the sendb_click where I send the message. It says Receipent not connected.
Error message:
System.net.sockets.socketexception (0x80004005); No connection could be made
because the target machine actively refused it 127.0.0.1:40003
at system.net.sockets.tcpclient..ctor(string hostname,int32 port)
at messenger.form2.sendb_click(object sender, eventargs e) in
D:\Messenger\Messenger\Form2.vb:line 97
:Interface
When connected you can choose a computer that is connected to the network on the right and message it. Im planning on having generated ports or whatever but for testing ive just made 40004 and 40005 the ports.
What is going wrong? Do i have to port forward or something?
below is my code
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Imports System.Net.Dns
Imports System.DirectoryServices
Public Class Form2
Class entity
Public name As String
Public ip As String
Public port As Integer
End Class
Public reciepent As New entity
Public user As New entity
Public CLient As New TcpClient
Public listener As New TcpListener(40003)
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
usernametb.Text = user.name
FindingThreats()
getmyip()
ipl.Text = user.ip
user.port = 40003
portl.Text = user.port
' listener = New TcpListener(40004)
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
ListThread.Start()
End Sub
Private Sub Listening()
listener.Start()
End Sub
Sub FindingThreats()
UsersL.Items.Clear()
Dim childEntry As DirectoryEntry
Dim ParentEntry As New DirectoryEntry
Try
ParentEntry.Path = "WinNT:"
For Each childEntry In ParentEntry.Children
Select Case childEntry.SchemaClassName
Case "Domain"
Dim SubChildEntry As DirectoryEntry
Dim SubParentEntry As New DirectoryEntry
SubParentEntry.Path = "WinNT://" & childEntry.Name
For Each SubChildEntry In SubParentEntry.Children
Select Case SubChildEntry.SchemaClassName
Case "Computer"
UsersL.Items.Add(SubChildEntry.Name)
End Select
Next
End Select
Next
Catch Excep As Exception
MsgBox("Error While Reading Directories : " + Excep.Message.ToString)
Finally
ParentEntry = Nothing
End Try
End Sub
Sub getmyip()
For Each ip As Net.IPAddress In
GetHostEntry(GetHostName).AddressList
user.ip = ip.ToString
Next
End Sub
Sub getip(strhostname As String, returnip As String)
Try
returnip = GetHostByName(strhostname).AddressList(0).ToString()
reciepent.ip = returnip
Catch
MsgBox("This user is Offline.")
End Try
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles UsersL.SelectedIndexChanged
CUL.Items.Clear()
CUL.Items.Add("Connected Users:")
Dim ip As String
Dim hostname = UsersL.SelectedItem.ToString
getip(hostname, ip)
reciepent.port = 40004
Timer1.Enabled = True
CUL.Items.Add(user.name)
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
Me.Close()
End Sub
Private Sub SendB_Click(sender As Object, e As EventArgs) Handles SendB.Click
Try
CLient = New TcpClient("127.0.0.1", 40004) 'reciepent.port)
Dim Writer As New StreamWriter(CLient.GetStream())
Writer.Write(ChatB.Text)
ChatL.Items.Add("> " & ChatB.Text)
Writer.Flush()
Catch
MsgBox("Receipent not connected.")
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim message As String
If listener.Pending = True Then
message = ""
CLient = listener.AcceptTcpClient()
Dim Reader As New StreamReader(CLient.GetStream())
While Reader.Peek > -1
message = message + Convert.ToChar(Reader.Read()).ToString
End While
ChatL.Items.Add("< " & message)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FindingThreats()
End Sub
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
listener.Stop()
Me.Close()
End Sub
End Class

Sending an receiving two types of data across TCP connection

On the Client I want to -
I want to send a screenimage from the client and receive a string with cursor coordinates in.
On the server I want to receive and display the screenimage on Form2
and then send back my cursor coordinates to the client.
I can successfully send text messages from the client and receive them on the server using this code -
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
senddata("CHAT|" & TextBox3.Text) 'send the data with CHAT| as header
TextBox4.Text &= "You: " & " " & TextBox3.Text.Split("|")(0) & vbNewLine
End Sub
Sub senddata(ByVal message As String)
Dim sw As New StreamWriter(t.GetStream) 'declare a new streamwriter
sw.WriteLine(message) 'write the message
sw.Flush()
End Sub
and receive it on the server with this code -
Imports System.IO
Imports System.Net.Sockets
Public Class ConnectedClient
Private cli As TcpClient 'decleare a tcp client which will be the client that we assign to an instance of this class
Private uniqueid As String 'this will be used for the name property
Public Property name ''This will be the name of the ID containing its Unique ID
Get
Return uniqueid 'when we want to get it, it will return the Unique ID
End Get
Set(ByVal value)
uniqueid = value 'Used for setting the name
End Set
End Property
Sub New(ByVal client As TcpClient)
Dim r As New Random 'create a new random to serve as way to create our unique ID
Dim x As String = String.Empty 'declare a new variable to hold the ID
For i = 0 To 7 'we are goign to have an ID of 7 randomly generated characters
x &= Chr(r.Next(65, 89)) 'create a generate dnumber between 65 and 89 and get the letter that has the same ascii value (A-Z)
' and add it onto the ID string
Next
Me.name = client.Client.RemoteEndPoint.ToString().Remove(client.Client.RemoteEndPoint.ToString().LastIndexOf(":")) & " - " & x 'set the name to the Unique ID
cli = client 'assign the client specified to the TCP client variable to we can operate with it
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'start reading using the read subroutine
End Sub
Public Event gotmessage(ByVal message As String, ByVal client As ConnectedClient) 'this is raised when we get a message from the client
Public Event disconnected(ByVal client As ConnectedClient) 'this is raised when we get the client disconnects
Sub read(ByVal ar As IAsyncResult) 'this will process all messages being recieved
Try
Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents
' the current client which it has recieved the message from to perform any client specific
' tasks if needed
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
Catch ex As Exception
Try 'if an error occurs in the reading purpose, we will try to read again to see if we still can read
Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents
' the current client which it has recieved the message from to perform any client specific
' tasks if needed
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
Catch ' IF WE STILL CANNOT READ
RaiseEvent disconnected(Me) 'WE CAN ASSUME THE CLIENT HAS DISCONNECTED
End Try
End Try
End Sub
Sub senddata(ByVal message As String) 'this is used to deal with sending out messages
Dim sw As New StreamWriter(cli.GetStream) 'declare a new streamwrite to write to the stream between the client and the server
sw.WriteLine(message) 'write the message to the stream
sw.Flush()
End Sub
Sub listen(ByVal port As Integer)
Try
Dim t As New TcpListener(IPAddress.Any, port) 'declare a new tcplistener
t.Start() 'start the listener
Do
Dim client As New ConnectedClient(t.AcceptTcpClient) 'initialize a new connected client
AddHandler client.gotmessage, AddressOf recieved 'add the handler which will raise an event when a message is recieved
AddHandler client.disconnected, AddressOf disconnected 'add the handler which will raise an event when the client disconnects
Loop Until False
Catch
End Try
End Sub
In a totally different application I can successfully send the screen image from the client using this code
Imports System.Net.Sockets
Imports System.Threading
Imports System.Drawing
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Form1
Dim client As New TcpClient
Dim ns As NetworkStream
Dim port As Integer
Dim server As TcpListener
Dim listening As New Thread(AddressOf Listen)
Dim GetImage As New Thread(AddressOf ReceiveCursor)
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btnConnect.Click
port = CInt(txtPort.Text)
Try
If btnConnect.Text = "Connect" Then
client.Connect(txtIP.Text, port)
'MsgBox("Client connected !")
Timer1.Start()
btnConnect.Text = "Disconnect"
Else
btnConnect.Text = "Connect"
Timer1.Stop()
End If
Catch ex As Exception
MsgBox("Failed to connect..." + ex.Message)
End Try
End Sub
Public Function Desktop() As Image
Dim bounds As Rectangle = Nothing
Dim screenshot As System.Drawing.Bitmap = Nothing
Dim graph As Graphics = Nothing
bounds = Screen.PrimaryScreen.Bounds
screenshot = New Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Return screenshot
End Function
Private Sub SendDesktop()
Dim bf As New BinaryFormatter
ns = client.GetStream
bf.Serialize(ns, Desktop())
End Sub
Private Sub BtnShare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShare.Click
If btnShare.Text.StartsWith("Share") Then
Timer1.Start()
'port = Integer.Parse(Me.txtPort.Text)
'server = New TcpListener(port)
'listening.Start()
btnShare.Text = "Stop Sharing"
Else
Timer1.Stop()
btnShare.Text = "Share Desktop"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SendDesktop()
' SetCursorPosition()
End Sub
Private Sub ReceiveCursor()
Dim bf As New BinaryFormatter
While client.Connected = True
ns = client.GetStream
txtCursorPosition.Text = bf.Deserialize(ns)
End While
End Sub
Private Sub Listen()
While client.Connected = False
server.Start()
client = server.AcceptTcpClient
End While
GetImage.Start()
End Sub
Public Sub StopListening()
GetImage.Abort()
server.Stop()
client = Nothing
If listening.IsAlive Then
listening.Abort()
End If
End Sub
End Class
and receive it on the server using this code -
Imports System.Net.Sockets
Imports System.Threading
Imports System.Drawing
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Form2
Dim port As Integer
Dim client As New TcpClient
Dim ns As NetworkStream
Dim server As TcpListener
Dim listening As New Thread(AddressOf Listen)
Dim GetImage As New Thread(AddressOf ReceiveImage)
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Start the timer, obtain the port and start listening
Timer1.Start()
port = Integer.Parse(Form1.txtPort.Text)
server = New TcpListener(port)
listening.Start()
End Sub
Private Sub ReceiveImage()
Dim bf As New BinaryFormatter
While client.Connected = True
ns = client.GetStream
PictureBox1.Image = bf.Deserialize(ns)
End While
End Sub
Private Sub Listen()
While client.Connected = False
server.Start()
client = server.AcceptTcpClient
End While
GetImage.Start()
End Sub
Public Sub StopListening()
GetImage.Abort()
server.Stop()
client = Nothing
If listening.IsAlive Then
listening.Abort()
End If
End Sub
End Class
Is there a way of doing both using the same connection and somehow detecting which is text and which is the screenimage?

gives error of "The method or operation is not implemented" while getting HDD serial number

I am trying to get the HDD serial number with below code...
Imports System.IO
Imports System.Management
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
HDDCheck()
End Sub
Private Sub HDDCheck()
Try
Dim HDD_Serial As String
Dim hdd As New ManagementObjectSearcher("select * from Win32_DiskDrive")
For Each hd In hdd.Get
HDD_Serial = hd("SerialNumber")
If Not "20202020202020200332043354132314a5341" = HDD_Serial Then
Me.Close()
Else
Exit For
End If
Next
Catch ex As Exception
End Try
End Sub
but I am getting below error...
System.NotImplementedException was caught Message=The method or
operation is not implemented.
Same code runs on same machine on my previous applications ,
but this is new application I have written, this gives me an error...
What I am missing ?
I just have to change of code
from
Dim hdd As New ManagementObjectSearcher("select * from Win32_DiskDrive")
to
Dim hdd As New Management.ManagementObjectSearcher("select * from Win32_DiskDrive")
Thanks David Wilson

Chat system with one or two ways?

I'm trying con build a simple chat client/software (whole in on executable) wich start listen from the start on the port 5900 and when a client connect to that port the chat is established.
The problem is that only the client can chat to the server, the server cannot answer the client because the connection is working in one way.
The i've tried to connect from "server" to the client when it establishes a connection but the system crash warning me that the port is already on use.
This my code: (working in one way)
Imports System.Net.Sockets
Imports System.Text
Imports System.Reflection
Public Class frmComplete
Dim Data As Integer
Dim Message As String
Private sServer As TcpListener
Private sClient As New TcpClient
Private cServer As TcpListener
Private cClient As New TcpClient
Private cNick As String
Dim BufferSize(1024) As Byte
Private Delegate Sub MessageDelegate(ByVal Message As String)
Private Sub frmComplete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
srvListen(5900)
btnSend.Enabled = False
End Sub
Private Sub OnServerConnect(ByVal AR As IAsyncResult)
sClient = sServer.EndAcceptTcpClient(AR)
sClient.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnRead, Nothing)
My.Computer.Audio.Play(Application.StartupPath & "\Connected.wav", AudioPlayMode.Background)
End Sub
Private Sub OnRead(ByVal AR As IAsyncResult)
Data = sClient.GetStream.EndRead(AR)
Message = Encoding.ASCII.GetString(BufferSize, 0, Data)
Dim Args As Object() = {Message}
Me.Invoke(New MessageDelegate(AddressOf PrintMessage), Args)
sClient.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnRead, Nothing)
End Sub
Private Sub PrintMessage(ByVal Message As String)
Try
txtChat.Text = txtChat.Text & Message & vbCrLf
My.Computer.Audio.Play(Application.StartupPath & "\Message.wav", AudioPlayMode.Background)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub srvListen(ByVal port As Integer)
Try
sServer = New TcpListener(System.Net.IPAddress.Any, 5900)
sServer.Start()
'THIS WILL RAISE THE EVENT WHEN A CLIENT IS CONNECTED
sServer.BeginAcceptTcpClient(AddressOf OnServerConnect, Nothing)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub txtMessage_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyDown
'FIXME (SOUND T_T)
If e.KeyCode = Keys.Enter Then
SendMessage(cNick & ":" & txtMessage.Text)
End If
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
ConnectToServer(txtIP.Text)
cNick = txtNickname.Text
txtNickname.Enabled = False
txtIP.Enabled = False
btnConnect.Enabled = False
End Sub
Private Sub ConnectToServer(ByVal ipadress As String)
Try
cClient.BeginConnect(ipadress, 5900, AddressOf OnClientConnect, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub OnClientConnect(ByVal AR As IAsyncResult)
Try
cClient.EndConnect(AR)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If Not String.IsNullOrEmpty(txtMessage.Text) Then
txtChat.Text = txtChat.Text & "Me:" & txtMessage.Text & vbCrLf
SendMessage(cNick & ":" & txtMessage.Text)
End If
End Sub
Private Sub SendMessage(ByVal message As String)
If cClient.Connected = True Then
Dim Writer As New IO.StreamWriter(cClient.GetStream)
Writer.Write(message)
Writer.Flush()
End If
txtMessage.Text = ""
End Sub
Private Sub SendCommand(ByVal command As String)
If cClient.Connected = True Then
Dim Writer As New IO.StreamWriter(cClient.GetStream)
Writer.Write(command)
Writer.Flush()
End If
txtMessage.Text = ""
End Sub
Private Sub txtMessage_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMessage.TextChanged
If Not String.IsNullOrEmpty(txtMessage.Text) Then
btnSend.Enabled = True
Else
btnSend.Enabled = False
End If
End Sub
End Class
What I should do? use two ports? one for write and another to read? And if i need to conect multiple clients to one user? (remember the same exe is server/client)
Please help me =(
You aren't reading any data coming back from the Server. You'll notice in your OnServerConnect method you call the BeginRead -- you will also need to do this for your client in the OnClientConnect method, or you'll get a one way communication. Perhaps this is why you are not seeing any data coming through?
I'm guessing, when your Server sends back the data to the client, you aren't getting a hard-error, just no data.
Just glancing over your code I noticed that you have both a TcpClient and TcpListener for your client and server. You don't need this. Your SERVER will be the TcpListener, and your CLIENT will be the TcpClient. By asking if you should connect back on a different port from the server, you are shortchanging yourself of what the TCP connection really is. Once your TcpClient has connected to the TcpServer, your connection is established. There is no need further to attempt to connect.
You're client code should be something similar to:
Private Sub OnClientConnect(ByVal AR As IAsyncResult)
Try
cClient.EndConnect(AR)
sServer.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnClientRead, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub OnClientRead(ByVal AR As IAsyncResult)
Data = sServer.GetStream.EndRead(AR)
Message = Encoding.ASCII.GetString(BufferSize, 0, Data)
Dim Args As Object() = {Message}
Me.Invoke(New MessageDelegate(AddressOf PrintMessage), Args)
sServer.GetStream.BeginRead(BufferSize, 0, BufferSize.Length, AddressOf OnClientRead, Nothing)
End Sub