求大神解决
服务器端程序
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Dim serverThread As Thread
Dim tcpListener As TcpListener
Dim netStream As NetworkStream
Dim rdStream As StreamReader
Dim wtStream As StreamWriter
Dim clientSocket As Socket
Private Sub Listen()
Dim clientMsg As String
tcpListener = New TcpListener(1111)
Try
tcpListener.Start()
clientSocket = tcpListener.AcceptSocket()
If clientSocket.Connected Then
lstClient.Items.Add("new user")
netStream = New NetworkStream(clientSocket)
rdStream = New StreamReader(netStream)
wtStream = New StreamWriter(netStream)
End If
While (True)
clientMsg = rdStream.ReadLine()
If clientMsg = "Exit" Then
Exit Sub
End If
lstClient.Items.Add(clientMsg)
End While
Catch ex As SocketException
MessageBox.Show("无法打开端口")
Catch ex As IOException
Catch ex As ThreadAbortException
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub EndListen()
If (Not netStream Is Nothing) Then
serverThread.Abort()
tcpListener.Stop()
End If
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Try
serverThread = New Thread(AddressOf Listen)
serverThread.Start()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_Disposed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Disposed
EndListen()
End Sub
Private Sub send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles send.Click
Try
wtStream.WriteLine(TextBox1.Text)
wtStream.Flush()
Catch ex As Exception
MessageBox.Show("无法发送消息")
End Try
End Sub
Private Sub out_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles out.Click
End
End Sub
End Class
客户端程序
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Dim tcpClient As TcpClient
Dim netStream As NetworkStream
Dim rdStream As StreamReader
Dim wtStream As StreamWriter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Try
tcpClient = New TcpClient("localhost", 1111)
netStream = tcpClient.GetStream()
rdStream = New StreamReader(netStream)
wtStream = New StreamWriter(netStream)
Catch ex As Exception
MessageBox.Show("无法连接服务器")
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
wtStream.WriteLine(txtMsg.Text)
wtStream.Flush()
Catch ex As Exception
MessageBox.Show("无法发送消息")
End Try
End Sub
Private Sub Form1_Disposed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Disposed
wtStream.WriteLine("Exit")
wtStream.Flush()
End Sub
Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
TextBox1.Text = rdStream.ReadLine()
End Sub
End Class
------解决思路----------------------
两台电脑,你访问的主机还写 localhost 肯定是找错了。
------解决思路----------------------
填写 IP地址
另外 这个问题 问得实在是。。。。。。。怎么说呢,不该回答你
------解决思路----------------------
不在一个局域网的话我也帮不了了、
如果在的话,IP和端口对应就没问题了,毕竟你在本机测试OK的