hey can any1 point me in the direction of making networking aps, like messenging aps and file-sharing.. sockets or whatever.. google isnt working for me like normal.
Printable View
hey can any1 point me in the direction of making networking aps, like messenging aps and file-sharing.. sockets or whatever.. google isnt working for me like normal.
MSDN Search for "client socket" and "server socket" for synchronous and asynchronous examples and explanations.
the example gives me this
VB Code:
Imports System Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.Text ' State object for receiving data from remote device. Public Class StateObject ' Client socket. Public workSocket As Socket = Nothing ' Size of receive buffer. Public BufferSize As Integer = 256 ' Receive buffer. Public buffer(256) As Byte ' Received data string. Public sb As New StringBuilder() End Class 'StateObject Public Class AsynchronousClient ' The port number for the remote device. Private Shared port As Integer = 11000 ' ManualResetEvent instances signal completion. Private Shared connectDone As New ManualResetEvent(False) Private Shared sendDone As New ManualResetEvent(False) Private Shared receiveDone As New ManualResetEvent(False) ' The response from the remote device. Private Shared response As [String] = [String].Empty Private Shared Sub StartClient() ' Connect to a remote device. Try ' Establish the remote endpoint for the socket. ' The name of the ' remote device is "host.contoso.com". Dim ipHostInfo As IPHostEntry = Dns.Resolve("host.contoso.com") Dim ipAddress As IPAddress = ipHostInfo.AddressList(0) Dim remoteEP As New IPEndPoint(ipAddress, port) ' Create a TCP/IP socket. Dim client As New Socket(AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) ' Connect to the remote endpoint. client.BeginConnect(remoteEP, AddressOf ConnectCallback, client) connectDone.WaitOne() ' Send test data to the remote device. Send(client, "This is a test<EOF>") sendDone.WaitOne() ' Receive the response from the remote device. Receive(client) receiveDone.WaitOne() ' Write the response to the console. Console.WriteLine("Response received : {0}", response) ' Release the socket. client.Shutdown(SocketShutdown.Both) client.Close() Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'StartClient Private Shared Sub ConnectCallback(ar As IAsyncResult) Try ' Retrieve the socket from the state object. Dim client As Socket = CType(ar.AsyncState, Socket) ' Complete the connection. client.EndConnect(ar) Console.WriteLine("Socket connected to {0}", _ client.RemoteEndPoint.ToString()) ' Signal that the connection has been made. connectDone.Set() Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'ConnectCallback Private Shared Sub Receive(client As Socket) Try ' Create the state object. Dim state As New StateObject() state.workSocket = client ' Begin receiving the data from the remote device. client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _ AddressOf ReceiveCallback, state) Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'Receive Private Shared Sub ReceiveCallback(ar As IAsyncResult) Try ' Retrieve the state object and client socket ' from the asynchronous state object. Dim state As StateObject = CType(ar.AsyncState, StateObject) Dim client As Socket = state.workSocket ' Read data from the remote device. Dim bytesRead As Integer = client.EndReceive(ar) If bytesRead > 0 Then ' There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _ bytesRead)) ' Get the rest of the data. client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _ AddressOf ReceiveCallback, state) Else ' All the data has arrived; put it in response. If state.sb.Length > 1 Then response = state.sb.ToString() End If ' Signal that all bytes have been received. receiveDone.Set() End If Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'ReceiveCallback Private Shared Sub Send(client As Socket, data As [String]) ' Convert the string data to byte data using ASCII encoding. Dim byteData As Byte() = Encoding.ASCII.GetBytes(data) ' Begin sending the data to the remote device. client.BeginSend(byteData, 0, byteData.Length, 0, _ AddressOf SendCallback, client) End Sub 'Send Private Shared Sub SendCallback(ar As IAsyncResult) Try ' Retrieve the socket from the state object. Dim client As Socket = CType(ar.AsyncState, Socket) ' Complete sending the data to the remote device. Dim bytesSent As Integer = client.EndSend(ar) Console.WriteLine("Sent {0} bytes to server.", bytesSent) ' Signal that all bytes have been sent. sendDone.Set() Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'SendCallback 'Entry point that delegates to C-style main Private Function. Public Overloads Shared Sub Main() System.Environment.ExitCode = _ Main(System.Environment.GetCommandLineArgs()) End Sub Overloads Public Shared Function Main(args() As [String]) As Integer StartClient() Return 0 End Function 'Main End Class 'AsynchronousClient
however im not sure how to use this.. its sorta just shooting code at me.. im sorta looking for more of an article/step-by-step thing. or a compiled example as a VBproject... i just dont know how to make it into one from what they give me.
That's understandable as you jumped right into the asynchronous client example. If you want to understand that code, read the topic "Using an Asynchronous Client Socket".
But I would recommend starting with the synchronous stuff first. I would first read "Using a Synchronous Client Socket", then "Using a Synchronous Server Socket". Then, try out some code with the topics "Synchronous Client Socket Example" and "Synchronous Server Socket Example".
Once you get the synchronous business, then (if you need to), move on to the asynchronous stuff. IMHO, jumping right into asynchronous socket programming is quite a bit to chew. Try out the synchronous stuff first, and post if you have any problems.
Mike
Oh, I should mention that both client and server socket example code is part of the 101 VB.NET examples - check out the sticky - the first topic in the forum to download.
FWIW, I have a few VB.NET books, and I'm not sure if ANY of them have any decent socket code (or any at all for that matter). That's too bad, but MSDN, with a little work, give you everything you need.