Results 1 to 7 of 7

Thread: System.net.sockets :D

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    113

    Wink System.net.sockets :D

    i know how to connect to other pc but i don´t know how to send data, can someone write me a code or explain me how to send data system.net.sockets.

  2. #2
    Junior Member
    Join Date
    Aug 2002
    Posts
    26
    Hi, what do you mean you know how to connect?

    Can you send a message to another machine?

    Do you want to send files??

    Imi

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    113
    I don´t know how to send text and files, but i know how to connect

    imports system.net.sockets

    dim tcpsrv as new tcplistener(port)

    tcplistener.start

    and in the client

    dim tcpc as new tcpclient

    tcpc.connect(host,port)

    but i want to send files and text how can i do that????

  4. #4
    New Member
    Join Date
    Jul 2002
    Posts
    12
    why not just use the winsock control?>

  5. #5
    Junior Member
    Join Date
    Aug 2002
    Posts
    26
    Well if you say that...

    why not just use VB6..........


    why not VB5?........

    The point is with .NET you shouldn't have to use any VB6 controls.

  6. #6
    Junior Member
    Join Date
    Aug 2002
    Posts
    26
    Try this pamtru, taken from the MSDN help files

    or try here http://msdn.microsoft.com/vbasic/dow...isualbasic.asp

    they have the downloadable client and listener projects. (look for the sockets link)


    client code
    -------------------------------------------------------------
    Dim tcpClient As New TcpClient()
    Try
    tcpClient.Connect("www.contoso.com", 11000)
    Dim networkStream As NetworkStream = tcpClient.GetStream()

    If networkStream.CanWrite And networkStream.CanRead Then

    ' Does a simple write.
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
    networkStream.Write(sendBytes, 0, sendBytes.Length)

    ' Reads the NetworkStream into a byte buffer.
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

    ' Returns the data received from the host to the console.
    Dim returndata As String = Encoding.ASCII.GetString(bytes)
    Console.WriteLine(("This is what the host returned to you: " + returndata))

    Else
    If Not networkStream.CanRead Then
    Console.WriteLine("You can not write data to this stream")
    tcpClient.Close()
    Else
    If Not networkStream.CanWrite Then
    Console.WriteLine("You can not read data from this stream")
    tcpClient.Close()
    End If
    End If
    End If
    Catch e As Exception
    Console.WriteLine(e.ToString())
    End Try
    ----------------------------------------------------

    Listener code

    ----------------------------------------------------
    Const portNumber As Integer = 13
    Dim tcpListener As New TcpListener(portNumber)

    tcpListener.Start()

    Console.WriteLine("Waiting for a connection....")

    Try

    'Accept the pending client connection and return a TcpClient initialized for communication.
    Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
    Console.WriteLine("Connection accepted.")

    Dim networkStream As NetworkStream = tcpClient.GetStream()

    Dim responseString As String = "You have successfully connected to me."

    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
    networkStream.Write(sendBytes, 0, sendBytes.Length)

    Console.WriteLine(("Message Sent /> : " + responseString))

    'Any communication with the remote client using the TcpClient can go here.
    '
    '//////
    'Close TcpListener and TcpClient.
    tcpClient.Close()
    tcpListener.Stop()

    Catch e As Exception
    Console.WriteLine(e.ToString())
    End Try

    -------------------------------------------------

    Hope this helps

    Imogen

    PS sorry its seems to have lost its indentation.... dont know how to cure that. You should get the idea though.
    Last edited by Imogen; Sep 1st, 2002 at 06:38 AM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    113
    Thanks to all who reply me!!!!!!

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