Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement simple Game client based on TCP in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to implement a simple game client based on TCP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Interface

The audio button at the top left and the stop button below are the picture display below, and the next is to enter and exit the game, and finally the command input box to the right is the message box.

Second, Code 1. Play audio

You need to use the WindowsMediaPlayer component, which can be found in the selection of regular components.

Implementation code

Private void start_Click (object sender, EventArgs e) {/ / New thread opens audio file new Thread (new ThreadStart (beginPlay)) .Start ();} private void stop_Click (object sender, EventArgs e) {/ / tentative playback axWindowsMediaPlayer1.Ctlcontrols.pause () } public void beginPlay () {string s = @ "G:\ VisualStudioProject\ GameWindows\ resources\ music.mp3"; axWindowsMediaPlayer1.URL = s;} 2. Play the picture

Use a timer to enable and set the event to be triggered once per 1000ms

Event function

Private String [] pictures= {"\ 1.jpg", "\\ 2.jpg", "\\ 3.jpg", "\\ 4.jpg", "5.jpg", "6.jpg"}; private int postion = 0; private void timer1_Tick (object sender, EventArgs e) {pictureBox1.Image = Image.FromFile ("G:\\ VisualStudioProject\\ GameWindows\\ resources\" + pictures [(+ + postion)% pictures.Length]);} 3. Log in and exit the game

Using tcp protocol to establish a connection with the server

Public void connection () {try {/ / issue a connection request tcpClient.Connect ("10.1.230.74", 3900) to the server at the specified IP address; messages.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) 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) ) {messages.Items.Add (msg1 [j]);} catch (System.Exception ex) {messages.Items.Add (ex.Message);}}

Log in to the game to establish a tcp connection, and disable the login button to enable the exit button to exit the game port tcp connection, and disable the exit button and enable login button

Private void quitGame_Click (object sender, EventArgs e) {tcpClient.Close (); beginGame.Enabled = true; quitGame.Enabled = false;} 4. Command interaction

Send data to the server through tcp

Private void sendButton_Click (object sender, EventArgs e) {sendCommand ();} public void sendCommand () {if (tcpClient.Connected) {/ / send data string msg = command.Text to the server Byte [] outbytes = System.Text.Encoding.Default.GetBytes (msg + "\ n"); stream.Write (outbytes, 0, outbytes.Length); byte [] data = new byte [1024] / / receive server reply data 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; jacks +) {messages.Items.Add (msg2 [j]) } else {messages.Items.Add ("connection disconnected");}} 5. Information display

Display all received messages through listbox

6. The final code using System;using System.Drawing;using System.Net;using System.Net.Sockets;using System.Windows.Forms;using System.Text;using System.Threading;namespace GameWindows {public partial class Form1: Form {private NetworkStream stream; private TcpClient tcpClient = new TcpClient () Private String [] pictures= {"\ 1.jpg", "\\ 2.jpg", "\\ 3.jpg", "\\ 4.jpg", "5.jpg", "6.jpg"}; private int postion = 0; public Form1 () {InitializeComponent (); CheckForIllegalCrossThreadCalls = false; quitGame.Enabled = false; axWindowsMediaPlayer1.Hide () PictureBox1.Image = Image.FromFile ("G:\\ VisualStudioProject\\ GameWindows\\ resources\" + pictures [postion]);} private void start_Click (object sender, EventArgs e) {/ / New thread opens the audio file new Thread (new ThreadStart (beginPlay)) .Start () } private void stop_Click (object sender, EventArgs e) {/ / tentatively play axWindowsMediaPlayer1.Ctlcontrols.pause ();} private void beginGame_Click (object sender, EventArgs e) {Thread thread = new Thread (new ThreadStart (connection)); thread.Start (); beginGame.Enabled = false QuitGame.Enabled = true;} private void quitGame_Click (object sender, EventArgs e) {tcpClient.Close (); beginGame.Enabled = true; quitGame.Enabled = false } private void timer1_Tick (object sender, EventArgs e) {pictureBox1.Image = Image.FromFile ("G:\\ VisualStudioProject\\ GameWindows\\ resources\" + pictures [(+ + postion)% pictures.Length]);} private void sendButton_Click (object sender, EventArgs e) {sendCommand () } public void connection () {try {/ / issue a connection request tcpClient.Connect ("10.1.230.74", 3900) to the server at the specified IP address; messages.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) 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) System.Exception ex +) {messages.Items.Add (msg1 [j]);} catch (System.Exception ex) {messages.Items.Add (ex.Message) }} public void beginPlay () {string s = @ "G:\ VisualStudioProject\ GameWindows\ resources\ music.mp3"; axWindowsMediaPlayer1.URL = s } public void sendCommand () {if (tcpClient.Connected) {/ / send data to the server string msg = command.Text; Byte [] outbytes = System.Text.Encoding.Default.GetBytes (msg + "\ n"); stream.Write (outbytes, 0, outbytes.Length) Byte [] data = new byte [1024]; / / receive server reply data 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; jacks +) {messages.Items.Add (msg2 [j]) } else {messages.Items.Add ("connection disconnected");} III. Effect

In order to facilitate the display of pictures, the playback interval is set for the 1000ms.

This is the end of the content of "how to implement a simple game client based on TCP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report