|
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;
namespace AClient
{
public partial class FClient : Form
{
//计算机是如何判断 isExit
private bool isExit = false;
private TcpClient client;
private BinaryReader br;
private BinaryWriter bw;
public FClient()
{
InitializeComponent();
Random r = new Random(~unchecked((int)DateTime.Now.Ticks));
txtUserName.Text = "" + r.Next(0, 600);
}
private delegate void AddStatusDelegate(string message);
private void AddStatus(string message)
{
if (txtStatus.InvokeRequired)
{
AddStatusDelegate d = new AddStatusDelegate(AddStatus);
//下面这句不懂
txtStatus.Invoke(d, new object[] { message });
}
else
{
txtStatus.AppendText(message);
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
btnConnect.Enabled = false;
AddStatus("开始连接");
ConnectToServer();
}
private void ConnectToServer()
{
client=new TcpClient();
IAsyncResult result = client.BeginConnect(Dns.GetHostName(), 51888, null, null);
while (result.IsCompleted == false)
{
Thread.Sleep(100);
AddStatus("稍等");
}
client.EndConnect(result);
AddStatus("连接成功");
NetworkStream netWorkStream = client.GetStream();
br = new BinaryReader(netWorkStream);
bw = new BinaryWriter(netWorkStream);
AsyncSendMessage(txtUserName.Text+"已登陆");
Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
threadReceive.IsBackground = true;
threadReceive.Start();
}
delegate void SendMessageDelegate(string message);
private struct SendMessageStates
{
public SendMessageDelegate d;
public IAsyncResult result;
}
private void AsyncSendMessage(string message)
{
SendMessageDelegate d = new SendMessageDelegate(SendMessage);
IAsyncResult result = d.BeginInvoke(message, null, null);
while (result.IsCompleted == false)
{
if (isExit==true)
{
return;
}
Thread.Sleep(500);
}
SendMessageStates states = new SendMessageStates();
states.d = d;
states.result = result;
Thread t = new Thread(FinishAsyncSendMessage);
t.IsBackground = true;
t.Start(states);
}
private void SendMessage(string message)
{
try
{
bw.Write(message);
bw.Flush();
}
catch
{
AddStatus("发送失败!");
}
}
private void FinishAsyncSendMessage(object obj)
{
SendMessageStates states = (SendMessageStates)obj;
states.d.EndInvoke(states.result);
}
delegate void ReceiveMessageDelegate(out string receiveString);
private void ReceiveData()
{
string receiveString = null;
while (isExit == false)
{
ReceiveMessageDelegate d = new ReceiveMessageDelegate(ReceiveMessage);
IAsyncResult result = d.BeginInvoke(out receiveString, null, null);
while (result.IsCompleted == false)
{
if (isExit)
{
break;
}
Thread.Sleep(500);
}
d.EndInvoke(out receiveString, result);
if (receiveString == null)
{
if (isExit == false)
{
MessageBox.Show("与服务器失去联系");
}
break;
}
string[] splitString = receiveString.Split(',');
string command = splitString[0].ToString();
switch(command)
{
case"登陆":
AddOnline(splitString[1]);
break;
case"退出":
AddOnline(splitString[1]);
break;
case"talk":
AddTalkMessage(splitString[1]+":\r\n");
AddTalkMessage(receiveString.Substring(splitString[0].Length+splitString[1].Length+2));
break;
}
}
}
private delegate void AddTalkMessageDelegate(string message);
private void AddTalkMessage(string message)
{
if (txtTalkInfo.InvokeRequired)
{
AddTalkMessageDelegate d = new AddTalkMessageDelegate(AddTalkMessage);
txtTalkInfo.Invoke(d, new object[] { message });
}
else
{
txtTalkInfo.AppendText(message);
txtTalkInfo.ScrollToCaret();
}
}
private void ReceiveMessage(out string receiveMessage)
{
receiveMessage = null;
try
{
receiveMessage = br.ReadString();
}
catch (Exception ex)
{
AddStatus(ex.Message);
}
}
private delegate void AddOnlineDelegate(string message);
private void AddOnline(string message)
{
if (lstOnline.InvokeRequired)//判断控件和主线是否在程同一个线程
{
AddOnlineDelegate d = new AddOnlineDelegate(AddOnline);
lstOnline.Invoke(d, new object[] { message });
}
else
{
lstOnline.Items.Add(message);
lstOnline.SelectedIndex = lstOnline.Items.Count - 1;
lstOnline.ClearSelected();
}
}
private void FClient_FormClosing(object sender, FormClosingEventArgs e)
{
if (client != null)
{
isExit = true;
if (client.Client.Connected)
{
AsyncSendMessage("退出," + txtUserName.Text.Trim());
br.Close();
bw.Close();
}
client.Close();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (lstOnline.SelectedIndex != -1)
{
AsyncSendMessage("talk," + lstOnline.SelectedItem + "," + txtSend.Text + "\r\n");
txtSend.Clear();
}
else
{
MessageBox.Show("请先在【当前在线】中选择一个对话者");
}
}
}
}[/code]
|
-
-
Projects.rar
84.72 KB, 下载次数: 255, 下载积分: 金钱 -1
为什么lstOnline显示不了用户名
|