Archive for Gennaio 27th, 2008

network socket server

CodeKeep C# Feed Gennaio 27th, 2008

Description: This is a server using a typical socket connection. Also look at the "network socket client" snippet.

Link: http://www.codekeep.net/snippets/d036c60b-57ba-499e-89d1-ec71b4dc0e43.aspx

try {
			IPAddress ipAd = IPAddress.Parse("192.168.0.2"); //use local m/c IP address, and use the same in the client
			TcpListener myList=new TcpListener(ipAd,8001);

			myList.Start();

			Console.WriteLine("The server is running at port 8001...");	
			Console.WriteLine("The local End point is  :" + myList.LocalEndpoint );
			Console.WriteLine("Waiting for a connection.....");

			Socket s=myList.AcceptSocket();
			Console.WriteLine("Connection accepted from "+s.RemoteEndPoint);

			byte[] b=new byte[100];
			int k=s.Receive(b);
			Console.WriteLine("Recieved...");
			for (int i=0;i<k;i++)
				Console.Write(Convert.ToChar(b[i]));

			ASCIIEncoding asen=new ASCIIEncoding();
			s.Send(asen.GetBytes("The string was recieved by the server."));
			Console.WriteLine("\\nSent Acknowledgement");

			s.Close();
			myList.Stop();

}

catch (Exception e) {
			Console.WriteLine("Error..... " + e.StackTrace);
	            }	
	

network socket client

CodeKeep C# Feed Gennaio 27th, 2008

Description: This is a client using typical a socket connection. Also look at the "network socket server" snippet.

Link: http://www.codekeep.net/snippets/3e5a4e72-97ba-44e8-ba7d-682ad3d13fb2.aspx

try {
			TcpClient tcpclnt = new TcpClient();
			Console.WriteLine("Connecting.....");
			
			tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program
			
			Console.WriteLine("Connected");
			Console.Write("Enter the string to be transmitted : ");
			
			String str=Console.ReadLine();
			Stream stm = tcpclnt.GetStream();
						
			ASCIIEncoding asen= new ASCIIEncoding();
			byte[] ba=asen.GetBytes(str);
			Console.WriteLine("Transmitting.....");
			
			stm.Write(ba,0,ba.Length);
			
			byte[] bb=new byte[100];
			int k=stm.Read(bb,0,100);
			
			for (int i=0;i<k;i++)
				Console.Write(Convert.ToChar(bb[i]));
			
			tcpclnt.Close();
}
		
	catch (Exception e) 
        {
			Console.WriteLine("Error..... " + e.StackTrace);
        }

DropDownList - Preenchimento a partir de um grid

CodeKeep C# Feed Gennaio 27th, 2008

Description: Lista as colunas do Grid qua não são templates

Link: http://www.codekeep.net/snippets/1e5928e0-73e5-4c93-9e3a-0e8b3035c9de.aspx

private void LoadGridColumns()
        {
            int j = 0;
            for (int i = 0; i < GridView1.Columns.Count; i ++ )
            {
                if (this.GridView1.Columns[i].HeaderText != "")
                {
                    System.Web.UI.WebControls.ListItem lstItem = new System.Web.UI.WebControls.ListItem();
                    lstItem.Text = this.GridView1.Columns[i].ToString();
                    lstItem.Value = Convert.ToString(i);
                    this.ddlGridColumns.Items.Insert(j, lstItem);
                    j++;
                }
            }
        }