zheng's profileWindows Live 共享空间PhotosBlogGuestbookMore Tools Help

Windows Live 共享空间

zheng

There are no photo albums.
April 01

C语言之旅(1)

自己看了这么久的c语言居然连一个struct都不会写 ,我滴神啊,于是抱着书狂啃.终于在我不懈的努力之后我终于.....学会了写一个简单的struct尴尬.
typedef struct MyStruct
{
 int data;
 struct MyStruct *link;
};
OK,完成了.
January 07

Windows Socket编程

服务器端代码如下:

using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.IO;

namespace WindowsSocket
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 1234;

            IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];

            //监听(ip地址,端口)
            TcpListener tcpListener = new TcpListener(ipAddress, port);
            tcpListener.Start();
            Console.WriteLine("Server Started");

            Socket socketForClient = tcpListener.AcceptSocket();

            try
            {
                if (socketForClient.Connected)
                {
                    while (true)
                    {
                        Console.WriteLine("Client connected");
                        NetworkStream networkStream = new NetworkStream(socketForClient);
                        StreamWriter streamWriter = new StreamWriter(networkStream);
                        StreamReader streamReader = new StreamReader(networkStream);
                        string line = streamReader.ReadLine();
                        Console.WriteLine("Read:" + line);
                        streamWriter.WriteLine(line);
                        Console.WriteLine("Wrote:" + line);
                        streamWriter.Flush();
                    }
                }
                socketForClient.Close();
                Console.WriteLine("Exiting");
            }
            catch(Exception  ex)
            {
                throw ex;
            }
        }
    }
}

 

客户端代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace NetSocket
{
    public partial class NetSocket : Form
    {
        private NetworkStream networkStream;
        private StreamReader streamReader;
        private StreamWriter streamWriter;
        ArrayList sb;
        TcpClient myclient;
        bool flag = false;       
        public NetSocket()
        {
            InitializeComponent();
            sb = new ArrayList();
            if (!flag)
            {
                Connect();
                networkStream = myclient.GetStream();
                streamReader = new StreamReader(networkStream);
                streamWriter = new StreamWriter(networkStream);
                ShowMessage();
            }
        }

        //Connecting
        protected void Connect()
        {
            try
            {
                sb.Add("Connecting to Server...");
                myclient = new TcpClient("localhost", 1234);
                sb.Add("Connected,Please enter something in the textbox");
            }
            catch
            {
                sb.Add(string.Format("Failed to connect to server at{0}:1234", "localhost"))
;
            }
            flag = true;
        }

        //Show Message
        protected void ShowMessage()
        {
            for (int i = 0; i < sb.Count; i++)
            {
                listBox1.Items.Add((object)sb[i].ToString());
            }
            sb.Clear();
        }

        private void txbText_TextChanged(object sender, EventArgs e)
        {
            if (txbText.Text == "")
            {
                btnApply.Enabled = false;
            }
            else
            {
                btnApply.Enabled = true;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (txbText.Text == "")
            {
                sb.Add("Please enter something in the textbox");
                txbText.Focus();
                return;
            }
            string s;
            try
            {
                streamWriter.WriteLine(txbText.Text);
                Console.WriteLine("Sending Message");
                streamWriter.Flush();
                s = streamReader.ReadLine();
                Console.WriteLine("Reading Message");
                Console.WriteLine(s);
                sb.Add(s);
                txbText.Text = "";
                txbText.Focus();
                ShowMessage();
            }
            catch
            {
                MessageBox.Show("Error.");
            }
        }
    }
}

December 18

神奇的关键字where

以下内容搞自MSDN

http://msdn2.microsoft.com/zh-cn/library/6b0scde8(VS.80).aspx

C# 语言参考

where(C# 参考)

where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量。例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就可以实现 IComparable<T> 接口:

复制代码

public class MyGenericClass<T> where T:IComparable { }

除了接口约束,where 子句还可以包括基类约束,以指出某个类型必须将指定的类作为基类(或者就是该类本身),才能用作该泛型类型的类型参数。这样的约束一经使用,就必须出现在该类型参数的所有其他约束之前。

复制代码

// cs_where.cs
// compile with: /target:library
using System;

class MyClassy<T, U>
    where T : class
    where U : struct
{
}

where 子句还可以包括构造函数约束。可以使用 new 运算符创建类型参数的实例;但类型参数为此必须受构造函数约束 new() 的约束。new() 约束可以让编译器知道:提供的任何类型参数都必须具有可访问的无参数(或默认)构造函数。例如:

复制代码

// cs_where_2.cs
// compile with: /target:library
using System;
public class MyGenericClass <T> where T: IComparable, new()
{
    // The following line is not possible without new() constraint:
    T item = new T();
}

new() 约束出现在 where 子句的最后。

对于多个类型参数,每个类型参数都使用一个 where 子句,例如:

复制代码

// cs_where_3.cs
// compile with: /target:library
using System;
using System.Collections;

interface MyI
{
}

class Dictionary<TKey,TVal>
    where TKey: IComparable, IEnumerable
    where TVal: MyI
{
    public void Add(TKey key, TVal val)
    {
    }
}

还可以将约束附加到泛型方法的类型参数,例如:

复制代码

public bool MyMethod<T>(T t) where T : IMyInterface { }

请注意,对于委托和方法两者来说,描述类型参数约束的语法是一样的:

复制代码

delegate T MyDelegate<T>() where T : new()

有关泛型委托的信息,请参见泛型委托

神奇的上下文关键字 partial

以下内容摘自MSDN

http://msdn2.microsoft.com/zh-cn/library/wbx7zzdd(VS.80).aspx

C# 语言参考

partial(C# 参考)

分部类型定义允许将类、结构或接口的定义拆分到多个文件中

(签名必须一致)

 

Guestbook

An error occurred loading this module.