Jump to content

Recommended Posts

Posted

lol

using System;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication1
{
   class Program
   {
       static void Main(string[] args)
       {
           TcpListener serverSocket = new TcpListener(8888);
           int requestCount = 0;
           TcpClient clientSocket = default(TcpClient);
           serverSocket.Start();
           Console.WriteLine(" >> Server Started");
           clientSocket = serverSocket.AcceptTcpClient();
           Console.WriteLine(" >> Accept connection from client");
           requestCount = 0;

           while ((true))
           {
               try
               {
                   requestCount = requestCount + 1;
                   NetworkStream networkStream = clientSocket.GetStream();
                   byte[] bytesFrom = new byte[10025];
                   networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                   string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                   dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                   Console.WriteLine(" >> Data from client - " + dataFromClient);
                   string serverResponse = "Server response " + Convert.ToString(requestCount);
                   Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                   networkStream.Write(sendBytes, 0, sendBytes.Length);
                   networkStream.Flush();
                   Console.WriteLine(" >> " + serverResponse);
               }
               catch (Exception ex)
               {
                   Console.WriteLine(ex.ToString());
               }
           }

           clientSocket.Close();
           serverSocket.Stop();
           Console.WriteLine(" >> exit");
           Console.ReadLine();
       }
   }
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...