In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the complete steps of C# to write an online tourist client. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Implemented using Virtual Studio 2019.
First, know NetworkStream (Network flow)
1. Relevant knowledge points of NetworkStream
① NetworkStream can only be used in a protocol with TCP/IP. When used in UDP, there is no error, but an exception occurs.
② NetworkStream is connection oriented.
③ NetworkStream transmits information in the form of a stream.
2. Properties and methods commonly used in NetworkStream
Property or method explains whether the CanRead stream supports reading. You can read the data from the stream and return it to trueDataAvailable to see if there is data available on the read stream. It is often used to determine whether there is data in the broken stream Read (byte [] buffer,int offset,int size) reads the data into buffer and returns the number of bytes read successfully Write (byte [] buffer,int offset,int size) sends the data content in the buffer to the network.
Understanding NetworkStream will help you to use TcpClient to send and receive data later. If you are interested, you can also learn about StreamReader,StreamWriter.
Connect to the server
Code:
Private NetworkStream stream;private TcpClient tcpClient = new TcpClient (); public Form1 () {InitializeComponent (); try {/ / issue a connection request tcpClient.Connect ("10.160.52.106", 3900) to the server at the specified IP address; listBox1.Items.Add ("connection succeeded!") ; stream = tcpClient.GetStream (); byte [] data = new byte [1024]; / / determine whether the network flow is readable 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 = "\ r\ n"; 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("服务器未启动!"); }} 分析: 本过程是先为界面创建一个TCPClient对象属性,在窗体初始化的时候,就连接服务器,并把服务器返回的数据在ListBox中显示出来。由于一些转义控制字符无法进行解析,返回的数据会出现一些特殊字符。 运行效果: 在显示返回的数据时,最开始采用UTF-8编码的时候,显示回的数据是乱码的情况。 解决方式: ①采用Default的方式 string msg = Encoding.Default.GetString(data, 0, data.Length); ②进行编码转换(GB转换为UTF-8) 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); 获得的数据直接采用listBox的Add方法添加,显示只会显示一行数据,不会进行换行 解决方式(对字符串以\r\n为分隔符进行分割,循环显示) string str = "\r\n"; char[] str1 = str.ToCharArray(); string[] msg1=msg.Split(str1); for(int j = 0; j < msg1.Length; j++) { listBox1.Items.Add(msg1[j]); } 三、客户端向服务器发送数据 代码: private void button12_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++) { listBox1.Items.Add(msg2[j]); } } } else { listBox1.Items.Add("连接已断开"); }} 分析: 将输入到textBox中的信息发送给服务器,再将服务器发送回来的数据给添加到ListBox中显示出来。 运行效果: 最开始发送数据的时候,服务器不返回任何数据 解决方式: 在发送的数据内容后面加上回车(\n),需要回车的原因是来表示客服端向服务器发送某个消息的结束标识。(仅是笔者的看法,并不准确) 每次在textBox输入信息,都有先删除上一次的内容,比较麻烦 解决方式: 在按钮事件中添加textBox1.Clear()语句 四、实现播放背景音乐 添加WindowsMediaPlayer控件 没有的情况下,添加控件的方法 选择常规后,右键,再选择选择项,就会弹出一个新界面,在新界面上选择COM组件。接下来就是找到所需要的控件,将其勾选上。 代码: private void button13_Click(object sender, EventArgs e){ string s = @"D:\game1\game1\bin\Debug\Wav\mp3\Kapuskasing.mp3"; axWindowsMediaPlayer1.URL = s;}private void button14_Click(object sender, EventArgs e){ //用于暂停正在播放的背景音乐 axWindowsMediaPlayer1.Ctlcontrols.pause();} 分析: 播放和停止播放是通过两个按钮的点击来实现的。当点击播放按钮后,就会播放背景音乐,需要暂停播放,就点击停止按钮就可以实现停止。 运行效果:In fact, after clicking the play button, there is background music playing. When you click stop, it's gone.
Fifth, realize the background picture transformation of the game.
Code:
Int flag = 0 private void timer1_Tick (object sender, EventArgs e) {flag++; string picturePath = @ "D:\ game1\ game1\ bin\ Debug\ imag\" + flag+ ".jpg"; pictureBox1.Image = Image.FromFile (picturePath); if (flag = = 5) {flag = 0;}}
Analysis:
This process is implemented using the tick event of the Timer control, where Timer is re-executed every 3 seconds, and then the image path of the pictureBox control is modified. Thus, the transformation of the picture is realized.
Running effect:
How to set the time interval of timer
Under the interface design, select the timer control, then in the properties area on the right, find Interval, and set its value to 100 (0.1s) by default. You also need to change the value of Enable to true.
VI. Summary
The main purpose of this process is to realize part of the functions of a simple network tourist client. Background music and background picture transformation two functions. I'm not the only one to show this function, but there are other ways to do it. The whole process still takes a long time, mainly for some controls are not very familiar with. The design part of the interface is the frame code issued by the teacher, and the time function of some controls has not been realized yet. At first, the connection to the server was not successful, and there should be a problem with the corresponding server. After successfully connecting to the server, it took a lot of time to send and receive data. At first, there was some garbled code in the accepted data, but I didn't understand what was going on. Finally, I realized that it was the coding problem and some escape control characters that could not be escaped to be displayed correctly. When using the loop, there are no problems such as reporting errors, but click to run, that is, there is no interface pop-up, remove the loop part, but there is an interface, I do not know what the situation is. Looking at the information on the Internet, I didn't find anything wrong with it. Finally, I don't know what happened, but it worked again.
These are the complete steps of compiling a web visitor client for the C# shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.