编译软件:Visual Studio 2019
编译环境:Windows 10
使用语言:C#
本文章编写一个网游客户端,游戏服务器的校园内网IP地址为 10.160.230.74,端口为3900,采用TCP连接,主要功能有四个,如下:
①连接成功后,可以将服务器发来的消息不停地显示在 textbox 中;
②客户端要发给服务器的数据,通过 textbox 输入或者点击 button ;
③能够播放背景音乐;
④每隔30秒,变换一次游戏背景图片。
本文章涉及到了各个控件的基本使用、TCP的连接、客户端与服务器端的数据传输等等。
实现功能:点击连接游戏按钮后,能连接服务器,若连接成功,则将服务器发来的消息不停地显示在 textbox 中;若连接失败,则在 textbox 中显示 “ 服务器未启动 ” 。
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
private void button2_Click(object sender, EventArgs e)
{
/*
* 连接服务器
*/
try
{
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.1.230.74", 3900);
textBox2.AppendText("连接成功!");
stream = tcpClient.GetStream();
byte[] data = new byte[1024];
//判断网络流是否可读
if (stream.CanRead)
{
int len = stream.Read(data, 0, data.Length);
string msg = Encoding.Default.GetString(data, 0, data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
string[] msg1 = msg.Split(str1);
for (int j = 0; j < msg1.Length; j++)
{
textBox2.AppendText(msg1[j]);
}
}
}
catch
{
textBox2.AppendText("服务器未启动!");
}
}
实现功能:客户端要发给服务器的数据,通过 textbox 输入,然后点击 button 发送出去,并将服务器端返回结果显示在 textbox 中。
private void button1_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 = "\r\n";
char[] str1 = str.ToCharArray();
string[] msg2 = msg1.Split(str1);
for (int j = 0; j < msg2.Length; j++)
{
textBox2.AppendText(msg2[j]);
}
//刷新输入框
textBox1.Clear();
//将光标集中到输入框中
textBox1.Focus();
}
}
else
{
textBox2.AppendText("连接已断开");
}
}
实现功能:当进入游戏后,开始播放背景音乐,并能调节音量大小。
private string s = @"I:\IOT\CsProject\GameClient\data\mp3\Bloom_of_Youth.mp3";
private void button4_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void button5_Click(object sender, EventArgs e)
{
//用于暂停正在播放的背景音乐
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
代码:
private void timer1_Tick(object sender, EventArgs e)
{
flag++;
string picturePath = @"I:\IOT\CsProject\GameClient\data\pictures\" + flag + ".jpeg";
//设置图片填充
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (flag == 4)
{
flag = 0;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GameClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
trackBar1.Value = 0;
label1.Text = "0";
}
//分别创建 TcpClient 和 NetworkStream 的对象
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
//定义flag变量
private int flag = 0;
//字符串存储音乐路径
private string s = @"I:\IOT\CsProject\GameClient\data\mp3\Bloom_of_Youth.mp3";
/*****************
* 进入游戏
*****************/
private void button2_Click(object sender, EventArgs e)
{
/*
* 连接服务器
*/
try
{
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.1.230.74", 3900);
textBox2.AppendText("连接成功!");
stream = tcpClient.GetStream();
byte[] data = new byte[1024];
//判断网络流是否可读
if (stream.CanRead)
{
int len = stream.Read(data, 0, data.Length);
string msg = Encoding.Default.GetString(data, 0, data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
string[] msg1 = msg.Split(str1);
for (int j = 0; j < msg1.Length; j++)
{
textBox2.AppendText(msg1[j]);
}
}
}
catch
{
textBox2.AppendText("服务器未启动!");
}
/******************
* 播放背景音乐
******************/
//设置音量键值
trackBar1.Value = 30;
label1.Text = "30";
//设置为循环播放
axWindowsMediaPlayer1.settings.setMode("loop", true);
//设置初始音乐的音量大小(范围:0——100)
axWindowsMediaPlayer1.settings.volume = 30;
//设置播放歌曲的路径
axWindowsMediaPlayer1.URL = s;
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
}
private void button1_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 = "\r\n";
char[] str1 = str.ToCharArray();
string[] msg2 = msg1.Split(str1);
for (int j = 0; j < msg2.Length; j++)
{
textBox2.AppendText(msg2[j]);
}
//刷新输入框
textBox1.Clear();
//将光标集中到输入框中
textBox1.Focus();
}
}
else
{
textBox2.AppendText("连接已断开");
}
}
private void button4_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void button5_Click(object sender, EventArgs e)
{
//用于暂停正在播放的背景音乐
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
//控制音量大小
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
//控制音量显示
label1.Text = trackBar1.Value.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
flag++;
string picturePath = @"I:\IOT\CsProject\GameClient\data\pictures\" + flag + ".jpeg";
//设置图片填充
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(picturePath);
if (flag == 4)
{
flag = 0;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (stream != null)
{
stream.Close();
tcpClient.Close();
}
textBox2.Clear();
//停止播放背景音乐
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
}
}
AxWindowsMediaPlayer的详细用法