C#谁能给我个,C/S 通过Socket与线程通信的例子,,

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#谁能给我个,C/S 通过Socket与线程通信的例子,,相关的知识,希望对你有一定的参考价值。

S
Thread serThd=null;
Socket soc=null;
ServerStart()
soc=new ...
while(true)
////...



C
Thread thd=null;
Socket ...

//要求,只要C可以通过一个方法,或事件,完成与S的一次交互(即读写双边)即可.但是前提是用,双端都用Thread完成的,
我知道难,,,回答好,加30分,绝不食言

VS2003下的:
//======================客户端=========================
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 S_c

public class Form1 : System.Windows.Forms.Form

private IPEndPoint ipendpoint;
private Socket localsocket;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.ComponentModel.Container components = null;

public Form1()

InitializeComponent();


protected override void Dispose( bool disposing )

if( disposing )
if (components != null)
components.Dispose();


base.Dispose( disposing );


private void InitializeComponent()

this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(208, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 56);
this.button1.TabIndex = 0;
this.button1.Text = "Begin Connect";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 1;
this.label1.Text = "HostIP:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox1.Location = new System.Drawing.Point(64, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "127.0.0.1";
this.label2.Location = new System.Drawing.Point(8, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Port:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox2.Location = new System.Drawing.Point(64, 64);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(24, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 112);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Connect";
this.groupBox2.Controls.Add(this.richTextBox1);
this.groupBox2.Location = new System.Drawing.Point(24, 128);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 176);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Receive Messages";

this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(3, 17);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(290, 156);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.groupBox3.Controls.Add(this.richTextBox2);
this.groupBox3.Location = new System.Drawing.Point(24, 312);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(296, 176);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Send Messages";
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Location = new System.Drawing.Point(3, 17);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(290, 156);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
this.button2.Location = new System.Drawing.Point(69, 496);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 6;
this.button2.Text = "SEND";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Location = new System.Drawing.Point(208, 496);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 6;
this.button3.Text = "REWRITE";
this.statusBar1.Location = new System.Drawing.Point(0, 535);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] this.statusBarPanel1,this.statusBarPanel2);
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(344, 22);
this.statusBar1.TabIndex = 7;
this.statusBarPanel1.Text = "Current Status:";
this.statusBarPanel2.Width = 350;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 557);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.button3);
this.Name = "Form1";
this.Text = "S_c";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.ResumeLayout(false);

[STAThread]
static void Main()

Application.Run(new Form1());


private void button1_Click(object sender, System.EventArgs e)

try
ipendpoint=new IPEndPoint(IPAddress.Parse(textBox1.Text.Trim()),int.Parse(textBox2.Text.Trim()));
catch
statusBar1.Panels[1].Text="Please input a valid value!";
return;


Thread thread=new Thread(new ThreadStart(accp));
thread.Start();


private void accp()
try
localsocket=new Socket(localsocket.Connect(ipendpoint);
catch
statusBar1.Panels[1].Text="Host not found!";
return;
statusBar1.Panels[1].Text="Has connected to host-->"+ipendpoint.ToString();

Thread thread=new Thread(new ThreadStart(getmsg));
thread.Start();


private void getmsg()
while(true)
byte[] buffer=new byte[128];
localsocket.Receive(buffer);
richTextBox1.AppendText("\n"+System.Text.Encoding.BigEndianUnicode.GetString(buffer));



private void button2_Click(object sender, System.EventArgs e)
buffer=System.Text.Encoding.BigEndianUnicode.GetBytes(richTextBox2.Text.Trim());
localsocket.Send(buffer);



//======================服务端=========================
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 S_s
public class Form1 : System.Windows.Forms.Form

private IPEndPoint ipendpoint;
private Socket localsocket;
private Socket remotesocket;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.ComponentModel.Container components = null;

public Form1()

InitializeComponent();


protected override void Dispose( bool disposing )

if( disposing )
if (components != null)
components.Dispose();


base.Dispose( disposing );


private void InitializeComponent()

this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(208, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 56);
this.button1.TabIndex = 0;
this.button1.Text = "Begin Listen";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 1;
this.label1.Text = "HostIP:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox1.Location = new System.Drawing.Point(64, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "127.0.0.1";
this.label2.Location = new System.Drawing.Point(8, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Port:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox2.Location = new System.Drawing.Point(64, 64);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(24, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 112);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Listen";
this.groupBox2.Controls.Add(this.richTextBox1);
this.groupBox2.Location = new System.Drawing.Point(24, 128);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 176);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Receive Messages";
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(3, 17);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(290, 156);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.groupBox3.Controls.Add(this.richTextBox2);
this.groupBox3.Location = new System.Drawing.Point(24, 312);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(296, 176);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Send Messages";
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Location = new System.Drawing.Point(3, 17);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(290, 156);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
this.button2.Location = new System.Drawing.Point(69, 496);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 6;
this.button2.Text = "SEND";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Location = new System.Drawing.Point(208, 496);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 6;
this.button3.Text = "REWRITE";
this.statusBar1.Location = new System.Drawing.Point(0, 535);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] this.statusBarPanel1,this.statusBarPanel2);
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(344, 22);
this.statusBar1.TabIndex = 7;
this.statusBarPanel1.Text = "Current Status:";
this.statusBarPanel2.Width = 350;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 557);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.button3);
this.Name = "Form1";
this.Text = "S_s";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.ResumeLayout(false);



[STAThread]
static void Main()

Application.Run(new Form1());

private void button1_Click(object sender, System.EventArgs e)

try

ipendpoint=new IPEndPoint(IPAddress.Parse(textBox1.Text.Trim()),int.Parse(textBox2.Text.Trim()));

catch

statusBar1.Panels[1].Text="Please input a valid value!";
return;


Thread thread=new Thread(new ThreadStart(accp));
thread.Start();


private void accp()

while(true)

try

localsocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
localsocket.Bind(ipendpoint);
localsocket.Listen(128);

catch
statusBar1.Panels[1].Text="Host not found or Listen has began!";
return;

statusBar1.Panels[1].Text="Listen has bengin!";

while(true)
remotesocket=localsocket.Accept();
Thread thread=new Thread(new ThreadStart(getmsg));
thread.Start();




private void getmsg()

while(true)

byte[] buffer=new byte[128];
remotesocket.Receive(buffer);
richTextBox1.AppendText("\n"+System.Text.Encoding.BigEndianUnicode.GetString(buffer));



private void button2_Click(object sender, System.EventArgs e)

byte[] buffer=System.Text.Encoding.BigEndianUnicode.GetBytes(DateTime.Now+"\n"+richTextBox2.Text.Trim());
remotesocket.Send(buffer);


参考技术A Socket异步通讯:
我做的一个网络联机小游戏上NetWork部分代码!可能能帮你解决问题!

Server:
http://code.google.com/p/shortboom/source/browse/trunk/NetWork/GameServer.cs

Client:
http://code.google.com/p/shortboom/source/browse/trunk/NetWork/GameClient.cs本回答被提问者采纳
参考技术B 可以使用 FlyTCPFramework
如果需要请我联系
也可以到我的网站来交流http://www.bg1jt.cn

我安装了天正建筑cad8.0(试用版),安装ID号是DPFD212S4RRL27M,谁能给我个注册码啊?谢谢了!

参考技术A 25B2062A29D5A15387E80AF44D04CB 试试看,不一定用得了 参考技术B 2597ADC42B4D50D995D130DBEA677A

以上是关于C#谁能给我个,C/S 通过Socket与线程通信的例子,,的主要内容,如果未能解决你的问题,请参考以下文章

谁能给我个PHP 5.5版本的下载地址啊。

谁能给我个abdroid运行的ubuntu系统镜像的img文件,115网盘或者百度云盘的。

c# c/s结构Socket上传文件的代码

我安装了天正建筑cad8.0(试用版),安装ID号是DPFD212S4RRL27M,谁能给我个注册码啊?谢谢了!

c# socket 客户端 循环发包 服务器端粘粘包严重!求高手给我个解决粘包的例子!

谁能给我一个MicrosoftOfficeProfessionalPlus2007的密钥?