Introduction :

The Program Socket Programming is used a starter for those who wants to write C# Codes using Net Sockets. It lets you to use Socket Connections with other Systems.

The Server Waits for the Connection and gives a Warm Welcome Message to the User Or the Client.

Tools/API Information :

This Programme it Written using Net.Socket and no extra Feature is used. This is written with the Aim that anyone should understand the Usage of Sockets and NetWorks and to show that C# has got great Support for NetWorking.

Benefit :

There are lots of codes still I am sending this because of its Simplicity and you can use write this Code with just a NotePad/TextPad/Editor.

Server Side Code:

using System;
using System.Net.Sockets;
public class AsynchIOServer
{
public static void Main()
{
TCPListener tcpListener =
new TCPListener(10);
tcpListener.Start();
Socket socketForClient = tcpListener.Accept();
if (socketForClient.Connected)
{
Console.WriteLine("Client connected");
NetworkStream networkStream =
new NetworkStream(socketForClient);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
string theString = "Sending";
streamWriter.WriteLine(theString);
Console.WriteLine(theString);
streamWriter.Flush();
theString = streamReader.ReadLine();
Console.WriteLine(theString);
streamReader.Close();
networkStream.Close();
streamWriter.Close();
}
socketForClient.Close();
Console.WriteLine("Exiting...");
}
}

Client Code:

using System;
using System.Net.Sockets;
public class Client
{
static public void Main( string[] Args )
{
TCPClient socketForServer;
try
{
socketForServer =
new TCPClient("localHost", 10);
}
catch
{
Console.WriteLine(
"Failed to connect to server at {0}:999", "localhost");
return;
}
NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
try
{
string outputString;
// read the data from the host and display it
{
outputString = streamReader.ReadLine();
Console.WriteLine(outputString);
streamWriter.WriteLine("Client Message");
Console.WriteLine("Client Message");
streamWriter.Flush();
}
}
catch
{
Console.WriteLine("Exception reading from Server");
}
// tidy up
networkStream.Close();
}
}

 

           

 

/*
The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.
A 'ListBox' and a 'Button' control are added to a form, containing a delegate
which encapsulates a method that adds items to the listbox.This function is executed
on the thread that owns the underlying handle of the form. When user clicks on button
the above delegate is executed using 'Invoke' method.


*/


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

   public class MyFormControl : Form
   {
      public delegate void AddListItem();
      public AddListItem myDelegate;
      private Button myButton;
      private Thread myThread;
      private ListBox myListBox;
      public MyFormControl()
      {
         myButton = new Button();
         myListBox = new ListBox();
         myButton.Location = new Point(72, 160);
         myButton.Size = new Size(152, 32);
         myButton.TabIndex = 1;
         myButton.Text = "Add items in list box";
         myButton.Click += new EventHandler(Button_Click);
         myListBox.Location = new Point(48, 32);
         myListBox.Name = "myListBox";
         myListBox.Size = new Size(200, 95);
         myListBox.TabIndex = 2;
         ClientSize = new Size(292, 273);
         Controls.AddRange(new Control[] {myListBox,myButton});
         Text = " 'Control_Invoke' example";
         myDelegate = new AddListItem(AddListItemMethod);
      }
      static void Main()
      {
         MyFormControl myForm = new MyFormControl();
         myForm.ShowDialog();
      }
      public void AddListItemMethod()
      {
         String myItem;
         for(int i=1;i<6;i++)
         {
            myItem = "MyListItem" + i.ToString();
            myListBox.Items.Add(myItem);
            myListBox.Update();
            Thread.Sleep(300);
         }
      }
      private void Button_Click(object sender, EventArgs e)
      {
         myThread = new Thread(new ThreadStart(ThreadFunction));
         myThread.Start();
      }
      private void ThreadFunction()
      {
         MyThreadClass myThreadClassObject  = new MyThreadClass(this);
         myThreadClassObject.Run();
      }
   }

// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// containing a delegate which encapsulates a method that adds items to the listbox.

   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }

      public void Run()
      {
         // Execute the specified delegate on the thread that owns
         // 'myFormControl1' control's underlying window handle.
         myFormControl1.Invoke(myFormControl1.myDelegate);
      }
   }

C# Dictionary 키 나열하기

Window based/C# | 2010/01/05 12:00 | Noonipoony

 

           

 

SerializableDictionary<String, LogData>.KeyCollection keyColl = m_dicLogData.Keys;


   foreach (String key in keyColl)
            {
    // 날짜 별로 정렬
    TreeNode currentDateNode = Control_Log_TreeList.Nodes[TreeRootKey].Nodes[m_dicLogData[key].Date];
    if (currentDateNode == null)
    {
     currentDateNode = Control_Log_TreeList.Nodes[TreeRootKey].Nodes.Add(m_dicLogData[key].Date, m_dicLogData[key].Date);
    }
    currentDateNode.Nodes.Add(key, m_dicLogData[key].Title);

            }

 

           

 

BackgroundWorker bwConnector = new BackgroundWorker();
            bwConnector.DoWork += new DoWorkEventHandler(bwConnector_DoWork);
            bwConnector.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwConnector_RunWorkerCompleted);
            bwConnector.RunWorkerAsync();

 

           

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ServerLib
{
 [XmlRoot("dictionary")]

 public class SerializableDictionary<TKey, TValue>
  : Dictionary<TKey, TValue>, IXmlSerializable
 {
  #region IXmlSerializable Members
  public System.Xml.Schema.XmlSchema GetSchema()
  {
   return null;
  }

  public void ReadXml(System.Xml.XmlReader reader)
  {
   XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
   XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

   bool wasEmpty = reader.IsEmptyElement;
   reader.Read();

   if (wasEmpty)
    return;


   while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
   {
    reader.ReadStartElement("item");
    reader.ReadStartElement("key");

    TKey key = (TKey)keySerializer.Deserialize(reader);

    reader.ReadEndElement();
    reader.ReadStartElement("value");
    TValue value = (TValue)valueSerializer.Deserialize(reader);
    reader.ReadEndElement();
    this.Add(key, value);

    reader.ReadEndElement();
    reader.MoveToContent();
   }
   reader.ReadEndElement();
  }

  public void WriteXml(System.Xml.XmlWriter writer)
  {
   XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
   XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));


   foreach (TKey key in this.Keys)
   {
    writer.WriteStartElement("item");
    writer.WriteStartElement("key");
    keySerializer.Serialize(writer, key);
    writer.WriteEndElement();
    writer.WriteStartElement("value");
    TValue value = this[key];
    valueSerializer.Serialize(writer, value);
    writer.WriteEndElement();
    writer.WriteEndElement();
   }
  }
  #endregion
 }
}

이전 1 2 3 4 5 ... 32 다음