i found on google in some website when i try to send this on the client Code: Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) the client will show Code: woot wootwootwootwootwootwoot but i like to see Code: woot woot woot woot woot woot whats the problem in the code? any ideas? SERVER CODE Code: Imports System.Net.Sockets Imports System.Net Public Class Form1 Dim serverSocket As Socket Dim clientSocket As Socket Dim byteData(1023) As Byte Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim IpEndPoint As Net.IPEndPoint = New Net.IPEndPoint(IPAddress.Any, 8800) serverSocket.Bind(IpEndPoint) serverSocket.Listen(50) serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing) End Sub Public Sub Send(ByVal msg As String, ByVal client As Socket) Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg) client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New System.AsyncCallback(AddressOf OnSend), client) Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length) End Sub Private Sub OnSend(ByVal ar As IAsyncResult) Dim Client As Socket = ar.AsyncState Client.EndSend(ar) End Sub Delegate Sub _OnRecieve(ByVal ar As IAsyncResult) Private Sub OnRecieve(ByVal ar As IAsyncResult) If InvokeRequired Then Invoke(New _OnRecieve(AddressOf OnRecieve), ar) Exit Sub End If Dim client As Socket = ar.AsyncState Try client.EndReceive(ar) Dim bytesRec As Byte() = byteData Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), clientSocket) Catch ex As Exception End Try End Sub Private Sub OnAccept(ByVal ar As IAsyncResult) clientSocket = serverSocket.EndAccept(ar) serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), clientSocket) AddClient(clientSocket) End Sub Delegate Sub _AddClient(ByVal client As Socket) Private Sub AddClient(ByVal client As Socket) If InvokeRequired Then Invoke(New _AddClient(AddressOf AddClient), client) Exit Sub End If End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) Send("woot", clientSocket) End Sub End Class CLIENT CODE Code: Imports System.Net.Sockets Imports System.Net Public Class Form1 Dim clientSocket As Socket Dim byteData(1023) As Byte Private Sub OnConnect(ByVal ar As IAsyncResult) Try clientSocket.EndConnect(ar) restarttimer() Send("username:" & Environment.UserName, clientSocket) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), clientSocket) Catch ex As Exception reconnectdata() End Try End Sub Public Sub Send(ByVal msg As String, ByVal client As Socket) Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg) client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client) End Sub Private Sub OnSend(ByVal ar As IAsyncResult) Dim client As Socket = ar.AsyncState client.EndSend(ar) End Sub Delegate Sub _OnRecieve(ByVal ar As IAsyncResult) Private Sub OnRecieve(ByVal ar As IAsyncResult) If InvokeRequired Then Invoke(New _OnRecieve(AddressOf OnRecieve), ar) Exit Sub Else Dim client As Socket = ar.AsyncState Try client.EndReceive(ar) Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(byteData) Read(message) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client) Catch ex As Exception clientSocket.Disconnect(False) reconnectdata() End Try End If End Sub Private Sub Read(ByVal msg As String) ListBox1.Items.Add(msg) ListBox1.SelectedIndex = ListBox1.Items.Count - 1 Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load System.Threading.Thread.Sleep(2000) clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim ipAddress As IPAddress = ipAddress.Parse(ini.ReadString("ip")) Dim ipEndPoint As IPEndPoint = New IPEndPoint(ipAddress, ini.ReadString("port")) clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing) End Sub End Class
Can you try with it? Code: Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket)
still the same when i try only these Code: Send("woot" & vbNewLine, clientSocket) Send("woot" & vbNewLine, clientSocket) the output is Code: woot woot when i add more the output will be this Code: woot wootwoot
Have you tried to send a couple of data in one packet instead of doing it separately? Something like that: Code: Send("woot" & vbNewLine & "woot" & vbNewLine & "woot" & vbNewLine & "woot" & vbNewLine & "woot" & vbNewLine & "woot" & vbNewLine & "woot" & vbNewLine & "woot" & vbNewLine, clientSocket) I have seen no issue around your code.
becuase im trying to send words or commands in new line but still the same Send("computerresetcommand",clientsocket) Send("shuttingdown",clientsocket) Send("messageshowme",clientsocket)
so what i did is to count what i send from server to client, the server looks good and ok on the sending, but the client only receive 2, i don't know whats the problem but still working and giving clues
Hmm, ok. Try to replace this code by this another one: OLD: Code: Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length) NEW: Code: byteData = New Byte(1023) { }
yup still the same, damn i think theres something wrong with the client i think, i dont know what line, when i send 3 or more the second and up will stacked each other woot << 1st send wootwootwoot <<< 2nd 3rd and 4th send, i think theres something wrong with this line Code: Delegate Sub _OnRecieve(ByVal ar As IAsyncResult) Private Sub OnRecieve(ByVal ar As IAsyncResult) If InvokeRequired Then Invoke(New _OnRecieve(AddressOf OnRecieve), ar) Exit Sub Else Try Dim client As Socket = ar.AsyncState client.EndReceive(ar) Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(byteData) Read(message) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client) Catch ex As Exception clientSocket.Disconnect(False) reconnectdata() End Try End If End Sub
Try it: Code: Private Sub Read(ByVal msg As String) ListBox1.Items.Insert(ListBox1.Items.Count, msg) ListBox1.SelectedIndex = ListBox1.Items.Count - 1 Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length) End Sub
Have you tried setting the property "Socket.NoDelay" to true? See Here http://msdn.microsoft.com/en-us/lib...aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
modify your output box so when it recives the data does a break and adds a new line. Private Sub Read(ByVal msg As String) If InvokeRequired Then Invoke(New _Read(AddressOf Read), msg) Exit Sub End If RichTextBox1.AppendText(msg & vbCrLf) RichTextBox1.ScrollToCaret() End Sub Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles SendButton.Click Send(InputTextBox.Text & vbNewLine, clientSocket) End Sub this will do the trick, it worked for me. don't forget to changes accordingly in the server and client app.
In the past whenever I was doing client/server stuff I generally always delimited commands just to avoid any situations like this (among others)... maybe there was a better way of doing things but it always worked for me. Send("woot|", clientSocket) Send("woot|", clientSocket) Send("woot|", clientSocket) And on the receiving end split by | and go from there.. whether you have one or many commands mushed together on a line it's easy peasy to process and not get any errors. Code: Dim commands() As String = Split(msg, "|") For Each s As String In commands If Not String.IsNullOrEmpty(s) Then ' perform action End If Next Easy but effective solution.