Sending a string to the server from a client in C #

I am writing a simple test program to send a string from a client to a server and then display the text in the server program. How I would do it.

Here is my client code.

using System;
using System.Net;
using System.Net.Sockets;


    class client
    {

        static String hostName = Dns.GetHostName();

        public static void Main(String[] args)
        {
            String test = Console.ReadLine();

            IPHostEntry ipEntry = Dns.GetHostByName(hostName);
            IPAddress[] addr = ipEntry.AddressList;

            //The first one in the array is the ip address of the hostname
           IPAddress ipAddress = addr[0];


            TcpClient client = new TcpClient();

            //First parameter is Hostname or IP, second parameter is port on which server is listening
            client.Connect(ipAddress, 8);


            client.Close();
        }
    }

Here is my server code

using System;
using System.Net;
using System.Net.Sockets;

class server
{
    static int port = 8;
    static String hostName = Dns.GetHostName();
    static IPAddress ipAddress;

    public static void Main(String[] args)
    {
        IPHostEntry ipEntry = Dns.GetHostByName(hostName);

        //Get a list of possible ip addresses
        IPAddress[] addr = ipEntry.AddressList;

        //The first one in the array is the ip address of the hostname
        ipAddress = addr[0];

        TcpListener server = new TcpListener(ipAddress,port);

        Console.Write("Listening for Connections on " + hostName + "...\n");

        //start listening for connections
        server.Start();

        //Accept the connection from the client, you are now connected
        Socket connection = server.AcceptSocket();

        Console.Write("You are now connected to the server\n\n");


        int pauseTime = 10000;
        System.Threading.Thread.Sleep(pauseTime);

        connection.Close();


    }


}
+5
source share
1 answer

You can use Sendand Receiveoverload on Socket. An asynchronous version also exists using BeginSomethingand methods EndSomething.

You send raw bytes, so you will need to decide which protocol is simple. To send some text, select the encoding (I would use UTF-8) and get the raw bytes from Encoding.GetBytes.

Example:

Byte[] bytes = Encoding.UTF8.GetBytes("Royale With Cheese");

UTF8 Encoding, .

/ :

int sent = connection.Send(bytes);

:

Byte[] bytes = new Bytes[100]; 
int received = connection.Receive(bytes);

, . , , SocketException. , , , sent received. Send Receive. , ( , , ​​, ?)

- , :

var bytes = Encoding.UTF8.GetBytes("Royale With Cheese");
int count = 0;
while (count < bytes.Length) // if you are brave you can do it all in the condition.
{
    count += connection.Send(
        bytes, 
        count, 
        bytes.Length - count, // This can be anything as long as it doesn't overflow the buffer, bytes.
        SocketFlags.None)
}

Send Receive , , . , - (Socket.SendTimeout Socket.ReceiveTimeout.) 0, , .

? .

int count = 0;
var bytes = new Byte[100];
do
{
    count += connection.Receive(
        bytes,
        count,
        bytes.Length - count,
        SocketFlags.None);
} while (count < bytes.Length);

. ... , 100? , , , . , 100? 100 .

. , , :

var sunIsBurning = true;
while (sunIsBurning) {
    var bytes = new Byte[16];
    int received = socket.Receive(bytes);
    if (received > 0)
        Console.Out.WriteLine("Got {0} bytes. START:{1}END;", received, Encoding.UTF8.GetString(bytes));
}

: " 16 , sunIsBurning, UTF-8". , , , UTF-8 (, ). , .

15 , , 15 . :)

; .

" " , , reset .

+8

All Articles