0

I have made a small server program using c# socket which will send a welcome message to the client as soon as it is connected and the client will send an id and the server will receive it. Now I want to send the following array to the client as soon as the client is connected (that means soon after sending the welcome message)

string[] array = new string[] { "apple", "cat", "dog" };

My sample code is given below:

Server:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;


namespace server
{
    class Program
    {
        static void Main(string[] args)
    {            

        TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
        tcpListener.Start();

        while (true)
        {

           TcpClient tcpClient = tcpListener.AcceptTcpClient();

            byte[] data = new byte[1024];
            NetworkStream ns = tcpClient.GetStream();

            string welcome = "Welcome to my test server";
            data = Encoding.ASCII.GetBytes(welcome);
            ns.Write(data, 0, data.Length);

           int recv = ns.Read(data, 0, data.Length);

            string id = Encoding.ASCII.GetString(data, 0, recv);
            StreamReader reader = new StreamReader(tcpClient.GetStream());


             }
    }   
}
}

How will I send the array at a time so that the client can receive it from the client end?

  • Send complete string including commas between characters. string output = string.Join(",", array); – jdweng Sep 28 '15 at 16:47
  • In this case there must be a character that is never part of the Strings. If this is true, the it works. Comma is a Bad choice, I suggest one of the control chars for this (0x00 to 0x1F). – Tobias Knauss Sep 28 '15 at 17:43

1 Answers1

0

The data which is sent over the network is just a bunch of bytes. In your case, as you are using TCP, the data is encapsulated into a TCP packet. The creation of the data by the sender and its interpretation by the receiver is completely application specific.

This means, sending an integer value over TCP, you should use the Bitconverter class, GetBytes(Int32) and ToInt32(byte[ ], ...). If you have a .net app on both sides, you can use these 2 functions. If one side is not .net, you need to implement the algorithm for creating/parsing bytes by yourself.

On Strings, you first need to decide whether you want to send ASCII or Unicode. I prefer the 2nd, because this is what .net uses in a String. For retrieving the bytes, see another SO post.

Your final problem is sending the complete array. You could build your own kind of high level protocol for this, i.e. (raw data):

array type=string len=3 data len=5 "apple" data len=3 "cat" data len=3 "dog"

This universal approach would be platform independant, because .net information about an array (type, size,...) and its content very likely differ from other programming languages (if there existent at all).

If you stick to .net, then you perhaps can omit the whole stuff above and should look into serialization, i.e. search for array serialization. Also look at this SO post. The asking person probably used a filestream. Other solutions are described as well.

Community
  • 1
  • 1
Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45