Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket class or TCPCllient class

Status
Not open for further replies.

leangross

IS-IT--Management
Jul 3, 2002
84
PH
not sure if i should post this here or at the other forum.. anyway, im new in socket and network programming.... just wondering which class to use...


I will be sending file from a client to server... just want to know what classes i need to know....

Socket class has a method that send and receive data and i also read TcpClient classs has these methods also.. and both can send data to the network....

How will i know which one is appropriete or when to use socket class or tcpclient class?

what are their differences?

hope someone could explain to me the use of each and their relation if they have..

thanks!!
 
TcpClient class is a wrapper for the socket class (though you can't easily access the underlying socket - stream yes, but not socket - without inheriting from TcpClient).

Both are used for communicating across a network. The Socket is a much more powerful class allowing you to control just about everything in the connection, while the TcpClient is simpler (in setup, not necessarily use) TcpClient is limited to TCP connections, while with a Socket you can use TCP, UDP, and others.

In some ways though using a socket is simpler, for example sending data (I have not actually used a TcpClient, so I am basing this off of the methods/properties I can see).

With TcpClient it would appear you have to retrieve the underlying NetworkStream to read and write to the network:
Code:
        Dim x As New System.Net.Sockets.TcpClient
        Dim ns As System.Net.Sockets.NetworkStream = x.GetStream()
while with a Socket you just call the methods
Code:
        Dim byte_array() As Byte
        Dim y As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        y.Send(byte_array, byte_array.Length, Net.Sockets.SocketFlags.None)

I seem to throw out this link a lot, but that's because I think it's just a great control for this sort of situation:

That is a link to a wrapper for the Socket class that emulates the old Winsock control. It's a lot easier to use than either the TcpClient or the Socket class - and provides some useful functionality as well.
 
thank you so much!!!! I check the url you gave. thanks again....
 
Hi one more thing... could you differentiate the TCP,UDP and other type of connection or the differenct protocol type?

Which one to use?

Thanks in advance!!!
 
TCP is the standard protocol in use. It is a connection-based protocol, meaning it established a 2-way connection that is available the entire time it is connected. This is the one you'll probably want to use.

UDP is a connectionless protocol. This means that a server is listening for data and clients don't connect to the server, they just send data in the server's direction and hope that the data gets there fine.

I don't know much about the other protocols (other than they are there). Here are a few of them: ICMP, Spx, Raw, etc.
 
hi thanks!!!

I got this sample code from
It has client and server application sample. Im just wondering how could i test the sample code if it working properly.

I believe there should be an application (on the server) that will receive the information from client and throw back information to the client. How could I place a code to the server? How will i know which port the server application is assinged to?


This is the sample code i downloaded
client Code:
Code:
Imports System.Net.Sockets
Imports System.Text
Class TCPCli
    Shared Sub Main()

        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect("127.0.0.1", 8000)
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
            ' Do a simple write.
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Host returned: " + returndata))
        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    tcpClient.Close()
                End If
            End If
        End If
        ' pause so user can view the console output
        Console.ReadLine()
    End Sub
End Class

TCPServer Code

Code:
Imports System.Net.Sockets
Imports System.Text
Class TCPSrv
    Shared Sub Main()
        ' Must listen on correct port- must be same as port client wants to connect on.
        Const portNumber As Integer = 8000
        Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
        Console.WriteLine("Waiting for connection...")
        Try
            'Accept the pending client connection and return a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            Console.WriteLine("Connection accepted.")
            ' Get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            ' Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Return the data received from the client to the console.
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Client sent: " + clientdata))
            Dim responseString As String = "Connected to server."
            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()
            Console.WriteLine("exit")
            Console.ReadLine()
        Catch e As Exception
            Console.WriteLine(e.ToString())
            Console.ReadLine()
        End Try
    End Sub
End Class


I would really appreciate if you could help me on this one. Thank you so much
 
In order to test that, you need to run the server first - and then run the client.

From Server:
Code:
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
tcpListener.Start()

This tells me that the server will be listening on port 8000.

From Client:
Code:
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("127.0.0.1", 8000)

This tells me that the client will attempt to connect to a server on the local host on port 8000.

That code looks like it should work - just remember, that code is for a console application.

Also looking at the code, it is a synchronous application - which means it will sit and wait at each network operation before moving on to the next step. While synchronous may be fine for your needs, you may want to consider an asynchronous solution which would allow you application to continue working while waiting for a response from a client.
 
hi... one more question. if i will send/receive data using the internet,will i still use TCP connection or something else? is TCPClient for intranet only? how about socket, is it for internet connection only?


Thanks in advance!!!
 
Either will work for both Internet or Intranet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top