首页 新闻 会员 周边

求救高手!c#问题

0
悬赏园豆:100 [已关闭问题]

1.编写一个简单的c/s程序,客户端发送个字符串,服务端接收后显示在屏幕上!

 

2.实现3个线程,同时赋值一个整型变量,一个线程同时读取,并显示此变量的值,并要求变量从0开始依次累加显示(1,2,3,4,5,6...

 

)

木+头的主页 木+头 | 初学一级 | 园豆:105
提问于:2010-04-14 14:20
< >
分享
其他回答(2)
0

你这是操作系统习题吧?

AlexLiu | 园豆:145 (初学一级) | 2010-04-14 16:20
不知道啊,好像是朋友的面试题
支持(0) 反对(0) 木+头 | 园豆:105 (初学一级) | 2010-04-14 17:11
0

呵呵,最近正好了解线程方面的,贴第二题:(我测试了下。基本上所有数都会按顺序并且连续的输出。。你自己也测下。)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

 

namespace ConsoleApplication1

{

    class Program

    {

        static bool IsContinue = true;

        static int currentNum = -1;

        static void Main(string[] args)

        {

            ThreadStart ts = new ThreadStart(delegate()

            {

                do

                {

                    Program.IncreaseNum();

                }

                while (IsContinue);

            });

            Thread writeThread1 = new Thread(ts);

            Thread writeThread2 = new Thread(ts);

            Thread writeThread3 = new Thread(ts);

            ThreadStart readTs = new ThreadStart(delegate()

            {

                do

                {

                    int num = Program.ReadNum();

                    if (currentNum != num)

                    {

                        Console.Write(num.ToString()+"\n");

                        currentNum = num;

                    }

                }

                while (IsContinue);

            });

            Thread readThread = new Thread(readTs);

            readThread.Start();

            writeThread1.Start();

            writeThread2.Start();

            writeThread3.Start();

            //readThread.Start(

        }

 

        public static int Num = 0;

        public static ReaderWriterLock RWL = new ReaderWriterLock();

 

        public static void IncreaseNum()

        {

            RWL.AcquireWriterLock(Timeout.Infinite);

            try

            {

                Num++;

                Thread.Sleep(Num);

            }

            finally

            {

                RWL.ReleaseReaderLock();

            }

 

        }

 

        public static int ReadNum()

        {

            RWL.AcquireReaderLock(Timeout.Infinite);

            try

            {

                return Num;

            }

            finally

            {

                RWL.ReleaseReaderLock();

            }

        }

    }

}

lsjwzh | 园豆:113 (初学一级) | 2010-04-14 23:16
谢谢兄弟的答案!
支持(0) 反对(0) 木+头 | 园豆:105 (初学一级) | 2010-04-15 08:14
0

刚好以前写过一个测试通讯是正常的例子:

第一题:

服务端代码:

代码
static void Main(string[] args)
{
System.Net.Sockets.TcpListener lister
= new System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse("192.168.1.108"), 9643);
lister.Start();
System.Net.Sockets.NetworkStream stream
= null;
byte[] data = new byte[256];
Write(
"正在等待链接...");
System.Net.Sockets.TcpClient server
= lister.AcceptTcpClient();
bool connect = false;
while (true)
{
stream
= server.GetStream();
int i;
try
{
while ((i = stream.Read(data, 0, data.Length)) != 0)
{
if (!connect)
{
connect
= true;
Write(
"已建立链接...");
}
Write(System.Text.Encoding.Default.GetString(data));
}
}
catch
{
Write(
"已链接已关闭...");
}
}
}
public static void Write(string msg)
{
Console.WriteLine(msg.Trim());
}

}

 

客户端代码:

代码
static void Main(string[] args)
{
TcpClient client
= new TcpClient();
client.Connect(IPAddress.Parse(
"192.168.1.108"), 9643);
Byte[] data
= null;
string msg = null;
NetworkStream stream
= null;
while (true)
{
msg
= Console.ReadLine();
if (msg != "bye")
{
data
= System.Text.Encoding.ASCII.GetBytes(msg);
stream
= client.GetStream();
stream.Write(data,
0, data.Length);
Console.WriteLine(
"Sent: {0}", msg);
}
else
{
stream
= null;
client.Close();
break;
}
}
}
路过秋天 | 园豆:4787 (老鸟四级) | 2010-04-27 11:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册