Carloine menths

Carloine menths

  • 2.1k
  • 41
  • 23.1k

Server and client connection

Feb 11 2018 6:27 PM
Hello,
 
I want to send  data to server the problem is only the first data is sent . 
server code:
  1. namespace Server  
  2. {  
  3.     class Program  
  4.     {  
  5.         static byte[] Buffer { getset; }  
  6.         static Socket sck;  
  7.         static void Main(string[] args)  
  8.         {  
  9.             sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  10.             sck.Bind(new IPEndPoint(0, 2000));  
  11.             sck.Listen(100);  
  12.             Socket accepted = sck.Accept();  
  13.             Buffer = new byte[accepted.SendBufferSize];  
  14.             int bytesRead = accepted.Receive(Buffer);  
  15.             byte[] formatted = new byte[bytesRead];  
  16.             for (int i = 0; i < bytesRead; i++)  
  17.             {  
  18.                 formatted[i] = Buffer[i];  
  19.             }  
  20.             string strData = Encoding.ASCII.GetString(formatted);  
  21.             Console.Write(strData + "\r\n");  
  22.             Console.Read();  
  23.             sck.Close();  
  24.             accepted.Close();  
  25.         }  
  26.     }  

 client code:
  1. namespace Client  
  2. {  
  3.     class Program  
  4.     {  
  5.         static Socket sck;  
  6.         static void Main(string[] args)  
  7.         {  
  8.             sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  9.             IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);  
  10.             try  
  11.             {  
  12.                 sck.Connect(localEndPoint);  
  13.             }  
  14.             catch  
  15.             {  
  16.                 Console.Write("Unable to connect to remote end point!\r\n");  
  17.                 Main(args);  
  18.             }  
  19.             Console.Write("Your name: ");  
  20.             string text = Console.ReadLine();  
  21.             byte[] data = Encoding.ASCII.GetBytes(text);  
  22.   
  23.             sck.Send(data);  
  24.             Console.Read();  
  25.             Console.Write("Your age: ");  
  26.             string text1 = Console.ReadLine();  
  27.             byte[] data1 = Encoding.ASCII.GetBytes(text1);  
  28.   
  29.             sck.Send(data1);  
  30.             Console.Write("Data Sent!\r\n");  
  31.             Console.Write("Press any key to continue...");  
  32.             Console.Read();  
  33.             sck.Close();  
  34.         }  
  35.     }  

 

Answers (1)