• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

这是我作的,本来想实现点对点的聊天程序, 只实现了简单的消息发送,贴出来,请大家指正!!

dragon2002 2002-12-20 09:35:19
服务器端:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net ;
using System.Net.Sockets ;
using System.Threading ;
namespace Tcp编程
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private TcpListener listener;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button3;
private int port;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Label label4;
private Socket fromrequest;
//private TcpListener listener;
private bool control ;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label2 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 0;
this.label1.Text = "监听端口";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 280);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(192, 280);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "停止监听";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 327);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(392, 22);
this.statusBar1.TabIndex = 4;
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 5;
this.label2.Text = "发送消息";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(104, 160);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(232, 96);
this.richTextBox1.TabIndex = 6;
this.richTextBox1.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(112, 280);
this.button3.Name = "button3";
this.button3.TabIndex = 7;
this.button3.Text = "发送消息";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(104, 72);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(232, 72);
this.richTextBox2.TabIndex = 17;
this.richTextBox2.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(24, 80);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 16;
this.label4.Text = "接受信息";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox2,
this.label4,
this.button3,
this.richTextBox1,
this.label2,
this.statusBar1,
this.button2,
this.button1,
this.textBox1,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
port=Int32.Parse(textBox1.Text );
if(textBox1.Text==null)
throw(new ArgumentNullException());
}
catch
{
MessageBox.Show ("你的输入格式不对,请从新输入!");

}
try
{
listener= new TcpListener(port);
listener.Start();
statusBar1.Text="开始监听-----";
// sock=listener.AcceptSocket();
// if(sock.Connected )
// {
// statusBar1.Text="与客户建立连接!!";
// }
Thread thread1=new Thread(new ThreadStart(targetstart));
thread1.Start();
}
catch(Exception ee)
{
MessageBox.Show (ee.Message );

}

}
private void targetstart()
{
fromrequest=listener.AcceptSocket();

if(fromrequest.Connected )
{
statusBar1.Text="与客户建立连接!!";

while(!control)
{

byte[] by=new byte[640];
int i=fromrequest.Receive(by,by.Length,System.Net.Sockets.SocketFlags.None);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(by);
richTextBox2.AppendText(ss+"\r\n");
}
}
}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
control=true;
listener.Stop();
statusBar1.Text ="停止监台听!---";
}
catch(Exception ee)
{
MessageBox.Show ("监听没有开始,关闭无效"+ee.Message );

}
}
private void button3_Click(object sender, System.EventArgs e)
{

string str=richTextBox1.Text +"\r\n";
byte[] sendclient=System.Text.Encoding.BigEndianUnicode.GetBytes(str);
fromrequest.Send(sendclient,sendclient.Length,SocketFlags.None);//(sendclient,sendclient.Length ,0);


}
}
}

...全文
26 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dragon2002 2002-12-20
谁有关于网络编程的资料或站点,也请大家推建几本好书。
回复
tophy 2002-12-20
可以的,昨天我试过了,,不过好像需要用户名密码的时候就不行了:(
回复
dragon2002 2002-12-20
在两台机子上用时无法连接到对方,
所安全策略出错。
回复
tophy 2002-12-20
to dragon
有什么问题呀?我想知道呢!改进改进~~~~

我也是刚刚学C#才4天,很多都不知道,大家以后多帮帮我哦!
回复
jd2bs 2002-12-20
to:tophy(震一飞)兄,怎么没源码啊?
可能的话,希望学习一下;jd2ds@163.com
回复
dragon2002 2002-12-20
to tophy(震一飞) ;
使用有问题,界面作得不错

我刚学网络编程,大家多多指点。
回复
ToBeCharcoal 2002-12-20
好样的!
回复
kinglht 2002-12-20
继续努力,继续完善!!
回复
whblxl 2002-12-20
good
回复
tophy 2002-12-20
呵,我昨天也刚刚写了一个,是点对点的,不过只能在局域网内使用:(
这是地址:(自己对自己也可以:P)

www.eastagri.com/temp/ptp.zip
回复
dragon2002 2002-12-20
继续客户端:
private void button1_Click(object sender, System.EventArgs e)
{
int port=0;
client=new TcpClient();
IPAddress myIP=IPAddress.Parse("127.0.0.1");
try
{
myIP=IPAddress.Parse(textBox2.Text);

}
catch
{
MessageBox.Show ("请输入正确的ip格式!");

}
try
{
port=Int32.Parse(textBox2.Text);

}
catch
{
MessageBox.Show ("请输入整数!");

}
try
{
if(textBox3.Text!="")
{
client.Connect(textBox3.Text,port );
statusBar1.Text ="与服务器建立连接关系!";
Thread thread1=new Thread(new ThreadStart(target));
thread1.Start();
}
else if(textBox1.Text!="")
{
client.Connect(textBox1.Text.Trim() ,port );
statusBar1.Text ="与服务器建立连接关系!";
Thread thread1=new Thread(new ThreadStart(target));
thread1.Start();
}
}
catch(Exception ee)
{
MessageBox.Show (ee.Message );

}
}
private void target()
{
while(!control)
{
stream=client.GetStream();
byte[] fromclient=new byte[640];
int i=stream.Read(fromclient,0,640);
string str=System.Text.Encoding.BigEndianUnicode.GetString(fromclient);
richTextBox1.AppendText(str+"\r\n");
}

}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
client.Close();
statusBar1.Text ="与服务器建立断开连接关系!";
}
catch(Exception ee)
{
MessageBox.Show ("连接没有开始,关闭无效"+ee.Message );

}
}


private void button3_Click(object sender, System.EventArgs e)
{
NetworkStream fromclient=client.GetStream();
string flush=richTextBox2.Text +"\r\n";
byte[] by=System.Text.Encoding.BigEndianUnicode.GetBytes(flush.ToCharArray());
fromclient.Write(by,0,by.Length );
fromclient.Flush();
}
}
}
回复
dragon2002 2002-12-20
客户端:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net ;
using System.Net.Sockets ;
using System.Threading ;
namespace clienttcp
{
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox richTextBox1;
private TcpClient client;
private bool control= false;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button3;
private NetworkStream stream;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 351);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(368, 22);
this.statusBar1.TabIndex = 9;
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 8;
this.button2.Text = "断开连接";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "连接";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 5;
this.label1.Text = "服务器名称";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 10;
this.label2.Text = "端口";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(104, 104);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 11;
this.textBox2.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 12;
this.label3.Text = "服务器IP";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(104, 56);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 13;
this.textBox3.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 23);
this.label4.TabIndex = 14;
this.label4.Text = "接受信息";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(104, 136);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(232, 72);
this.richTextBox1.TabIndex = 15;
this.richTextBox1.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 232);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(80, 23);
this.label5.TabIndex = 16;
this.label5.Text = "发送信息";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(104, 224);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(232, 80);
this.richTextBox2.TabIndex = 17;
this.richTextBox2.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(128, 320);
this.button3.Name = "button3";
this.button3.TabIndex = 18;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.richTextBox2,
this.label5,
this.richTextBox1,
this.label4,
this.textBox3,
this.label3,
this.textBox2,
this.label2,
this.statusBar1,
this.button2,
this.button1,
this.textBox1,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)
{
//new TcpClient()
}


回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2002-12-20 09:35
社区公告

让您成为最强悍的C#开发者