locked
Asynchronous Socket Programming Read but can not Send !... RRS feed

  • Question

  • I am using below code for listening, there is no problem when data was send to me i can capture and read. But I couldn't write a send function  (echo) to same port. In my while true loop i need to look a data and if it is ok then i must send data to socket.

    I will appreciate if someone can help me with that code.

    Thanks.

    Mustafa

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Net;
    using System.Net.Sockets;
    using System.Text.RegularExpressions;
    namespace Server
    {
      class Program
      {
        public class StateObject
        {
          public Socket workSocket = null;
          public const int BUFFER_SIZE = 1024;
          public byte[] buffer = new byte[BUFFER_SIZE];
          public StringBuilder sb = new StringBuilder();
        }
        static void Main(string[] args)
        {
          Socket[] connections = new Socket[1000];
          TcpListener serverSocket = new TcpListener(System.Net.IPAddress.Any, 1067);
          serverSocket.Start();
          int i = 1;
          Console.WriteLine("Waiting for connections...");
          Console.Title = "Server";
          while (true)
          {
            if (serverSocket.Pending() == true)
            {
              Socket s1 = serverSocket.AcceptSocket();
              IPEndPoint clientep = (IPEndPoint)s1.RemoteEndPoint;
              connections[i] = s1;
              StateObject so2 = new StateObject();
              so2.workSocket = s1;
              s1.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(Read_Callback), so2);
              Console.WriteLine(">> Accepted connection from " + clientep.Address + " [id: " + i.ToString() + "]");
              i++;
              Console.Beep(2134, 500);
            }
          }
        }
        public static void Read_Callback(IAsyncResult ar)
        {
          StateObject so = (StateObject)ar.AsyncState;
          Socket s = so.workSocket;
          int read = s.EndReceive(ar);
          if (read > 0)
          {
            so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
            s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,
                         new AsyncCallback(Read_Callback), so);
          }
          else
          {
            if (so.sb.Length > 1)
            {
              //All of the data has been read, so displays it to the console
              string strContent;
              strContent = so.sb.ToString();
              Console.WriteLine(String.Format("Read {0} byte from socket" +
                       "data = {1} ", strContent.Length, strContent));
            }
            s.Close();
          }
        }
      }
    }


    • Edited by Mst71 Tuesday, September 25, 2012 10:50 AM
    • Moved by Jason Dot Wang Wednesday, September 26, 2012 9:13 AM This thread is about Network Class Library(System.Net) (From:Visual C# General)
    Tuesday, September 25, 2012 10:46 AM

All replies

  • You have done everything correctly in your code.

    So you can just echo back from inside your following check:

     if (so.sb.Length > 1)

    using Socket.Send as by creating a byte buffer out of "strContent"

    s.Send
    The same socket should work because TCP is a duplex channel.
    Tuesday, September 25, 2012 11:09 AM
  • You have done everything correctly in your code.

    So you can just echo back from inside your following check:

     if (so.sb.Length > 1)

    using Socket.Send as by creating a byte buffer out of "strContent"

    s.Send
    The same socket should work because TCP is a duplex channel.

    Mr Rajesh,

    First of all thanks for your answer.

    I need to send when my condition occured not when i READ!

    while (true)
          {
            if (serverSocket.Pending() == true)
            {
              ...this part work
            }
            if (my_condition_occured)
            {
              1- Set/Check the Connection??
              2- Send(Data) ???
            }
          }

    How can i do this?

    • Edited by Mst71 Tuesday, September 25, 2012 3:13 PM
    Tuesday, September 25, 2012 2:48 PM
  • Hi Mst71,

      Welcome to MSDN Forum Support.

      You are more likely to get more efficient responses to Network Class Library (System.Net) http://social.msdn.microsoft.com/Forums/en/ncl/threads  where you can contact General  Network Class Library (System.Net)  experts,so I would like to redirect you to appropriate forum for better
    responses.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 26, 2012 9:12 AM
  • SOMEONE PLEASE HELP ME!...
    Wednesday, September 26, 2012 10:14 AM
  • What have you tried? What doesn't work. We're not mind readers. :-)


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Saturday, October 6, 2012 2:14 PM