Results 1 to 5 of 5

Thread: networking/sockets

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2004
    Posts
    19

    networking/sockets

    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.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    MSDN Search for "client socket" and "server socket" for synchronous and asynchronous examples and explanations.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2004
    Posts
    19

    i dont get it..

    the example gives me this
    VB Code:
    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Imports System.Threading
    5. Imports System.Text
    6.  
    7. ' State object for receiving data from remote device.
    8. Public Class StateObject
    9.     ' Client socket.
    10.     Public workSocket As Socket = Nothing
    11.     ' Size of receive buffer.
    12.     Public BufferSize As Integer = 256
    13.     ' Receive buffer.
    14.     Public buffer(256) As Byte
    15.     ' Received data string.
    16.     Public sb As New StringBuilder()
    17. End Class 'StateObject
    18.  
    19. Public Class AsynchronousClient
    20.     ' The port number for the remote device.
    21.     Private Shared port As Integer = 11000
    22.    
    23.     ' ManualResetEvent instances signal completion.
    24.     Private Shared connectDone As New ManualResetEvent(False)
    25.     Private Shared sendDone As New ManualResetEvent(False)
    26.     Private Shared receiveDone As New ManualResetEvent(False)
    27.    
    28.     ' The response from the remote device.
    29.     Private Shared response As [String] = [String].Empty
    30.    
    31.    
    32.     Private Shared Sub StartClient()
    33.         ' Connect to a remote device.
    34.         Try
    35.             ' Establish the remote endpoint for the socket.
    36.             ' The name of the
    37.             ' remote device is "host.contoso.com".
    38.             Dim ipHostInfo As IPHostEntry = Dns.Resolve("host.contoso.com")
    39.             Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
    40.             Dim remoteEP As New IPEndPoint(ipAddress, port)
    41.            
    42.             '  Create a TCP/IP socket.
    43.             Dim client As New Socket(AddressFamily.InterNetwork, _
    44.                 SocketType.Stream, ProtocolType.Tcp)
    45.            
    46.             ' Connect to the remote endpoint.
    47.             client.BeginConnect(remoteEP, AddressOf ConnectCallback, client)
    48.             connectDone.WaitOne()
    49.            
    50.             ' Send test data to the remote device.
    51.             Send(client, "This is a test<EOF>")
    52.             sendDone.WaitOne()
    53.            
    54.             ' Receive the response from the remote device.
    55.             Receive(client)
    56.             receiveDone.WaitOne()
    57.            
    58.             ' Write the response to the console.
    59.             Console.WriteLine("Response received : {0}", response)
    60.            
    61.             ' Release the socket.
    62.             client.Shutdown(SocketShutdown.Both)
    63.             client.Close()
    64.        
    65.         Catch e As Exception
    66.             Console.WriteLine(e.ToString())
    67.         End Try
    68.     End Sub 'StartClient
    69.    
    70.    
    71.     Private Shared Sub ConnectCallback(ar As IAsyncResult)
    72.         Try
    73.             ' Retrieve the socket from the state object.
    74.             Dim client As Socket = CType(ar.AsyncState, Socket)
    75.            
    76.             ' Complete the connection.
    77.             client.EndConnect(ar)
    78.            
    79.             Console.WriteLine("Socket connected to {0}", _
    80.                 client.RemoteEndPoint.ToString())
    81.            
    82.             ' Signal that the connection has been made.
    83.             connectDone.Set()
    84.         Catch e As Exception
    85.             Console.WriteLine(e.ToString())
    86.         End Try
    87.     End Sub 'ConnectCallback
    88.    
    89.    
    90.     Private Shared Sub Receive(client As Socket)
    91.         Try
    92.             ' Create the state object.
    93.             Dim state As New StateObject()
    94.             state.workSocket = client
    95.            
    96.             ' Begin receiving the data from the remote device.
    97.             client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
    98.                 AddressOf ReceiveCallback, state)
    99.         Catch e As Exception
    100.             Console.WriteLine(e.ToString())
    101.         End Try
    102.     End Sub 'Receive
    103.    
    104.    
    105.     Private Shared Sub ReceiveCallback(ar As IAsyncResult)
    106.         Try
    107.             ' Retrieve the state object and client socket
    108.             ' from the asynchronous state object.
    109.             Dim state As StateObject = CType(ar.AsyncState, StateObject)
    110.             Dim client As Socket = state.workSocket
    111.            
    112.             ' Read data from the remote device.
    113.             Dim bytesRead As Integer = client.EndReceive(ar)
    114.            
    115.             If bytesRead > 0 Then
    116.                 ' There might be more data, so store the data received so far.
    117.                 state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
    118.                     bytesRead))
    119.                
    120.                 ' Get the rest of the data.
    121.                 client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
    122.                     AddressOf ReceiveCallback, state)
    123.             Else
    124.                 ' All the data has arrived; put it in response.
    125.                 If state.sb.Length > 1 Then
    126.                     response = state.sb.ToString()
    127.                 End If
    128.                 ' Signal that all bytes have been received.
    129.                 receiveDone.Set()
    130.             End If
    131.         Catch e As Exception
    132.             Console.WriteLine(e.ToString())
    133.         End Try
    134.     End Sub 'ReceiveCallback
    135.    
    136.    
    137.     Private Shared Sub Send(client As Socket, data As [String])
    138.         ' Convert the string data to byte data using ASCII encoding.
    139.         Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
    140.        
    141.         ' Begin sending the data to the remote device.
    142.         client.BeginSend(byteData, 0, byteData.Length, 0, _
    143.             AddressOf SendCallback, client)
    144.     End Sub 'Send
    145.    
    146.    
    147.     Private Shared Sub SendCallback(ar As IAsyncResult)
    148.         Try
    149.             ' Retrieve the socket from the state object.
    150.             Dim client As Socket = CType(ar.AsyncState, Socket)
    151.            
    152.             ' Complete sending the data to the remote device.
    153.             Dim bytesSent As Integer = client.EndSend(ar)
    154.             Console.WriteLine("Sent {0} bytes to server.", bytesSent)
    155.            
    156.             ' Signal that all bytes have been sent.
    157.             sendDone.Set()
    158.         Catch e As Exception
    159.             Console.WriteLine(e.ToString())
    160.         End Try
    161.     End Sub 'SendCallback
    162.    
    163.     'Entry point that delegates to C-style main Private Function.
    164.     Public Overloads Shared Sub Main()
    165.         System.Environment.ExitCode = _
    166.            Main(System.Environment.GetCommandLineArgs())
    167.     End Sub
    168.    
    169.    
    170.     Overloads Public Shared Function Main(args() As [String]) As Integer
    171.         StartClient()
    172.         Return 0
    173.     End Function 'Main
    174. 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.

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width