socket具体实例(socket的实例)

本文目录
socket的实例
public class XmlSocket{//异步socket侦听从客户端传来的数据public static string data=null;//Threadsignal.线程用一个指示是否将初始状态设置为终止的布尔值初始化ManualResetEvent类的新实例。public static ManualResetEvent allDone=new ManualResetEvent(false);static void Main(string byteData = Encoding.UTF8.GetBytes(data);handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);}private static void SendCallback(IAsyncResult ar){Socket handler = (Socket)ar.AsyncState;//向远端发送数据int bytesSent = handler.EndSend(ar);StateObject state = new StateObject();state.workSocket = handler;handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback),state);handler.Shutdown(SocketShutdown.Both);handler.Close();}public static void StopListening(){allDone.Close();log.close();}
400分求vb.net,求一个socket的编程实例,详细见问题补充:
至少需要
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
还要了解异步与委托
简单点的话
sub tcplisten() ’监听过程
Const LPort As Integer = 6850 ’本地监听端口
Dim IPadd As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList(1) ’本地IP
’addresslist里面包括IPV6和IPV4
Dim IPEP As New IPEndPoint(IPadd, LPort) ’结点
TCPL = New TcpListener(IPEP) ’建立监听实例
TCPL.Start() ’开始监听
If TCPL.Pending = True Then ’如果有连接接入
Dim TCPLX As New Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp) ’建立一个新的SOCKET
TCPLX=TCPL.Accept() ’将第一个接入挂起的连接传给新建SOCKET,因为监听的SOCKET还要继续监听
dim Ripep as new ipendpoint=TCPLX.RemoteEndPoint ’此时获得远程的ip与端口号
listbox.add(ripep.ip)
dim buff as byte()
buff=TCPLX.Receive() ’listen》accept》recieve这时候可以接受数据了
dim s as string=Encoding.Default.GetString(buff) ’转换成文本
msg "s"
TCPLX.send(buff) ’这个是发送
end if
End Sub
长时间没写,可能有点小错误,你自己调试调试,大概步骤就是listen》accept》recieve,最好看看msdn,那个学起来才系统
C#谁能给我个,C/S 通过Socket与线程通信的例子,,
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);
}
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.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.Text="Host not found!";
return;
}statusBar1.Panels.Text="Has connected to host--》"+ipendpoint.ToString();
Thread thread=new Thread(new ThreadStart(getmsg));
thread.Start();
}
private void getmsg(){
while(true){
byte;
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);
}
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.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.Text="Host not found or Listen has began!";
return;
}
statusBar1.Panels.Text="Listen has bengin!";
while(true)
{remotesocket=localsocket.Accept();
Thread thread=new Thread(new ThreadStart(getmsg));
thread.Start();
}
}
}
private void getmsg()
{
while(true)
{
byte;
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);
}
}
}

更多文章:
blast premier春季赛(csgo战队vitality有谁)
2026年9月7日 21:50
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00





