Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. socket

socket

Scheduled Pinned Locked Moved C#
sysadmindata-structureshelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurelcly
    wrote on last edited by
    #1

    I have a small problem to send a collection or array via socket as we know we can only send byte. This is a small example of what I wanna do but it is not work:

    List list = new List();
    list.Add("item1");
    list.Add("item1");
    sockt.Send(list);

    I try this small piece of code but I do not received what I need

    //////// Client Application

    String username = Console.ReadLine();
    String password = Console.ReadLine();

    byte[] msg1 = Encoding.ASCII.GetBytes(username);
    byte[] msg2 = Encoding.ASCII.GetBytes(password);

    // Build the buffers for the send.
    List<ArraySegment<byte>> sendBuffers = new List<ArraySegment<byte>>(2);

    sendBuffers.Add(new ArraySegment<byte>(msg1));

    sendBuffers.Add(new ArraySegment<byte>(msg2));

    client.Send(sendBuffers);

    //////// Server Application

    // Build the buffers for the receive.
    List<ArraySegment<byte>> recvBuffers = new List<ArraySegment<byte>>(2);

    byte[] username = new byte[10];
    byte[] password = new byte[10];

    // Specify the first buffer
    recvBuffers.Add(new ArraySegment<byte>(username));

    // Specify the second buffer
    recvBuffers.Add(new ArraySegment<byte>(password));

    try
    {
    int bytesReceived = sock.Receive(recvBuffers);
    }
    catch
    {
    }

    sockt.Received(recvBuffers);

    Now if in Client enter toto as username and 123456 as password in server I received toto123456 no totoand 123456 seperatly What can I proceed.

    L P 2 Replies Last reply
    0
    • A aurelcly

      I have a small problem to send a collection or array via socket as we know we can only send byte. This is a small example of what I wanna do but it is not work:

      List list = new List();
      list.Add("item1");
      list.Add("item1");
      sockt.Send(list);

      I try this small piece of code but I do not received what I need

      //////// Client Application

      String username = Console.ReadLine();
      String password = Console.ReadLine();

      byte[] msg1 = Encoding.ASCII.GetBytes(username);
      byte[] msg2 = Encoding.ASCII.GetBytes(password);

      // Build the buffers for the send.
      List<ArraySegment<byte>> sendBuffers = new List<ArraySegment<byte>>(2);

      sendBuffers.Add(new ArraySegment<byte>(msg1));

      sendBuffers.Add(new ArraySegment<byte>(msg2));

      client.Send(sendBuffers);

      //////// Server Application

      // Build the buffers for the receive.
      List<ArraySegment<byte>> recvBuffers = new List<ArraySegment<byte>>(2);

      byte[] username = new byte[10];
      byte[] password = new byte[10];

      // Specify the first buffer
      recvBuffers.Add(new ArraySegment<byte>(username));

      // Specify the second buffer
      recvBuffers.Add(new ArraySegment<byte>(password));

      try
      {
      int bytesReceived = sock.Receive(recvBuffers);
      }
      catch
      {
      }

      sockt.Received(recvBuffers);

      Now if in Client enter toto as username and 123456 as password in server I received toto123456 no totoand 123456 seperatly What can I proceed.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to convert your messages into continuous byte streams, rather than sending multiple messages.

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      1 Reply Last reply
      0
      • A aurelcly

        I have a small problem to send a collection or array via socket as we know we can only send byte. This is a small example of what I wanna do but it is not work:

        List list = new List();
        list.Add("item1");
        list.Add("item1");
        sockt.Send(list);

        I try this small piece of code but I do not received what I need

        //////// Client Application

        String username = Console.ReadLine();
        String password = Console.ReadLine();

        byte[] msg1 = Encoding.ASCII.GetBytes(username);
        byte[] msg2 = Encoding.ASCII.GetBytes(password);

        // Build the buffers for the send.
        List<ArraySegment<byte>> sendBuffers = new List<ArraySegment<byte>>(2);

        sendBuffers.Add(new ArraySegment<byte>(msg1));

        sendBuffers.Add(new ArraySegment<byte>(msg2));

        client.Send(sendBuffers);

        //////// Server Application

        // Build the buffers for the receive.
        List<ArraySegment<byte>> recvBuffers = new List<ArraySegment<byte>>(2);

        byte[] username = new byte[10];
        byte[] password = new byte[10];

        // Specify the first buffer
        recvBuffers.Add(new ArraySegment<byte>(username));

        // Specify the second buffer
        recvBuffers.Add(new ArraySegment<byte>(password));

        try
        {
        int bytesReceived = sock.Receive(recvBuffers);
        }
        catch
        {
        }

        sockt.Received(recvBuffers);

        Now if in Client enter toto as username and 123456 as password in server I received toto123456 no totoand 123456 seperatly What can I proceed.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Shouldn't the client wait for a prompt before sending the data? And perhaps include a carriage return?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups