TCP客户端连接循环

编程入门 行业动态 更新时间:2024-10-22 23:24:12
本文介绍了TCP客户端连接循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿伙计们, 我正在为我的一个游戏制作服务器,以便我可以玩多人游戏,但我不能让这一点工作。到目前为止,我可以使服务器工作,以便服务器启动,然后客户端,但我想它,以便它能够作为客户端启动并进入连接循环,以便我继续搜索服务器然后当服务器启动时,它会连接。任何人都可以帮助我吗? 代码:

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.IO; 使用 System.Net; 使用 System.Net.Sockets; namespace TCP_Server_Test___Client { class Program { 静态 void Main( string [] args) { Console.Write( root [。 ] $); string cmd = Console.ReadLine(); if (cmd.StartsWith( connect )) { string op = cmd.Substring( 8 ); string ipaddress = op.Substring( 0 ,op.IndexOf( :)); ipaddress = ipaddress.Replace( , ); int port = Convert.ToInt32(op.Substring(op.IndexOf( :)+ 1 )); Console.Clear(); Console.Write( password:); string pass = Console.ReadLine(); if (pass == password ) { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine( Connecting ......); 连接: tcpclnt.Connect(ipaddress,port); Console.WriteLine( 已连接); 开始: Console.Write( Klunus [。] $) ; string str = Console.ReadLine(); byte [] data = Encoding.ASCII.GetBytes(str); NetworkStream netStream = tcpclnt.GetStream(); if (netStream.CanWrite) { netStream.Write(data, 0 ,data.Length); } goto 开始; } catch (例外e) { Console.WriteLine(e.Message); } } } } } }

解决方案

); string cmd = Console.ReadLine(); if (cmd.StartsWith( connect)) { string op = cmd.Substring( 8 ); string ipaddress = op.Substring( 0 ,op.IndexOf( :)); ipaddress = ipaddress.Replace( , ); int po rt = Convert.ToInt32(op.Substring(op.IndexOf( :)+ 1 )); Console.Clear(); Console.Write( password:); string pass = Console.ReadLine(); if (pass == password ) { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine( Connecting ......); 连接: tcpclnt.Connect(ipaddress,port); Console.WriteLine( 已连接); 开始: Console.Write( Klunus [。]

); string str = Console.ReadLine(); byte [] data = Encoding.ASCII.GetBytes(str); NetworkStream netStream = tcpclnt.GetStream(); if (netStream.CanWrite) { netStream.Write(data, 0 ,data.Length); } goto 开始; } catch (例外e) { Console.WriteLine(e.Message); } } } } } }

hey guys, i am making a server for one of my games so that i can play multiplayer, but i can''t get this one bit working. so far i can make a server that works so that the server starts up and then the client, but i want it so that it is able to work as the client starting up and going into a connect loop so that i keeps searching for the server and then when the server starts up, it connects. can anyone help me? code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; namespace TCP_Server_Test___Client { class Program { static void Main(string[] args) { Console.Write("root[.]$ "); string cmd = Console.ReadLine(); if (cmd.StartsWith("connect ")) { string op = cmd.Substring(8); string ipaddress = op.Substring(0, op.IndexOf(":")); ipaddress = ipaddress.Replace(" ", ""); int port = Convert.ToInt32(op.Substring(op.IndexOf(":") + 1)); Console.Clear(); Console.Write("password: "); string pass = Console.ReadLine(); if (pass == "password") { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting..."); Connect: tcpclnt.Connect(ipaddress, port); Console.WriteLine("Connected"); Begin: Console.Write("Klunus[.]$ "); string str = Console.ReadLine(); byte[] data = Encoding.ASCII.GetBytes(str); NetworkStream netStream = tcpclnt.GetStream(); if (netStream.CanWrite) { netStream.Write(data, 0, data.Length); } goto Begin; } catch (Exception e) { Console.WriteLine(e.Message); } } } } } }

解决方案

"); string cmd = Console.ReadLine(); if (cmd.StartsWith("connect ")) { string op = cmd.Substring(8); string ipaddress = op.Substring(0, op.IndexOf(":")); ipaddress = ipaddress.Replace(" ", ""); int port = Convert.ToInt32(op.Substring(op.IndexOf(":") + 1)); Console.Clear(); Console.Write("password: "); string pass = Console.ReadLine(); if (pass == "password") { try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting..."); Connect: tcpclnt.Connect(ipaddress, port); Console.WriteLine("Connected"); Begin: Console.Write("Klunus[.]

"); string str = Console.ReadLine(); byte[] data = Encoding.ASCII.GetBytes(str); NetworkStream netStream = tcpclnt.GetStream(); if (netStream.CanWrite) { netStream.Write(data, 0, data.Length); } goto Begin; } catch (Exception e) { Console.WriteLine(e.Message); } } } } } }

更多推荐

TCP客户端连接循环

本文发布于:2023-11-25 09:07:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   TCP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!