套接字服务器:需要帮助转移到WinForm App [英] Socket Server: Need help transferring to WinForm App
本文介绍了套接字服务器:需要帮助转移到WinForm App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我将其从控制台应用程序转移到WinForms应用程序吗?我还是这个新手,一旦我对VB.net有了更深入的了解,我想以此为基础.我不知道这是否有帮助,但是我使用的是Visual Studio 2008(非表达).
Can someone help me tranfer this from a Console app to a WinForms app? I'm still new to this and I want to build on top of this once I get more acquainted with VB.net. I don't know if this may be helpful, but I'm using Visual Studio 2008 (not express).
我在网上找到的服务器代码:
The Server Code I Found Online:
导入 System.Net.Sockets |
导入 System.Text |
导入 System.IO |
导入 System.Net |
导入 Microsoft.VisualBasic |
模块 Module1 |
子 Main() |
暗 端口 为 Int32 = 500 |
暗 localAddr As IPAddress = IPAddress.Parse( " 192.168.0.5" ) |
暗 serverSocket 如 新 暗 requestCount 按 整数 > |
暗 clientSocket 如 TcpClient |
serverSocket.Start() |
msg( 服务器已启动" ) |
客户端套接字= serverSocket.AcceptTcpClient() |
msg( 从客户端接受连接" |
而 ( True ) |
尝试 |
``requestCount == requestCount + 1'' |
Dim networkStream <字体样式="color:blue">为 <字体样式="font-size:11px"> NetworkStream == _ |
客户端套接字.getStream() |
Dim bytesFrom(10024) | font> 为 字节
``networkStream.Read(bytesFrom,0, CInt (clientSocket.ReceiveBufferSize)) | 字体>
Dim dataFromClient <字体样式="color:blue">为 字符串 dataFromClient = _ |
dataFromClient.Substring(0,dataFromClient.IndexOf( &"$"" msg( 来自客户端的数据-" 昏暗 serverResponse 字符串 = _ /font> |
服务器响应" + Convert.ToString(requestCount) |
暗 sendBytes [[ 字节 ]( )== _ |
Encoding.ASCII.GetBytes(serverResponse) |
networkStream.Write(sendBytes,0,sendBytes.Length) |
networkStream.Flush() |
msg(serverResponse) |
捕获 例如 如 例外 |
``MsgBox(ex.ToString) |
结束 尝试 |
结束 而 |
clientSocket.Close() |
serverSocket. 停止 () |
msg( &"exit"" 结束 子 |
Sub msg( ByVal 消息 为 String ) |
mess.Trim() |
Console.WriteLine( ">>" 结束 Sub |
结束 模块 |
Imports System.Net.Sockets |
Imports System.Text |
Imports System.IO |
Imports System.Net |
Imports Microsoft.VisualBasic |
Module Module1 |
Sub Main() |
Dim port As Int32 = 500 |
Dim localAddr As IPAddress = IPAddress.Parse("192.168.0.5") |
Dim serverSocket As New TcpListener(localAddr, port) |
Dim requestCount As Integer |
Dim clientSocket As TcpClient |
serverSocket.Start() |
msg("Server Started") |
clientSocket = serverSocket.AcceptTcpClient() |
msg("Accept connection from client") |
requestCount = 0 |
While (True) |
Try |
requestCount = requestCount + 1 |
Dim networkStream As NetworkStream = _ |
clientSocket.GetStream() |
Dim bytesFrom(10024) As Byte |
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize)) |
Dim dataFromClient As String = _ |
System.Text.Encoding.ASCII.GetString(bytesFrom) |
dataFromClient = _ |
dataFromClient.Substring(0, dataFromClient.IndexOf("$")) |
msg("Data from client - " + dataFromClient) |
Dim serverResponse As String = _ |
"Server response " + Convert.ToString(requestCount) |
Dim sendBytes As [Byte]() = _ |
Encoding.ASCII.GetBytes(serverResponse) |
networkStream.Write(sendBytes, 0, sendBytes.Length) |
networkStream.Flush() |
msg(serverResponse) |
Catch ex As Exception |
MsgBox(ex.ToString) |
End Try |
End While |
clientSocket.Close() |
serverSocket.Stop() |
msg("exit") |
Console.ReadLine() |
End Sub |
Sub msg(ByVal mesg As String) |
mesg.Trim() |
Console.WriteLine(" >> " + mesg) |
End Sub |
End Module |
推荐答案
在这里,UBigDummie,我知道这个线程有点旧...但是我没有别的事情可做:是RichTextBox1.
Here UBigDummie, i know this thread is a bit old... but i had nothing else to do :p
just add a richtextbox to ur form, its name must be RichTextBox1.
Imports System.Text
Imports System.IO
Imports System.Net
Imports Microsoft.VisualBasic
Public Class Form1
Dim IsClosing As Boolean = False
Dim Outcome As String = Nothing
Dim LastOutcome As String = Nothing
Sub Main()
CheckForIllegalCrossThreadCalls = False
Dim port As Int32 = 500
Dim localAddr As IPAddress = IPAddress.Parse("192.168.0.5")
Dim serverSocket As New TcpListener(localAddr, port)
Dim requestCount As Integer
Dim clientSocket As TcpClient
serverSocket.Start()
msg("Server Started")
clientSocket = serverSocket.AcceptTcpClient()
msg("Accept connection from client")
requestCount = 0
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = _
System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("
))) msg(来自客户端的数据-" + dataFromClient) Dim serverResponse As String = _ 服务器响应" + Convert.ToString(requestCount) 昏暗的sendBytes为[Byte]()= _ Encoding.ASCII.GetBytes(serverResponse) networkStream.Write(sendBytes,0,sendBytes.Length) networkStream.Flush() msg(serverResponse) 异常捕获 MsgBox(例如ToString) 结束尝试 结束时间 clientSocket.Close() serverSocket.Stop() msg(退出") Console.ReadLine() 结束子 Sub msg(ByVal mesg作为字符串) mesg.Trim() RichTextBox1.Text =消息+ vbNewLine + RichTextBox1.Text 结束子 私有子Form1_FormClosing(ByVal发送者作为System.Object,ByVal e作为System.Windows.Forms.FormClosingEventArgs)处理MyBase.FormClosing IsClosing =真 结束子 末级
")) msg("Data from client - " + dataFromClient) Dim serverResponse As String = _ "Server response " + Convert.ToString(requestCount) Dim sendBytes As [Byte]() = _ Encoding.ASCII.GetBytes(serverResponse) networkStream.Write(sendBytes, 0, sendBytes.Length) networkStream.Flush() msg(serverResponse) Catch ex As Exception MsgBox(ex.ToString) End Try End While clientSocket.Close() serverSocket.Stop() msg("exit") Console.ReadLine() End Sub Sub msg(ByVal mesg As String) mesg.Trim() RichTextBox1.Text = mesg + vbNewLine + RichTextBox1.Text End Sub Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing IsClosing = True End Sub End Class
这篇关于套接字服务器:需要帮助转移到WinForm App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文