服务端代码
using System;
namespace ConsoleApplication1
{
class ServerStart
{
static void Main(string[] args)
{
Console.Write("服务器已启动");
new Listener().StartListener();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace ConsoleApplication1
{
public class Listener
{
private TcpListener tListnner;
private List<TcpClient> clientList = new List<TcpClient>();
public Listener()
{
tListnner = new TcpListener(1024);
tListnner.Start();
Thread t = new Thread(new ThreadStart(StartListener));
t.Start();
}
public void StartListener()
{
while (true)
{
TcpClient client = tListnner.AcceptTcpClient();
clientList.Add(client);
InInfo(client);//调用读取信息方法;
}
}
public void InInfo(TcpClient client)
{
StreamReader sr = new StreamReader(client.GetStream());
string value = null;
try
{
value = sr.ReadLine();
}
catch (Exception)
{
//clientList.RemoveAt(clientList.Count - 1);
return;
}
while ((value != null))
{
value = value + "\n";
try
{
value = sr.ReadLine();
SendInfo(value);//调用发送信息方法;
}
catch (Exception) { }
}
}
public void SendInfo(string sb)
{
foreach (TcpClient t in clientList)
{
StreamWriter sw = new StreamWriter(t.GetStream());
sw.WriteLine(sb);
sw.Flush();
}
}
}
}
客户端代码using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
static class StartClient
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
[code=C#]
namespace WindowsApplication1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(334, 271);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "发送";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(431, 234);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(3, 272);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(325, 21);
this.textBox2.TabIndex = 2;
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
//
// button2
//
this.button2.Location = new System.Drawing.Point(334, 241);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "连接";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 253);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(239, 12);
this.label1.TabIndex = 4;
this.label1.Text = "文明聊天...本版本不完善只是无聊做起耍的";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 307);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.Name = "Form1";
this.Text = "无聊口水ver1.0";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
}
}
using System;
namespace ConsoleApplication1
{
class ServerStart
{
static void Main(string[] args)
{
Console.Write("服务器已启动");
new Listener().StartListener();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace ConsoleApplication1
{
public class Listener
{
private TcpListener tListnner;
private List<TcpClient> clientList = new List<TcpClient>();
public Listener()
{
tListnner = new TcpListener(1024);
tListnner.Start();
Thread t = new Thread(new ThreadStart(StartListener));
t.Start();
}
public void StartListener()
{
while (true)
{
TcpClient client = tListnner.AcceptTcpClient();
clientList.Add(client);
InInfo(client);//调用读取信息方法;
}
}
public void InInfo(TcpClient client)
{
StreamReader sr = new StreamReader(client.GetStream());
string value = null;
try
{
value = sr.ReadLine();
}
catch (Exception)
{
//clientList.RemoveAt(clientList.Count - 1);
return;
}
while ((value != null))
{
value = value + "\n";
try
{
value = sr.ReadLine();
SendInfo(value);//调用发送信息方法;
}
catch (Exception) { }
}
}
public void SendInfo(string sb)
{
foreach (TcpClient t in clientList)
{
StreamWriter sw = new StreamWriter(t.GetStream());
sw.WriteLine(sb);
sw.Flush();
}
}
}
}
客户端代码using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
static class StartClient
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
[code=C#]
namespace WindowsApplication1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(334, 271);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "发送";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(431, 234);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(3, 272);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(325, 21);
this.textBox2.TabIndex = 2;
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
//
// button2
//
this.button2.Location = new System.Drawing.Point(334, 241);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "连接";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 253);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(239, 12);
this.label1.TabIndex = 4;
this.label1.Text = "文明聊天...本版本不完善只是无聊做起耍的";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 307);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.Name = "Form1";
this.Text = "无聊口水ver1.0";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
}
}
解决方案 »
- 各位大虾帮忙一下我这个菜鸟:
- 标准正态分布函数用C#计算
- C# Fri Feb 24 00:00:00 CST 2012 日期转换
- 关于运行符的小问题
- [winforms]: dataGridView问题
- (50分)关于StringBuilder.ToString()的怪问题
- HELP ME ! HELP ME!!!
- 请教怎么使用AxWebBrowser中的ExecWB方法!!!
- 如何隐藏datagrid中的一列
- 急,急,WebService不能访问,请思归等高手赐教!
- itext!
- 怎么在datagridview中显示图片 如果用string sql = "select ydy.照片 as 照片 from ydy where 专项='划船'";
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.IO;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private TcpClient t = null;
private StreamWriter sw = null;
private StreamReader sr = null;
private string serverAddress;
public string ServerAddress
{
set { serverAddress = value; }
} private int port;
public int Port
{
set { port = value; }
}
public Form1()
{
InitializeComponent();
}
//退出时调用..;
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (sw != null)
{
sw = new StreamWriter(t.GetStream());
sw.WriteLine("myexit...");
}
try
{
CloseAll();
}
catch (Exception) { }
}
private void CloseAll()
{
if (sw != null)
this.sw.Close();
if (sr != null)
this.sr.Close();
if (t != null)
this.t.Close();
}
//连接按扭的事件;
private void button2_Click(object sender, EventArgs e)
{
new LandForm(this).ShowDialog();
} public void LandServer()
{
t = new TcpClient(serverAddress,port);
Thread thread = new Thread(new ThreadStart(inInfo));
thread.Start();
} //发送的事件按扭;
private void button1_Click(object sender, EventArgs e)
{
if (t == null)
{
MessageBox.Show("请先连接!");
return;
}
string sb = textBox2.Text.Trim();
if ("".Equals(sb))
{
return;
}
if (sw == null)
sw = new StreamWriter(t.GetStream());
sw.WriteLine(sb);
sw.Flush();
textBox2.Text = "";
}
private void inInfo()
{
try
{
while (true)
{
if (sr == null)
sr = new StreamReader(t.GetStream());
string value = sr.ReadLine();
Console.Write(value);
while (value != null)
{
textBox1.AppendText(value + "\n\n");
value = sr.ReadLine();
}
}
}
catch (Exception e) { }
} private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
button1_Click(null, null);
textBox2.Focus();
}
}
}
}
namespace WindowsApplication1
{
partial class LandForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 82);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(65, 23);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 1;
this.label1.Text = "服务地址";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 58);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 2;
this.label2.Text = "端口";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(83, 17);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 3;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(83, 49);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 4;
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
//
// LandForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(222, 117);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "LandForm";
this.Text = "登陆";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class LandForm : Form
{
private Form1 f = null;
public LandForm(Form1 f)
{
this.f = f;
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string address = textBox1.Text;
string port = textBox2.Text;
f.ServerAddress = address;
f.Port = int.Parse(port);
f.LandServer();
this.Close();
} private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
button1_Click(null, null);
}
} }
}现在问题是第一次发送的时候没有显示。。要第二次发送才有。。怎么会丢失了第一次。。找了半天没找到。。