user1014212
user1014212

Reputation: 1

socket programming in C# server

bellow is my code for server, which runs successfully but small problem is, when i send data from client from twice it accepts once. e.g. if i run this server and client also togethor; first time it accepts data from client, second time when again i ping from client side it does not accept data third time when i ping from client side it accepts data, fourth time when i ping from client it does not accept data, fifth time when i ping from client it accepts data, and so on..... thanking you in advanced.

class Program
{
   //static byte[] Buffer { get; set; }
   //static Socket sck;
   static void Main(string[] args)
   {
       while (true)
       {
           Socket sck;
           sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream,      ProtocolType.Tcp);
           sck.Bind(new IPEndPoint(0, 2000));
           sck.Listen(10);
           Socket accepted = sck.Accept();
           byte [] Buffer = new byte[accepted.SendBufferSize];
           int bytesRead = accepted.Receive(Buffer);
           byte[] formatted = new byte[30];
           for (int i = 0; i < 30; i++)
           {
               formatted[i] = Buffer[i];
           }
           string strData = Encoding.ASCII.GetString(formatted);
           Console.Write(strData + "\r\n");
           sck.Close();
           accepted.Close();
       }
   }
}

Upvotes: 0

Views: 1403

Answers (3)

Kakashi
Kakashi

Reputation: 2195

You need run the server into Thread

 public void StartListener()
        {

            while (true)
            {
                mySocket = myListener.AcceptSocket();
                Console.WriteLine("\r\nsocket type = {0}", mySocket.SocketType);
                if (mySocket.Connected)
                {
                 byte[] receive = new byte[1024];
                 mySocket.Receive(receive, 1024, 0);
                 string sBuffer = Encoding.ASCII.GetString(receive);
                }
           }
   }

Then:

        IPAddress IPaddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
        TcpListener myListener = new TcpListener(IPaddress, 50);
        myListener.Start();
        Thread th = new Thread(new ThreadStart(StartListener));
        th.Start();

More info:

TcpListener Class

Thread

Upvotes: 0

bryanmac
bryanmac

Reputation: 39306

You need a TcpListner

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.71).aspx

Also, to handle many requests you need the server the process multiple requests on different threads. You'll need a thread pool. Look at ThreadPool.QueueUserWorkitem.

Here's a more complete C# TCP server example using that:

http://www.codeproject.com/KB/IP/dotnettcp.aspx

Upvotes: 0

This is not how you normally code a server. Usually, the listener stays up and just accepts new connections and closes them when done. It's possible that on the second attempt the client connects to your old listener just before you close it. Try keeping the listener open or else close the listener as soon as you accept a connection.

Upvotes: 1

Related Questions