概述
目录
- 一、端口扫描器程序
- a、单线程
- b、多线程
- 二、网游客户端
- a、TCP连接并显示和操作
- b、30秒变换一次背景图片
- c、播放背景音乐
一、端口扫描器程序
a、单线程
创建新项目:
设计页面如下:
写入代码:
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace PortScan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//自定义变量
private int port;//记录当前扫描的端口号
private string Address;//记录扫描的系统地址
private bool[] done = new bool[65536];//记录端口的开放状态
private int start;//记录扫描的起始端口
private int end;//记录扫描的结束端口
private bool OK;
private void button1_Click(object sender, EventArgs e)
{
label4.Text = textBox2.Text;
label6.Text = textBox3.Text;
progressBar1.Minimum = Int32.Parse(textBox2.Text);
progressBar1.Maximum = Int32.Parse(textBox3.Text);
listBox1.Items.Clear();
listBox1.Items.Add("端口扫描器v1.0.");
listBox1.Items.Add("");
PortScan();
}
private void PortScan()
{
start = Int32.Parse(textBox2.Text);
end = Int32.Parse(textBox3.Text);
//判断输入端口是否合法
if((start>=0&&start<=65536)&&(end>=0&&end<=65536)&&(start<=end))
{
listBox1.Items.Add("开始扫描:这个过程可能需要等待几分钟!");
Address = textBox1.Text;
for(int i = start; i <= end; i++)
{
port = i;
Scan();
progressBar1.Value = i;
label5.Text = i.ToString();
}
while (!OK)
{
OK = true;
for(int i = start; i <= end; i++)
{
if (!done[i])
{
OK = false;
break;
}
}
}
listBox1.Items.Add("扫描结束!");
}
else
{
MessageBox.Show("输入错误,端口范围为[0,65536]");
}
}
//连接端口
private void Scan()
{
int portnow = port;
done[portnow] = true;
TcpClient objTCP = null;
try
{
objTCP = new TcpClient(Address, portnow);
listBox1.Items.Add("端口"+portnow.ToString()+"开放");
}
catch
{
}
}
}
}
结果:
b、多线程
同理:
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PortScan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//自定义变量
private int port;//记录当前扫描的端口号
private string Address;//记录扫描的系统地址
private bool[] done = new bool[65536];//记录端口的开放状态
private int start;//记录扫描的起始端口
private int end;//记录扫描的结束端口
private bool OK;
private Thread scanThread;
//将输入的起始端口放到进度条的开始位置
private void label4_TextChanged(object sender, EventArgs e)
{
label4.Text = textBox2.Text;
}
//将输入的结束地址放到进度条的结束位置
private void label6_TextChanged(object sender, EventArgs e)
{
label6.Text = textBox3.Text;
}
private void button1_Click(object sender, EventArgs e)
{
label4_TextChanged(sender, e);
label6_TextChanged(sender, e);
//创建线程,并创建ThreadStart委托对象
Thread procss = new Thread(new ThreadStart(PortScan));
procss.Start();
//显示端口扫描范围
progressBar1.Minimum = Int32.Parse(textBox2.Text);
progressBar1.Maximum = Int32.Parse(textBox3.Text);
//显示框的初始化
listBox1.Items.Clear();
listBox1.Items.Add("端口扫描器v1.0.");
listBox1.Items.Add("");
}
private void PortScan()
{
start = Int32.Parse(textBox2.Text);
end = Int32.Parse(textBox3.Text);
//检查端口的合法性
if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end))
{
listBox1.Items.Add("开始扫描:这个过程可能需要等待几分钟!");
Address = textBox1.Text;
for (int i = start; i <= end; i++)
{
port = i;
//对该端口进行扫描的线程
scanThread = new Thread(Scan);
scanThread.Start();
//使线程睡眠
System.Threading.Thread.Sleep(100);
progressBar1.Value = i;
label5.Text = i.ToString();
}
//未完成时情况
while (!OK)
{
OK = true;
for (int i = start; i <= end; i++)
{
if (!done[i])
{
OK = false;
break;
}
}
}
listBox1.Items.Add("扫描结束!");
System.Threading.Thread.Sleep(1000);
}
else
{
MessageBox.Show("输入错误,端口范围为[0,65536]");
}
}
private void Scan()
{
int portnow = port;
//创建线程变量
Thread Threadnow = scanThread;
done[portnow] = true;
//创建TcpClient对象,TcpClient用于TCP网络服务提供客户端连接
TcpClient objTCP = null;
//扫描端口,成功就写入信息
try
{
objTCP = new TcpClient(Address, portnow);
listBox1.Items.Add("端口" + portnow.ToString() + "开放!");
}
catch
{
}
}
}
}
结果:
对比:
多线程速度明显更快。
二、网游客户端
a、TCP连接并显示和操作
同理新创建一个项目:
进行页面设计如下:
写入代码:(注意头文件的添加)
public partial class Form1 : Form
{
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
public Form1()
{
InitializeComponent();
try
{
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.1.230.41", 3900);
listBox1.Items.Add("连接成功!");
stream = tcpClient.GetStream();
byte[] data = new byte[1024];
//判断网络流是否可读
if (stream.CanRead)
{
int len = stream.Read(data, 0, data.Length);
//Encoding ToEncoding = Encoding.GetEncoding("UTF-8");
//Encoding FromEncoding = Encoding.GetEncoding("GB2312");
//data=Encoding.Convert(FromEncoding, ToEncoding, data);
//string msg = Encoding.UTF8.GetString(data, 0, data.Length);
string msg = Encoding.Default.GetString(data, 0, data.Length);
string str = "rn";
char[] str1 = str.ToCharArray();
string[] msg1 = msg.Split(str1);
for (int j = 0; j < msg1.Length; j++)
{
listBox1.Items.Add(msg1[j]);
}
}
}
catch
{
listBox1.Items.Add("服务器未启动!");
}
//以上代码可以单独直接运行
}
private void button3_Click(object sender, EventArgs e)
{
//判断连接是否断开
if (tcpClient.Connected)
{
//向服务器发送数据
string msg = textBox1.Text;
Byte[] outbytes = System.Text.Encoding.Default.GetBytes(msg + "n");
stream.Write(outbytes, 0, outbytes.Length);
byte[] data = new byte[1024];
//接收服务器回复数据
if (stream.CanRead)
{
int len = stream.Read(data, 0, data.Length);
string msg1 = Encoding.Default.GetString(data, 0, data.Length);
string str = "rn";
char[] str1 = str.ToCharArray();
string[] msg2 = msg1.Split(str1);
for (int j = 0; j < msg2.Length; j++)
{
listBox1.Items.Add(msg2[j]);
}
}
}
else
{
listBox1.Items.Add("连接已断开");
}
}
直接运行结果:(输入hello,左侧显示服务器反馈)
b、30秒变换一次背景图片
添加timer控件并设置:
写入如下代码:
private void timer1_Tick(object sender, EventArgs e)
{
{
flag++;
string picturePath = @"C:UsershpDesktopnetworknetworkbinDebug" + flag + ".jpg";
pictureBox1.Image = Image.FromFile(picturePath);
if (flag == 2)
{
flag = 0;
}
}
}
运行效果:
c、播放背景音乐
添加Windows media player:
写入以下代码:
private void button13_Click(object sender, EventArgs e)
{
string s = @"C:UsershpDesktopnetworknetworkbinDebug萨顶顶 - 不染.flac";
axWindowsMediaPlayer1.URL = s;
}
private void button14_Click(object sender, EventArgs e)
{
//用于暂停正在播放的背景音乐
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
运行结果:
文章参考自:
https://blog.csdn.net/qq_43279579?t=1
https://blog.csdn.net/xwmrqqq/article/details/109780201
最后
以上就是友好纸鹤为你收集整理的简易网游客户端一、端口扫描器程序二、网游客户端的全部内容,希望文章能够帮你解决简易网游客户端一、端口扫描器程序二、网游客户端所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复