กระดานแสดงความคิดเห็น
Home
Contents
Articles
Quiz
Members
Sponsor
Print-friendly
MENU
ปรับปรุง : 2566-10-15 (กระดานแสดงความคิดเห็น)
เว็บเพจหน้านี้สำหรับผู้ดูแลเท่านั้น
รหัส secure
=>
นำตัวอักษร สีขาวบนพื้นแดง มาป้อนในช่องนี้
edit_topic_password =>
<center><table width=90% border=0 bgcolor=#000080><tr><td><font color=white size=4>code เชื่อมต่อ client server ด้วย VB.NET</td></tr></table><table width=90% bordercolor=#000080 border=1><tr><td bgcolor=white><br>code เชื่อมต่อ client server ด้วย VB.NET<br /> VB.NET TCP Client - Server Socket Communications<br /> http://www.eggheadcafe.com/articles/20020323.asp<br /> Imports System.Net.Sockets<br /> Imports System.Text<br /> Class TCPCli<br /> Shared Sub Main()<br /> <br /> Dim tcpClient As New System.Net.Sockets.TcpClient()<br /> tcpClient.Connect("127.0.0.1", 8000)<br /> Dim networkStream As NetworkStream = tcpClient.GetStream()<br /> If networkStream.CanWrite And networkStream.CanRead Then<br /> ' Do a simple write.<br /> Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")<br /> networkStream.Write(sendBytes, 0, sendBytes.Length)<br /> ' Read the NetworkStream into a byte buffer.<br /> Dim bytes(tcpClient.ReceiveBufferSize) As Byte<br /> networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))<br /> ' Output the data received from the host to the console.<br /> Dim returndata As String = Encoding.ASCII.GetString(bytes)<br /> Console.WriteLine(("Host returned: " + returndata))<br /> Else<br /> If Not networkStream.CanRead Then<br /> Console.WriteLine("cannot not write data to this stream")<br /> tcpClient.Close()<br /> Else<br /> If Not networkStream.CanWrite Then<br /> Console.WriteLine("cannot read data from this stream")<br /> tcpClient.Close()<br /> End If<br /> End If<br /> End If<br /> ' pause so user can view the console output<br /> Console.ReadLine()<br /> End Sub<br /> End Class<br /> <br /> All we're doing here is creating a new TcpClient, calling its Connect method, and then getting access to its underlying NetworkStream via the GetStream() method. We Write our message into the stream (converted to a byte array first) and then Read the response from the server. When done, we close the socket. And now, on the server side: <br /> <br /> <br /> Imports System.Net.Sockets<br /> Imports System.Text<br /> Class TCPSrv<br /> Shared Sub Main()<br /> ' Must listen on correct port- must be same as port client wants to connect on.<br /> Const portNumber As Integer = 8000<br /> Dim tcpListener As New TcpListener(portNumber)<br /> tcpListener.Start()<br /> Console.WriteLine("Waiting for connection...")<br /> Try<br /> 'Accept the pending client connection and return 'a TcpClient initialized for communication. <br /> Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()<br /> Console.WriteLine("Connection accepted.")<br /> ' Get the stream<br /> Dim networkStream As NetworkStream = tcpClient.GetStream()<br /> ' Read the stream into a byte array<br /> Dim bytes(tcpClient.ReceiveBufferSize) As Byte<br /> networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))<br /> ' Return the data received from the client to the console.<br /> Dim clientdata As String = Encoding.ASCII.GetString(bytes)<br /> Console.WriteLine(("Client sent: " + clientdata))<br /> Dim responseString As String = "Connected to server."<br /> Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)<br /> networkStream.Write(sendBytes, 0, sendBytes.Length)<br /> Console.WriteLine(("Message Sent /> : " + responseString))<br /> 'Any communication with the remote client using the TcpClient can go here.<br /> 'Close TcpListener and TcpClient.<br /> tcpClient.Close()<br /> tcpListener.Stop()<br /> Console.WriteLine("exit")<br /> Console.ReadLine()<br /> Catch e As Exception<br /> Console.WriteLine(e.ToString())<br /> Console.ReadLine()<br /> End Try<br /> End Sub<br /> End Class<br /> <br><br></td></tr><tr><td align=right bgcolor=black><font color=white><small><b>จากคุณ :</b> บุรินทร์ <a href=mailto:></a><a title='118.172.104.109'>.</a><br> 02:01pm (26/07/08)</font></td></tr></table></center>