2 Replies - 8684 Views - Last Post: 25 November 2012 - 07:16 AM Rate Topic: -----

#1 ubis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-November 12

Converting byte array to string array

Posted 25 November 2012 - 03:05 AM

Hello, i'm trying to make simple tcp login with server and client.
I'm getting username and password from client in string array, converting it to byte array and sending to the server.
Here's my client code:

      Console.WriteLine("Please, login: \nUsername: ");
            details[0] = Console.ReadLine();
            Console.WriteLine("Password: ");
            details[1] = Console.ReadLine();
            string result = string.Join(" ", details);
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Selected server IP: " + ipAd + ":" + port);
                tcpclnt.Connect(ipAd, port);

                Stream stm = tcpclnt.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();

                byte[] ba = asen.GetBytes(result);
                stm.Write(ba, 0, ba.Length);

                //Console.WriteLine("Succefully connected! \n");
           }

            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }




Server receives from client byte array, which converts into char and shows in console. Code:

              Socket s = myList.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                    byte[] b = new byte[100];
                    int k = s.Receive(B)/>/>/>;
                    Console.Write("Login auth (" + s.RemoteEndPoint + ") ");
                    for (int i = 0; i < k; i++)
                    {
                        Console.Write(Convert.ToChar(b[i]));
                        
                    }
                    System.Threading.Thread.Sleep(1);


The thing is, i need to get from byte array to string, not chars. I want, that details will be received in someStringArray, so after that i can check if details were correct. Also, i need that received details is in 1 line. Ex.: right now, if i change Console.Write to Console.WriteLine, it shows chars in new newline(if username is root, server receives:
r
o
o
t
...
and so on).

Or maybe someone has got a better solution how to send details?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting byte array to string array

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Converting byte array to string array

Posted 25 November 2012 - 03:18 AM

You are confusing. Do you want it in a string, or a string array? If an array, what delimits the end of a line?

If you want to convert the byte array to a string, just create a new string with it:
String myString = new String(arrayOfBytesHere);

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Converting byte array to string array

Posted 25 November 2012 - 07:16 AM

Since you're trying to figure out how to make a chat application, let's just go directly there and skip the 50 steps of re-invention:


Q: ...make a chat or client-server application?
A:Client/server chat tutorial
Peer-to-peer chat
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1