首页 技术 正文
技术 2022年11月9日
0 收藏 989 点赞 2,815 浏览 1907 个字

C# 网络编程 TcpListener

1.服务断代码

 public partial class Server : Form
{ private bool lk = true; public Server()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
} private void button1_Click(object sender, EventArgs e)
{
IPAddress ip = IPAddress.Parse(textBox1.Text); TcpListener server = new TcpListener(ip, int.Parse(textBox2.Text));
server.Start(); TaskFactory tasks = new TaskFactory();
string ipaddress = string.Empty;
TcpClient client = null;
while (lk)
{
Console.WriteLine("等待连接。。。");
client = server.AcceptTcpClient(); tasks.StartNew(() => HandleClient(client, ipaddress)).Wait();
}
} private void HandleClient(TcpClient tcpclient, string ipadd)
{ lock (tcpclient)
{
if (tcpclient == null)
{
return;
} // Buffer for reading data
Byte[] bytes = new Byte[];
String data = null; // Enter the listening loop.
while (tcpclient.Connected)
{ data = null; NetworkStream stream = tcpclient.GetStream(); int i; if ((i = stream.Read(bytes, , bytes.Length)) != )
{
data = System.Text.Encoding.UTF8.GetString(bytes, , i); byte[] msg = System.Text.Encoding.UTF8.GetBytes(data); //stream.Write(msg, 0, msg.Length);
textBox3.AppendText(data);
}
tcpclient.Close();
}
}
} }

2.客户端代码

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} TcpClient client = null;
private void button1_Click(object sender, EventArgs e)
{ client = new TcpClient();
try
{
//if (!client.Connected)
// client.Close();
client.Connect(tbIP.Text, int.Parse(tbPort.Text)); // 与服务器连接
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
} textBox2.Text = "连接成功";
} private void textBox1_TextChanged(object sender, EventArgs e)
{ } private void button2_Click(object sender, EventArgs e)
{
button1_Click(null, null); string msg = textBox1.Text;
if (msg != "")
{
NetworkStream streamToServer = client.GetStream(); //创建一个客户端的NetworkStream对象
byte[] buffer = Encoding.UTF8.GetBytes(msg); // 获得缓存 streamToServer.Write(buffer, , buffer.Length); // 发往服务器
int numb = streamToServer.Read(buffer, , buffer.Length); //接收来自服务器传回来的数据,保存到buffer数组(byte型)中去
string s = Encoding.UTF8.GetString(buffer, , buffer.Length); //将数组中的内容转化成string字符串,并且输出
textBox2.AppendText(s + "\r\n");
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,089
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,566
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906