大家学习编程的大多都写过这个程序完!
我在写这程序时候遇到这样的问题:
在私聊的时候,不能和比自己后登陆的人私聊,可以和比你先登陆的人聊!
当和后登陆的人聊时候,会报错,说放弃一个线程
我试过,服务器端也不能收到自己发出的信息.
我在代码中加了更新的按钮,当我更新后才可以和所有的人私聊!
更新代码:
客户端:
if (connected)
{
string command = "gengxin"+"|"+clientname; //+"\r\n"; Byte[] outbytes = en.GetBytes(command.ToString());
ns.Write(outbytes, 0, outbytes.Length);
}
服务器端:
if (tokens[0] == "gengxin")
{
for (int n = 0; n < clients.Count; n++)
{ Client cl = (Client)clients[n]; if (cl.Name.CompareTo(tokens[1])==0)
{ SendToClient(cl, "gengxin|" + GetChatterList());
}
} }
我在写这程序时候遇到这样的问题:
在私聊的时候,不能和比自己后登陆的人私聊,可以和比你先登陆的人聊!
当和后登陆的人聊时候,会报错,说放弃一个线程
我试过,服务器端也不能收到自己发出的信息.
我在代码中加了更新的按钮,当我更新后才可以和所有的人私聊!
更新代码:
客户端:
if (connected)
{
string command = "gengxin"+"|"+clientname; //+"\r\n"; Byte[] outbytes = en.GetBytes(command.ToString());
ns.Write(outbytes, 0, outbytes.Length);
}
服务器端:
if (tokens[0] == "gengxin")
{
for (int n = 0; n < clients.Count; n++)
{ Client cl = (Client)clients[n]; if (cl.Name.CompareTo(tokens[1])==0)
{ SendToClient(cl, "gengxin|" + GetChatterList());
}
} }
客户端
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;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace QQclien
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private NetworkStream ns;
private StreamReader sr;
private TcpClient clientsocket;
private string serveraddress="localhost";
private int serverport=5555;
private bool connected=false;
private string clientname;
private Thread receive = null;
private Encoding en = Encoding.GetEncoding("gb2312"); protected override void OnClosed(EventArgs e)
{
try
{
QuitChat();
ns.Close();
clientsocket.Close();
receive.Abort();
Application.Exit();
}
catch (Exception cc)
{
cc.Message.ToString();
}
base.OnClosed(e); } private void 连接ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
MessageBox.Show("请输入名称", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{ EstablishConnection(); if (connected)
{
pictureBox1.Image = Image.FromFile(@"D:\My Documents\Visual Studio 2005\Projects\QQclien\QQclien\imag\1.bmp");
RegisterWithServer();
receive = new Thread(new ThreadStart(ReceiveChat));
receive.Start(); textBox2.Text = "";
连接ToolStripMenuItem.Enabled = false;
断开连接ToolStripMenuItem.Enabled = true;
}
}
}
{ this.label2.Text = "正在连接到服务器"; try
{ clientsocket = new TcpClient(serveraddress, serverport); ns = clientsocket.GetStream(); sr = new StreamReader(ns); connected = true; } catch (Exception)
{ MessageBox.Show("不能连接到服务器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.label2.Text = "已断开连接"; } }
private void RegisterWithServer()
{ clientname = this.textBox2.Text;
try
{
string command = "CONN|" + clientname; //+"\r\n";
Byte[] outbytes = en.GetBytes(command);
ns.Write(outbytes, 0, outbytes.Length);
Byte[] buffer = new Byte[1024]; // 2048???
ns.Read(buffer, 0, buffer.Length);
string chatter = en.GetString(buffer);
string[] tokens = chatter.Split('|');
//string serverresponse = sr.ReadLine();
//serverresponse.Trim();
if (tokens[0] == "LIST")
{
label2.Text = "已连接";
}
if (tokens[1] != "")
{
for (int n = 1; n < tokens.Length - 1; n++)
{ listBox1.Items.Add(tokens[n]); }
}
this.Text = clientname + ":已连接到服务器"; }
catch (Exception ex)
{
MessageBox.Show("注册时发生错误!" + ex.Message, "错误",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
connected = false;
}
}
{
bool keepalive = true;
while (keepalive)
{
try
{
Byte[] buffer = new Byte[1024]; // 2048???
ns.Read(buffer, 0, buffer.Length);
string chatter = en.GetString(buffer);
string[] tokens = chatter.Split(new char[] { '|' });
if (tokens[0] == "gengxin")
{
for (int n = 1; n < tokens.Length-1; n++)
{
methou1(tokens[n]);
}
} if (tokens[0] == "CHAT")
{
methou(tokens[1]);
}
if (tokens[0] == "PRIV")
{
methou("(私聊)");
methou(tokens[1]+"对");
methou(tokens[3]);
methou("说:");
methou(tokens[2] + "\r\n");
}
if (tokens[0] == "JOIN")
{
methou(tokens[1].Trim());
methou(" has joined the Chat\r\n");
methou1(tokens[1]);
}
if (tokens[0] == "GONE")
{
methou(tokens[1].Trim());
methou(" has left the Chat\r\n");
// if(logging)
// {
// logwriter.WriteLine(tokens[1]+" has left the Chat");
// }
removed(tokens[1]);
}
if (tokens[0] == "QUIT")
{
MessageBox.Show("服务器关闭!!!");
ns.Close();
clientsocket.Close();
keepalive = false;
string txt = "服务器端已停止";
labeladdtext(txt);
pictureBox1.Image = Image.FromFile(@"D:\My Documents\Visual Studio 2005\Projects\QQclien\QQclien\imag\2.bmp");
string tx = "客户端聊天程序";
thistxt(tx);
} }
catch (Exception eee) { eee.Message.ToString();
}
}
}