PDA

View Full Version : مشکل در برنامه نویسی سوکت: اطلاع از پایان یافتن دریافت دیتا در سرور



matrixrayaneh
جمعه 13 شهریور 1394, 15:04 عصر
سلام دوستان
من یه برنامه با سوکت نوشتم که در اون اطلاعات رو از کلاینت میخونم. تا اینجاش مشکلی نیست اما مشکل اینجاست که دو سری داده به سرور میاد که باید وقتی سری اول اومد یه پیغامی به کلاینت بفرستم تا اون کلاینت سری دوم رو هم بفرسته. مشکل اینجاست که نمیتونم پایان یافتن دریافت سری اول رو تو برنامه مشخص کنم. ممنون میشم راهنمایی بفرمایید. کد رو هم میذارم و اگه بشه با همین کد درستش کنم ممنون میشم.





Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text


Public Class Frm_server


Private Server As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Private Virtual_Client As Socket
Public EndP As IPEndPoint
Dim t As Integer
Private Recived As [Byte]()


Private Sub Server_BeginAccept(Result As IAsyncResult)


Virtual_Client = Server.EndAccept(Result)
Recived = New [Byte](1) {} '([Byte].MaxValue - 1) {}
Virtual_Client.BeginReceive(Recived, 0, Recived.Length, SocketFlags.None, AddressOf Server_BeginRecived, Nothing)


End Sub


Private Sub Server_BeginRecived(Result As IAsyncResult)
Dim Recv As Integer = Virtual_Client.EndReceive(Result)


If Recv > 0 Then
SetText(Encoding.ASCII.GetString(Recived))


Virtual_Client.BeginReceive(Recived, 0, Recived.Length, SocketFlags.None, AddressOf Server_BeginRecived, Nothing) 'Server_BeginRecived
t += Recv
Else
MsgBox("Recivd End")
End If


End Sub


Private Delegate Sub SetTextCallBack(Text As String)


Private Sub SetText(Text As [String])
If TextBox1.InvokeRequired Then
Dim d As New SetTextCallBack(AddressOf SetText)
Me.Invoke(d, New Object() {Text})
Else
TextBox1.Text += (Text)
End If


End Sub


Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
EndP = New IPEndPoint(IPAddress.Parse(txt_ip.Text), 2010)


Server.Bind(DirectCast(EndP, EndPoint))
Server.Listen(-1)
Server.BeginAccept(AddressOf Server_BeginAccept, Nothing)
End Sub


Private Sub Frm_server_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txt_ip.Text = "127.0.0.1"
End Sub
End Class