Καλησπέρα.
Προσπαθώ να μάθω πως μπορώ να φτιάξω μία Server εφαρμογή η οποία θα έχει και γραφικό interface.
Η δοκιμαστική εφαρμογή λοιπόν είναι έτυμη εκτός από μία λεπτομέρεια.
Έχω ένα Log
το οποίο θέλω να εμφανίζετε σε ένα datagridview αλλά μέσα από το thread δεν
γράφει στο grid αλλά ούτε βγάζει κάποιο μήνυμα λάθους ενώ περνάει από το σημείο και τρέχει την εντολή.
παρακάτω ο κώδικας μου
Option Strict On
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class dpserver
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WriteToLog("log.txt", "Listening on port " & "9999 kosmas", "Server")
Dim kos As New startup("k")
Dim thread2 As New Thread(New ThreadStart(AddressOf kos.run))
thread2.IsBackground = True
thread2.Priority = ThreadPriority.Highest
thread2.Start()
End Sub
Class startup
Private k As String
Public Sub New(ByVal k2 As String)
Me.k = k2
End Sub 'New
Public Sub run()
Dim tcplistener As New TcpListener(9999)
tcplistener.Start()
While True
Dim socket As Socket = tcplistener.AcceptSocket()
Dim dpse As New dpse(socket)
Dim thread As New Thread(New ThreadStart(AddressOf dpse.run))
thread.IsBackground = True
thread.Start()
End While
End Sub
End Class
Class dpse
Private clientSocket As Socket
Private read() As [Byte] = New Byte(1023) {}
Private Buffer As [Byte]() = Nothing
Private ASCII As Text.Encoding = System.Text.Encoding.ASCII
Private RecvBytes(4095) As [Byte]
Const port As Integer = 9999
Public Sub New(ByVal socket As Socket)
Me.clientSocket = socket
End Sub 'New
Public Sub run()
Dim clientmessage As String = " "
Dim sURL As String = " "
Dim logmsg As String = " "
Dim bytes As Integer = readmessage(read, clientSocket, clientmessage)
If bytes = 0 Then
Return
End If
Dim index1 As Integer = clientmessage.IndexOf(" "c)
logmsg = clientmessage.Substring(0, 100)
WriteToLog("log.txt", DirectCast("Client message: " + logmsg, String), "Client")
WriteToLog("log.txt", "Connection from : " + clientSocket.RemoteEndPoint.ToString(), "Server")
Try
sendmessage(clientSocket, "server :: " + clientmessage)
WriteToLog("log.txt", "Message returned : " + "server" + logmsg, "Server")
Catch exc2 As Exception
WriteToLog("log.txt", exc2.ToString, "Server")
End Try
End Sub 'run
Private Function readmessage(ByVal ByteArray() As Byte, ByRef s As Socket, ByRef clientmessage As String) As Integer
Dim bytes As Integer = s.Receive(ByteArray, 1024, 0)
Dim messagefromclient As String = System.Text.Encoding.ASCII.GetString(ByteArray)
clientmessage = CStr(messagefromclient)
Return bytes
End Function 'readmessage
Private Sub sendmessage(ByVal s As Socket, ByVal message As String)
Buffer = New [Byte](message.Length + 1) {}
Dim length As Integer = ASCII.GetBytes(message, 0, message.Length, Buffer, 0)
s.Send(Buffer, length, 0)
End Sub 'sendmessage
End Class
End Class
Module log
Public Sub WriteToLog(ByVal Filename As String, ByVal Comment As String, ByVal src As String)
Dim sw As System.IO.StreamWriter
Try
sw = New System.IO.StreamWriter(Filename, True)
sw.WriteLine(Date.Now & " " & Comment)
dpserver.DataGridView1.Rows.Add(Date.Now, Comment)
'dpserver.mes.Refresh()
Catch e As Exception
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Sub
End Module
Και o Client
Option Strict On
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ctr As Integer = 0
While ctr < 10
Label1.Text = ctr.ToString()
Dim st As New test(TextBox1.Text + ctr.ToString())
Dim thread2 As New Thread(New ThreadStart(AddressOf st.doit))
thread2.IsBackground = True
thread2.Priority = ThreadPriority.Highest
thread2.Start()
ctr += 1
End While
End Sub
Class test
Public te As String
Public Sub New(ByVal text As String)
te = text
End Sub
Public Sub doit()
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 13000
Dim client As New TcpClient("localhost", 9999)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(te)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Form1.TextBox2.Text += Environment.NewLine + "Sent: " + te + Environment.NewLine
Form1.Refresh()
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Form1.TextBox2.Text += "Received: " + responseData + Environment.NewLine
Form1.Refresh()
' Close everything.
stream.Close()
client.Close()
Catch ex As ArgumentNullException
Form1.TextBox2.Text += "ArgumentNullException: " + ex.Message.ToString() + Environment.NewLine
Form1.Refresh()
Catch ex As SocketException
Form1.TextBox2.Text += "ArgumentNullException: " + ex.Message.ToString() + Environment.NewLine
Form1.Refresh()
End Try
End Sub
End Class
End Class
Ευχαριστώ πολύ
Κοσμάς