求,关于开发socket的聊天程序---------精1
接上,一同名贴子============
'fig, 19.2 client.vb
'set up a clinet that reads and displays data sent from server
Imports System.Windows.Forms
Imports System.Threading
Imports System.Net.Sockets
Imports System.IO
Public Class client_2008
Inherits Form
'textboxes for inputting and displyaing information
'' Friend WithEvents txtinput As TextBox
'' Friend WithEvents txtdisplay As TextBox
'stream for ending data to server
Private output As NetworkStream
'objects for writing and reading bytes to streams
Private writer As BinaryWriter
Private reader As BinaryReader
Private message As String = "" 'message sent to server
'thread prevents clinet from blocking data transfer
Private readthread As Thread
Public Sub New()
MyBase.New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
'add any initialization after the
' initializecomponent call
End Sub 'new
Dim s1 As String
Dim txt_ip As String = "192.168.3.33"
Private Sub client_2008_closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
System.Environment.Exit(System.Environment.ExitCode)
End Sub
'send user input ot server
Private Sub txtinput_keydown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtinput.KeyDown
'send user input user pressed enter
Try
'determine whether user pressed enter
If e.KeyCode = Keys.Enter Then
'send data to server
writer.Write("CLIENT>>>" & txtinput.Text)
txtdisplay.Text &= vbCrLf & "CLIENT>>>" & txtinput.Text
txtinput.Clear()
End If
'handle exception if error occurs in sending data to server
Catch exception As SocketException
txtdisplay.Text &= vbCrLf & "Error writing object"
End Try
End Sub 'txtinput_keydown
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtinput.TextChanged
End Sub
'connection to server and display server-generated text
Public Sub runclient()
Dim client As TcpClient
'发送数据到服务器
Try
'' txtdisplay.Text &= "Attempting connection" & vbCrLf
'step 1:create tcpclient and connecton to server
client = New TcpClient()
'' client.Connect("localhost", 5000)
'' client.Connect("192.168.3.31", 5000)
Dim intport As Integer = CType(TextBox2.Text.Trim(), Integer)
client.Connect(txt_ip, intport)
'step 2:get networkstream assoclated with tcpclient
output = client.GetStream()
'create objects for writing and reading across stream
writer = New BinaryWriter(output)
reader = New BinaryReader(output)
'' txtdisplay.Text &= vbCrLf & "get I/O streams " & vbCrLf
s1 &= vbCrLf & "客户连接成功! " & vbCrLf
exec1(s1)
' ' txtinput.ReadOnly = False
'step 3: processing phase
Try
'loop until server signals termination
Do
'read message from server
message = reader.ReadString
'' txtdisplay.Text &= vbCrLf & message
s1 &= vbCrLf & message
exec1(s1)
Loop While message <> "SERVER>>>TERMINATE"
'HANDLE EXCEPTION IF ERROR IN READING SERVER DATA
Catch inputoutputexcetion As IOException
MessageBox.Show("客户断开连接!a")
'step 4: close connecton
Finally
'' txtdisplay.Text &= vbCrLf & "closing connection" & vbCrLf
s1 &= vbCrLf & "客户断开连接!1b" & vbCrLf
exec1(s1)
writer.Close()
reader.Close()
output.Close()
client.Close()
End Try
Application.Exit()
'handle exception if error in establishing connection
Catch inputoutputexception As Exception
MessageBox.Show("服务器没有开启服务!")
End Try
End Sub 'runclinet
Sub exec1(ByVal s1) '显示数据
' TextBox1.Text = s1
MsgBox(s1)
End Sub
Private Sub client_2008_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text <> "" And TextBox2.Text <> "" Then
readthread = New Thread(AddressOf runclient)
readthread.Start()
txt_ip = TextBox1.Text
Else
MsgBox("连接前,IP地址和端口号不能为空,请检查!")
TextBox1.Focus()
TextBox1.Select()
End If
End Sub
End Class 'frmclient
--------------------编程问答-------------------- 学习一下!!!!!!!! --------------------编程问答-------------------- 学习
补充:.NET技术 , VB.NET