plz do help me as i am new to vb.net i hav given an assignment of doing chat application using tcp socket ports ...i have done by connecting with the server machine using port 81 from my pc..it works out well but wat i neet is to open a text file in the server machine from client machine and send integers to the text file is it possible..any suggestions..i hav attached wt i did so far
TCP chat
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Nathan Lane17 pointsPosted Sep 10, 2008, 5:38 AM EDT
priya jenifer4 pointsPosted Sep 10, 2008, 8:12 PM EDT
can u plz explain with some codes as i'm new to this ..the code which i written in server side is as follows
[code]
Imports System.Text
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Inherits System.Windows.Forms.Form
Public Delegate Sub DisplayInvoker(ByVal t As String)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
Friend WithEvents txtSend As System.Windows.Forms.TextBox
Friend WithEvents btnSend As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtfiledisplay As System.Windows.Forms.TextBox
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnSend = New System.Windows.Forms.Button
Me.txtSend = New System.Windows.Forms.TextBox
Me.txtDisplay = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.txtfiledisplay = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'btnSend
'
Me.btnSend.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnSend.Location = New System.Drawing.Point(368, 408)
Me.btnSend.Name = "btnSend"
Me.btnSend.Size = New System.Drawing.Size(56, 24)
Me.btnSend.TabIndex = 2
Me.btnSend.Text = "Send"
'
'txtSend
'
Me.txtSend.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtSend.Location = New System.Drawing.Point(12, 408)
Me.txtSend.Name = "txtSend"
Me.txtSend.Size = New System.Drawing.Size(344, 20)
Me.txtSend.TabIndex = 1
'
'txtDisplay
'
Me.txtDisplay.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDisplay.Location = New System.Drawing.Point(0, 0)
Me.txtDisplay.Multiline = True
Me.txtDisplay.Name = "txtDisplay"
Me.txtDisplay.ReadOnly = True
Me.txtDisplay.Size = New System.Drawing.Size(424, 471)
Me.txtDisplay.TabIndex = 0
Me.txtDisplay.TabStop = False
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(31, 340)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(0, 13)
Me.Label1.TabIndex = 3
'
'txtfiledisplay
'
Me.txtfiledisplay.Location = New System.Drawing.Point(12, 476)
Me.txtfiledisplay.Name = "txtfiledisplay"
Me.txtfiledisplay.Size = New System.Drawing.Size(100, 20)
Me.txtfiledisplay.TabIndex = 4
'
'Form1
'
Me.AcceptButton = Me.btnSend
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(424, 508)
Me.Controls.Add(Me.txtfiledisplay)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnSend)
Me.Controls.Add(Me.txtSend)
Me.Controls.Add(Me.txtDisplay)
Me.Name = "Form1"
Me.Text = "Socket Client"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Private mobjClient As TcpClient
Private marData(1024) As Byte
Private mobjText As New StringBuilder()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mobjClient = New TcpClient("120.0.0.1", 81)
DisplayText("Connected to host" & vbCrLf)
Dim sFileName As String = "c:\test.txt"
Dim myFileStream As System.IO.FileStream
Try
'We have our variables, lets attempt to open it
myFileStream = New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Catch
'Make sure that it exists, and if it doesn't display an error
MessageBox.Show("Cannot not open the file. Make sure in the location shown.")
Finally
'It works, but we are going to try one last method
If myFileStream.CanRead = True Then
'This is where we would read from the file
Label1.Text = "File opened!"
txtfiledisplay.Text = "File opened!"
'We are done with the file now, so close it
myFileStream.Close()
End If
End Try
' Dim sFileName As String = "e:\Test.txt"
My.Computer.FileSystem.WriteAllText("c:\test.txt", txtSend.Text, False)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
Send("New client online")
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim sFileName As String = "c:\test.text"
Dim headings As String = "mst_ID VR VY VB RR RY RB CR CY CB AR AY AB RPF YPF BPF RecDt RecTime"
'text box data here
Dim vars As String = txtSend.Text
'open write stream
Dim writeStream As New System.IO.StreamWriter("C:\test.text")
'add tabs to strings (formatting preference)
vars = Replace(vars, ";", vbTab)
'write strings to file
writeStream.WriteLine(headings)
writeStream.WriteLine(vars)
'make sure you close the write stream
writeStream.Close()
'put column headings in here Dim headings As String = "item no code rate" 'text box data here Dim vars As String = TextBox.Text 'open write stream Dim writeStream As New System.IO.StreamWriter("C:\output.txt") 'add tabs to strings (formatting preference) vars = Replace(vars, ";", vbTab) 'write strings to file writeStream.WriteLine(headings) writeStream.WriteLine(vars) 'make sure you close the write stream writeStream.Close()
Dim myFileStream As System.IO.FileStream
Try
'We have our variables, lets attempt to open it
myFileStream = New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Catch
'Make sure that it exists, and if it doesn't display an error
MessageBox.Show("Cannot not open the file. Make sure in the location shown.")
Finally
'It works, but we are going to try one last method
If myFileStream.CanRead = True Then
'This is where we would read from the file
Label1.Text = "File opened!"
txtfiledisplay.Text = "File opened!"
'We are done with the file now, so close it
myFileStream.Close()
End If
End Try
' Dim sFileName As String = "e:\Test.txt"
My.Computer.FileSystem.WriteAllText("c:\test.txt", txtSend.Text, True)
Dim fileText As String = My.Computer.FileSystem.ReadAllText("c:\test.txt")
'Dim myfilestream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
'myfilestream = New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Dim NewCopy As String
'Dim myWriter As New System.IO.StreamWriter(myfilestream)
'filetocopy = "e:\test.txt"
'NewCopy = "e:\NewTest.txt"
' myWriter.WriteLine(txtSend.Text)
'myWriter.Flush()
'If System.IO.File.Exists(filetocopy) = True Then
'System.IO.File.Copy(filetocopy, NewCopy)
'MsgBox("File Copied")
'End If
txtSend.Text = "" End Sub Private Sub Send(ByVal t As String) Dim w As New IO.StreamWriter(mobjClient.GetStream) w.Write(t & vbCr) w.Flush() End Sub Private Sub DoRead(ByVal ar As IAsyncResult) Dim intCount As Integer Try intCount = mobjClient.GetStream.EndRead(ar) If intCount < 1 Then MarkAsDisconnected() Exit Sub End If BuildString(marData, 0, intCount) mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing) Catch e As Exception MarkAsDisconnected() End Try End Sub Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer) Dim intIndex As Integer For intIndex = offset To offset + count - 1 If Bytes(intIndex) = 10 Then mobjText.Append(vbLf) Dim params() As Object = {mobjText.ToString}Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)
mobjText = New StringBuilder()
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Private Sub MarkAsDisconnected()
txtSend.ReadOnly = True
'btnSend.Enabled = False
End Sub
Private Sub DisplayText(ByVal t As String)
txtDisplay.AppendText(t)
End Sub
Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
End Sub
End Class
[/code]
client side code
[code]
Imports System.Text
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Inherits System.Windows.Forms.Form
Public Delegate Sub DisplayInvoker(ByVal t As String)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
Friend WithEvents txtSend As System.Windows.Forms.TextBox
Friend WithEvents btnSend As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtfiledisplay As System.Windows.Forms.TextBox
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnSend = New System.Windows.Forms.Button
Me.txtSend = New System.Windows.Forms.TextBox
Me.txtDisplay = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.txtfiledisplay = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'btnSend
'
Me.btnSend.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnSend.Location = New System.Drawing.Point(368, 408)
Me.btnSend.Name = "btnSend"
Me.btnSend.Size = New System.Drawing.Size(56, 24)
Me.btnSend.TabIndex = 2
Me.btnSend.Text = "Send"
'
'txtSend
'
Me.txtSend.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtSend.Location = New System.Drawing.Point(12, 408)
Me.txtSend.Name = "txtSend"
Me.txtSend.Size = New System.Drawing.Size(344, 20)
Me.txtSend.TabIndex = 1
'
'txtDisplay
'
Me.txtDisplay.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDisplay.Location = New System.Drawing.Point(0, 0)
Me.txtDisplay.Multiline = True
Me.txtDisplay.Name = "txtDisplay"
Me.txtDisplay.ReadOnly = True
Me.txtDisplay.Size = New System.Drawing.Size(424, 471)
Me.txtDisplay.TabIndex = 0
Me.txtDisplay.TabStop = False
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(31, 340)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(0, 13)
Me.Label1.TabIndex = 3
'
'txtfiledisplay
'
Me.txtfiledisplay.Location = New System.Drawing.Point(12, 476)
Me.txtfiledisplay.Name = "txtfiledisplay"
Me.txtfiledisplay.Size = New System.Drawing.Size(100, 20)
Me.txtfiledisplay.TabIndex = 4
'
'Form1
'
Me.AcceptButton = Me.btnSend
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(424, 508)
Me.Controls.Add(Me.txtfiledisplay)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnSend)
Me.Controls.Add(Me.txtSend)
Me.Controls.Add(Me.txtDisplay)
Me.Name = "Form1"
Me.Text = "Socket Client"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Private mobjClient As TcpClient
Private marData(1024) As Byte
Private mobjText As New StringBuilder()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mobjClient = New TcpClient("120.0.0.1", 81)
DisplayText("Connected to host" & vbCrLf)
Dim sFileName As String = "c:\test.txt"
Dim myFileStream As System.IO.FileStream
Try
'We have our variables, lets attempt to open it
myFileStream = New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Catch
'Make sure that it exists, and if it doesn't display an error
MessageBox.Show("Cannot not open the file. Make sure in the location shown.")
Finally
'It works, but we are going to try one last method
If myFileStream.CanRead = True Then
'This is where we would read from the file
Label1.Text = "File opened!"
txtfiledisplay.Text = "File opened!"
'We are done with the file now, so close it
myFileStream.Close()
End If
End Try
' Dim sFileName As String = "e:\Test.txt"
My.Computer.FileSystem.WriteAllText("c:\test.txt", txtSend.Text, False)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
Send("New client online")
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim sFileName As String = "c:\test.text"
Dim headings As String = ""mst_ID VR VY VB RR RY RB CR CY CB AR AY AB RPF YPF BPF RecDt RecTime""
'text box data here
Dim vars As String = txtSend.Text
'open write stream
Dim writeStream As New System.IO.StreamWriter("C:\test.text")
'add tabs to strings (formatting preference)
vars = Replace(vars, ";", vbTab)
'write strings to file
writeStream.WriteLine(headings)
writeStream.WriteLine(vars)
'make sure you close the write stream
writeStream.Close()
'put column headings in here Dim headings As String = "item no code rate" 'text box data here Dim vars As String = TextBox.Text 'open write stream Dim writeStream As New System.IO.StreamWriter("C:\output.txt") 'add tabs to strings (formatting preference) vars = Replace(vars, ";", vbTab) 'write strings to file writeStream.WriteLine(headings) writeStream.WriteLine(vars) 'make sure you close the write stream writeStream.Close()
Dim myFileStream As System.IO.FileStream
Try
'We have our variables, lets attempt to open it
myFileStream = New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Catch
'Make sure that it exists, and if it doesn't display an error
MessageBox.Show("Cannot not open the file. Make sure in the location shown.")
Finally
'It works, but we are going to try one last method
If myFileStream.CanRead = True Then
'This is where we would read from the file
Label1.Text = "File opened!"
txtfiledisplay.Text = "File opened!"
'We are done with the file now, so close it
myFileStream.Close()
End If
End Try
' Dim sFileName As String = "e:\Test.txt"
My.Computer.FileSystem.WriteAllText("c:\test.txt", txtSend.Text, True)
Dim fileText As String = My.Computer.FileSystem.ReadAllText("c:\test.txt")
'Dim myfilestream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
'myfilestream = New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Dim NewCopy As String
'Dim myWriter As New System.IO.StreamWriter(myfilestream)
'filetocopy = "e:\test.txt"
'NewCopy = "e:\NewTest.txt"
' myWriter.WriteLine(txtSend.Text)
'myWriter.Flush()
'If System.IO.File.Exists(filetocopy) = True Then
'System.IO.File.Copy(filetocopy, NewCopy)
'MsgBox("File Copied")
'End If
txtSend.Text = "" End Sub Private Sub Send(ByVal t As String) Dim w As New IO.StreamWriter(mobjClient.GetStream) w.Write(t & vbCr) w.Flush() End Sub Private Sub DoRead(ByVal ar As IAsyncResult) Dim intCount As Integer Try intCount = mobjClient.GetStream.EndRead(ar) If intCount < 1 Then MarkAsDisconnected() Exit Sub End If BuildString(marData, 0, intCount) mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing) Catch e As Exception MarkAsDisconnected() End Try End Sub Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer) Dim intIndex As Integer For intIndex = offset To offset + count - 1 If Bytes(intIndex) = 10 Then mobjText.Append(vbLf) Dim params() As Object = {mobjText.ToString}Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)
mobjText = New StringBuilder()
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Private Sub MarkAsDisconnected()
txtSend.ReadOnly = True
'btnSend.Enabled = False
End Sub
Private Sub DisplayText(ByVal t As String)
txtDisplay.AppendText(t)
End Sub
Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
End Sub
End Class
[/code]