To send a message to a TCP server using C#, you can use the TcpClient
class provided in the System.Net.Sockets
namespace.
Here's an example code snippet that shows how you can use TcpClient
to send a message to a TCP server:
main.cs867 chars36 lines
In this example, we create a new TcpClient
object and connect to a server on localhost
port 1234
. We then get a NetworkStream
object for writing data to the server. We convert the message string "Hello, server!" to a byte array, and use the Write
method of the NetworkStream
object to send the message to the server. Finally, we close the connection.
Note that the Encoding.ASCII.GetBytes
method is used to convert the message string to a byte array. This ensures that the message is in a format that can be transmitted over the network.
gistlibby LogSnag