| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 600 人关注过本帖
标题:24分游戏(分服务器端和客户端两部分)
只看楼主 加入收藏
xxww1991
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-10-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
24分游戏(分服务器端和客户端两部分)
这是我的一个课程设计    现在调试程序总报错
哪位大大帮帮忙   让它能运行起来


客户端程序:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;
using System.Collections ;
namespace The24Client
{
    public partial class MainForm : Form
    {
        private System.DateTime StartTime;
        private TcpClient client;
        private BinaryReader br;
        private BinaryWriter bw;
        private bool isExit = false;
        IPAddress localAddress;
 public MainForm()
{
  InitializeComponent();
  buttonExit.Enabled = false;
  buttonGetNum.Enabled = false;
  buttonAnswer.Enabled = false;
buttonEnter.Enabled = false;
  buttonLogin.Enabled = false;
  buttonGetTop10.Enabled = false;
  IPAddress[] addrIP = Dns.GetHostAddresses(Dns.GetHostName());
  localAddress = addrIP[0];
  textBoxIP.Text = localAddress.ToString();
  textBoxDuank.Text = "51000"; }
 private void buttonConnect_Click(object sender, EventArgs e)
 {
buttonLogin.Enabled = true;
  buttonConnect.Enabled = false;
  buttonExit.Enabled = true;
  try {
  client = new TcpClient(Dns.GetHostName(), 51000);
  listBoxInfo.Items.Add("连接成功");
  }
  catch
  {
  listBoxInfo.Items.Add("连接失败");
  buttonConnect.Enabled = true;
return;
  }
NetworkStream networkStream = client.GetStream();
 br = new BinaryReader(networkStream);
bw = new BinaryWriter(networkStream);
 SendMessage("CONNECT" + textBoxIP.Text);
 Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
 threadReceive.IsBackground = true;
 threadReceive.Start();
 }
 private void ReceiveData()
 {
 string receiveString = null;
 while (isExit == false)
 {try
  {
  receiveString = br.ReadString();
 }
 catch
 {
 if (isExit == false)
 {
 MessageBox.Show("与服务器失去联系。");
 }
 break;
}
 string[] splitString = receiveString.Split(',');
 string command = splitString[0].ToLower();
 switch (command)
 {
 case "CONNECT":
 break;
 case "LOGIN":
 listBoxInfo.Items.Add("用户名上传成功");
 break;
 case "EXIT":
 listBoxInfo.Items.Add("连接失败或与服务器断开连接");
 break;
 case "COUNT":  
  listBoxInfo.Items.Add(string.Format("[用户{0}]检查结果并上传耗时", splitString[1]));
  break;
  default:
  listBoxInfo.Items.Add(receiveString);
  break;
  }
  }
  Application.Exit();
  }
  private void SendMessage(string message)
  {
  try
  {
  bw.Write(message);
  bw.Flush();
  }
  catch
  {
  listBoxInfo.Items.Add("发送失败!");
  }
  }
  private void buttonLogin_Click(object sender, EventArgs e)
  {
   buttonGetTop10.Enabled = true;
   buttonGetNum.Enabled = true;
   buttonLogin.Enabled = false;
NetworkStream networkStream = client.GetStream();
   br = new BinaryReader(networkStream);
   bw = new BinaryWriter(networkStream);
   SendMessage("LOGIN," + textBoxUserName.Text);
   Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
   threadReceive.IsBackground = true;
   threadReceive.Start();
   }
private void buttonGetNum_Click(object sender, EventArgs e)
   {
    buttonEnter.Enabled = true;
    buttonAnswer.Enabled = true;
    Random random = new Random(DateTime.Now.Millisecond);
    int numb1 = random.Next(1,9);
    int numb2 = random.Next(1,9);
    int numb3 = random.Next(1,9);
    int numb4 = random.Next(1,9);
    label5.Text = numb1.ToString();
    label6.Text = numb2.ToString();
    label7.Text = numb3.ToString();
    label8.Text = numb4.ToString();
    StartTime = DateTime.Now;
    timer1.Enabled = true;
        }
  private void buttonAnswer_Click(object sender, EventArgs e)
  {
   this.listBox1.Items.Clear();
    runter();
    timer1.Enabled = false;
    }
   private bool isSame(int a, int b, int c, int d)
   {
    if (a == b || a == c || a == d || b == c || b == d || c == d)
  {
   return false;
  }
  return true;
  }
  public int counter(int i, int sum, int num)
  {
   if (i == 0)
   {
   return sum + num;
   }
  else if (i == 1)
   {
    return sum - num;
   }
   else if (i == 2)
   {
    return sum * num;
    }
    else
    {
    float temp = (float)sum / num;
    if (num != 0)
    {
    if (temp > (sum / num))
    {
    return -1000;
    }
    else
   {
   return sum / num;
   }
   }
   else
   {
    return -1000;
   }
  }
  }
  private string operators(int i)
  {
   if (i == 0)
   {
   return "+";
   }
   else if (i == 1)
   {
    return "-";
    }
    else if (i == 2)
    {
   return "×";
    }
   else
  {
  return "÷";
  }
  }
  private void runter()
  {
  bool isResult = false;
  int[] numb = { 0, 0, 0, 0 };
  numb[0] = Int16.Parse(label5.Text);
  numb[1] = Int16.Parse(label6.Text);
  numb[2] = Int16.Parse(label7.Text);
  numb[3] = Int16.Parse(label8.Text);
 int count = 0;
  for (int a = 0; a < 4; a++)
  {
   for (int b = 0; b < 4; b++)
  {
  for (int c = 0; c < 4; c++)
  {
  for (int d = 0; d < 4; d++)
  {
  if (this.isSame(a, b, c, d))
  {  //+,-,*,/,循环********
  for (int a1 = 0; a1 < 4; a1++)
  {
  for (int b1 = 0; b1 < 4; b1++)
  {
  for (int c1 = 0; c1 < 4; c1++)
  {
  count = this.counter(c1, this.counter(b1, this.counter(a1, numb[a], numb[b]), numb[c]), numb[d]);
   if (count == 24)
   {
    listBox1.Items.Add("结果:((" + numb[a] + operators(a1) + numb[b] + ")" + operators(b1) + numb[c] + ")" + operators(c1) + numb[d] + "=" + count);
    isResult = true;
    }
    count = this.counter(c1, this.counter(a1, numb[a], this.counter(b1, numb[b], numb[c])), numb[d]);
    if (count == 24)
   {
    listBox1.Items.Add("结果:(" + numb[a] + operators(a1) + "(" + numb[b] + operators(b1) + numb[c] + "))" + operators(c1) + numb[d] + "=" + count);
    isResult = true;
    }
    count = this.counter(b1, this.counter(a1, numb[a], numb[b]), this.counter(c1, numb[c], numb[d]));
     if (count == 24)
   {
   listBox1.Items.Add("结果:(" + numb[a] + operators(a1) + numb[b] + ")" + operators(b1) + "(" + numb[c] + operators(c1) + numb[d] + ")" + "=" + count);
  isResult = true;
    }
    count = this.counter(a1, numb[a], this.counter(b1, numb[b], this.counter(c1, numb[c], numb[d])));
    if (count == 24)
    {
    listBox1.Items.Add("结果:" + numb[a] + operators(a1) + "(" + numb[b] + operators(b1) + "(" + numb[c] + operators(c1) + numb[d] + "))" + "=" + count);
    isResult = true;
     }
count = this.counter(a1, numb[a], this.counter(c1, this.counter(b1, numb[b], numb[c]), numb[d]));
    if (count == 24)
    {
    listBox1.Items.Add("结果:" + numb[a] + operators(a1) + "((" + numb[b] + operators(b1) + numb[c] + ")" + operators(c1) + numb[d] + ")" + "=" + count);
isResult = true;
}}}}
if (!isResult)
   {
    MessageBox.Show("此副牌无解");
    }
    }
  private void timer1_Tick(object sender, EventArgs e)
  {
  TimeSpan ts = DateTime.Now - StartTime;
   label12.Text =ts.Seconds.ToString();
  }
}







服务器端程序:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using System.Threading;
namespace The24Server
{
 public partial class MainForm : Form
 {
 private List<User> userList = new List<User>();
 IPAddress localAddress;
 private const int port = 51000;
 private TcpListener myListener;
 bool isNormalExit = false;
 public MainForm()
 {
  InitializeComponent();
  listBoxStatus.HorizontalScrollbar = true;
  IPAddress[] addrIP = Dns.GetHostAddresses(Dns.GetHostName());
  localAddress = addrIP[0];
  buttonStop.Enabled = false;
  }
  private void buttonStart_Click(object sender, EventArgs e)
 {
  myListener = new TcpListener(localAddress, port);
  myListener.Start();
  listBoxStatus.Items.Add(string.Format("开始在{0}:{1}监听客户连接", localAddress, port));
  Thread myThread = new Thread(ListenClientConnect);
  myThread.Start();
  buttonStart.Enabled = false;
  buttonStop.Enabled = true;
  }
  private void ListenClientConnect()
  {
  TcpClient newClient = null;
   while (true)
  {
   try
   {
     newClient = myListener.AcceptTcpClient();
    }
    catch
    {
     break;
    }
    User user = new User(newClient);
   Thread threadReceive = new Thread(ReceiveData);
   threadReceive.Start(user);
   userList.Add(user);
   listBoxStatus.Items.Add(string.Format("[{0}]进入", newClient.Client.RemoteEndPoint));
   listBoxStatus.Items.Add(string.Format("当前连接用户数:{0}", userList.Count));
   }
   }
   private void ReceiveData(object userState)
   {
   User user = (User)userState;
   TcpClient client = user.client;
   while (isNormalExit == false)
   {
   string receiveString = null;
   try
  {
   receiveString = user.br.ReadString();
   }
   catch
  {
  if (isNormalExit == false)
  {
   listBoxStatus.Items.Add(string.Format("与[{0}]失去联系,已终止接收该用户信息", client.Client.RemoteEndPoint));
   userList.Remove(user);
   }
   break;
   }
   string[] splitString = receiveString.Split(',');
    switch (splitString[0])
    {
    case "CONNECT":
    listBoxStatus.Items.Add(string.Format("来自[{0}]", user.client.Client.RemoteEndPoint));
    SendToClient(user, "CONNECT," + userList[0].userName);
    break;
    case "LOGIN":
    user.userName = splitString[1];
    listBoxStatus.Items.Add(string.Format("该用户用户名为[{0}]",splitString[1]));
    SendToClient(user, "lOGIN," + userList[0].userName);
    break;
    case "EXIT":
    return;
    case "COUNT":
    break;
    }
}
    }
private void SendToClient(User user, string message)
   {
   try
   {
   user.bw.Write(message);
   user.bw.Flush();
   }
   Catch(Exception e)
  {         }
private void SendToAllClient(User user, string message)
        {
  string command = message.Split(',')[0].ToLower();
  if (command == "lOGIN")
 {
  for (int i = 0; i < userList.Count; i++)
  {
  SendToClient(userList[i], message);
  if (userList[i].userName != user.userName)
  {
   SendToClient(user, "lOGIN," + userList[i].userName);
   }
  }
  }
 else if (command == "EXIT")
 {
  for (int i = 0; i < userList.Count; i++)
  {
   if (userList[i].userName != user.userName)
  {
   SendToClient(userList[i], message);
   }
   }
   }
  }
private void RemoveUser(User user)
   {
    throw new NotImplementedException();
    }
    private void buttonStop_Click(object sender, EventArgs e)
        {
     myListener.Stop();
     buttonStart.Enabled = true;
     buttonStop.Enabled = false;
     }
     private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
     {
     if (myListener != null)
     {
     buttonStop.PerformClick();
     }
     }   
     }}  
搜索更多相关主题的帖子: public 客户端 
2012-10-11 18:07
xxww1991
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-10-11
收藏
得分:0 
没有人 帮忙看看么   
2012-10-11 20:49
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:20 
帮顶

梅尚程荀
马谭杨奚







                                                       
2012-10-12 12:46
快速回复:24分游戏(分服务器端和客户端两部分)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018517 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved