Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
                    }
                }
            }
        }
    }
}
 
Posted 17-Dec-12 13:07pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100