展开全部
socket-client
----------------------------------------------
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;
namespace socket_client
{
//客户端
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
Socket s;//聊天用
Thread th;
//连接
private void button1_Click(object sender, EventArgs e)
{
//步骤1 配置远程服务器信息
IPEndPoint removeServer = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
//步骤2 创建套接字
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//步骤3 套接字连接远程服务器
s.Connect(removeServer);
//步骤4 提示连接状态
if (s.Connected)
{
label4.Text = "连接服务器成功!";
//步骤5 循环接收服务器发来的消息
th = new Thread(new ThreadStart(BB));
th.IsBackground = true;
th.Start();
}
}
void BB()
{
while (true)
{
byte[] bb = new byte[1024];
int i= s.Receive(bb); //接收数据,返回每次接收的字节总数
string removeMsg = Encoding.Unicode.GetString(bb,0,i);
if (removeMsg == "CMD--EXIT")//收到的是退出通知
{
label4.Text = "无连接";
DialogResult re=MessageBox.Show("服务器已经关闭.\n\"确定\"后退出程序,\n\"取消\"继续停留!", "消息提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
MessageBox.Show(re.ToString());
if (re == DialogResult.OK)
{
sendExit();//告诉服务器我退出了
Application.Exit();
}
return;
}
richTextBox1.AppendText(removeMsg+"\n") ;
richTextBox1.ScrollToCaret();
}
}
//发送消息
private void button3_Click(object sender, EventArgs e)
{
string msg = "客户端:" + richTextBox2.Text;
byte[] bb = Encoding.Unicode.GetBytes(msg);
s.Send(bb);
richTextBox2.Text = "";
richTextBox2.Focus();
}
//发送“客户端退出提示”
void sendExit()
{
string cmd = "CMD--EXIT";
byte[] bb = Encoding.Unicode.GetBytes(cmd);
s.Send(bb);
}
}
}
socket-server
--------------------------------------------------------------
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;
namespace socket_server
{
//服务器
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //可以调试时,不捕捉控件创建线程错误
}
Thread th;
Socket s1;//监听用
Socket s2;//聊天用
//监听
private void button1_Click(object sender, EventArgs e)
{
IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
//步骤1 创建网络端点IPEndPoint
IPEndPoint myServer = new IPEndPoint(ip, 888);
//步骤2 创建套接字Socket
s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//步骤3 套接字绑定到网络端点
s1.Bind(myServer);
label1.Text = ip+": 888 \n等待客户端连接......";
th = new Thread(new ThreadStart(AA));
th.IsBackground = true;
th.Start();
}
void AA()
{
//步骤4 监听
s1.Listen(5);
//步骤5 接受客户端连接
s2 = s1.Accept();
//步骤6 判断连接状态
if (s2.Connected)
{
label1.Text = "已有客户端连接!";
//步骤7 循环接收客户端消息
while (true)
{
byte[] bb = new byte[1024];
int i= s2.Receive(bb);
string removeMsg = Encoding.Unicode.GetString(bb,0,i);
if (removeMsg == "CMD--EXIT")//收到的是退出通知
{
label1.Text = "客户端已经取消了连接";
return;
}
richTextBox1.AppendText( removeMsg+"\n" );
richTextBox1.ScrollToCaret();
}
}
}
//停止监听
private void button2_Click(object sender, EventArgs e)
{
sendExit();//告诉客户端
s2.Shutdown(SocketShutdown.Both);
s1.Close();
th.Abort();
label1.Text = "无连接";
}
//发送消息
private void button3_Click(object sender, EventArgs e)
{
string msg = "服务器:" + richTextBox2.Text;
byte[] bb = Encoding.Unicode.GetBytes(msg);
s2.Send(bb);
richTextBox2.Text = "";
richTextBox2.Focus();
}
//发送“服务器退出提示”
void sendExit()
{
string cmd = "CMD--EXIT";
byte[] bb = Encoding.Unicode.GetBytes(cmd);
s2.Send(bb);
}
}
}
----------------------------------------------
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;
namespace socket_client
{
//客户端
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
Socket s;//聊天用
Thread th;
//连接
private void button1_Click(object sender, EventArgs e)
{
//步骤1 配置远程服务器信息
IPEndPoint removeServer = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
//步骤2 创建套接字
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//步骤3 套接字连接远程服务器
s.Connect(removeServer);
//步骤4 提示连接状态
if (s.Connected)
{
label4.Text = "连接服务器成功!";
//步骤5 循环接收服务器发来的消息
th = new Thread(new ThreadStart(BB));
th.IsBackground = true;
th.Start();
}
}
void BB()
{
while (true)
{
byte[] bb = new byte[1024];
int i= s.Receive(bb); //接收数据,返回每次接收的字节总数
string removeMsg = Encoding.Unicode.GetString(bb,0,i);
if (removeMsg == "CMD--EXIT")//收到的是退出通知
{
label4.Text = "无连接";
DialogResult re=MessageBox.Show("服务器已经关闭.\n\"确定\"后退出程序,\n\"取消\"继续停留!", "消息提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
MessageBox.Show(re.ToString());
if (re == DialogResult.OK)
{
sendExit();//告诉服务器我退出了
Application.Exit();
}
return;
}
richTextBox1.AppendText(removeMsg+"\n") ;
richTextBox1.ScrollToCaret();
}
}
//发送消息
private void button3_Click(object sender, EventArgs e)
{
string msg = "客户端:" + richTextBox2.Text;
byte[] bb = Encoding.Unicode.GetBytes(msg);
s.Send(bb);
richTextBox2.Text = "";
richTextBox2.Focus();
}
//发送“客户端退出提示”
void sendExit()
{
string cmd = "CMD--EXIT";
byte[] bb = Encoding.Unicode.GetBytes(cmd);
s.Send(bb);
}
}
}
socket-server
--------------------------------------------------------------
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;
namespace socket_server
{
//服务器
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //可以调试时,不捕捉控件创建线程错误
}
Thread th;
Socket s1;//监听用
Socket s2;//聊天用
//监听
private void button1_Click(object sender, EventArgs e)
{
IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
//步骤1 创建网络端点IPEndPoint
IPEndPoint myServer = new IPEndPoint(ip, 888);
//步骤2 创建套接字Socket
s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//步骤3 套接字绑定到网络端点
s1.Bind(myServer);
label1.Text = ip+": 888 \n等待客户端连接......";
th = new Thread(new ThreadStart(AA));
th.IsBackground = true;
th.Start();
}
void AA()
{
//步骤4 监听
s1.Listen(5);
//步骤5 接受客户端连接
s2 = s1.Accept();
//步骤6 判断连接状态
if (s2.Connected)
{
label1.Text = "已有客户端连接!";
//步骤7 循环接收客户端消息
while (true)
{
byte[] bb = new byte[1024];
int i= s2.Receive(bb);
string removeMsg = Encoding.Unicode.GetString(bb,0,i);
if (removeMsg == "CMD--EXIT")//收到的是退出通知
{
label1.Text = "客户端已经取消了连接";
return;
}
richTextBox1.AppendText( removeMsg+"\n" );
richTextBox1.ScrollToCaret();
}
}
}
//停止监听
private void button2_Click(object sender, EventArgs e)
{
sendExit();//告诉客户端
s2.Shutdown(SocketShutdown.Both);
s1.Close();
th.Abort();
label1.Text = "无连接";
}
//发送消息
private void button3_Click(object sender, EventArgs e)
{
string msg = "服务器:" + richTextBox2.Text;
byte[] bb = Encoding.Unicode.GetBytes(msg);
s2.Send(bb);
richTextBox2.Text = "";
richTextBox2.Focus();
}
//发送“服务器退出提示”
void sendExit()
{
string cmd = "CMD--EXIT";
byte[] bb = Encoding.Unicode.GetBytes(cmd);
s2.Send(bb);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
网易云信
2023-12-06 广告
2023-12-06 广告
很高兴能回答您的问题。以下是一段针对“一对一消息组件”的描述,字数在200字左右:该组件支持用户之间的私密交流,让沟通更加直接和高效。通过它,您可以向特定对象发送消息,并实时查看消息状态,包括对方是否已读或未读。同时,该组件还支持富文本消息...
点击进入详情页
本回答由网易云信提供
展开全部
使用套接字编程,上网搜一下,很多例子(SOCKET网络编程)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询