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.
Printable View
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.
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
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????
why not just use the winsock control?>
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.
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.
Thanks to all who reply me!!!!!! :D