View difference between Paste ID: R7YsKkSP and nhvuCDkX
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Text;
3
using System.Net.Sockets;
4
using System.Net;
5
6
7
namespace Klient_ECHO
8
{
9
    class Program
10
    {
11
12
        public static void closeConnection(Socket socket)
13
        {
14
            Console.WriteLine("Disconnected from server...");
15
            socket.Shutdown(SocketShutdown.Both);
16
            socket.Close();
17
            Console.ReadLine();
18
        }
19
20
        public static void tryConnection(Socket socket)
21
        {
22
            
23
            bool tryAgain = true;
24
            while (tryAgain)
25
            {
26
                Console.Write("Which port you want to connect [7]? : ");
27
                string port = Console.ReadLine(); if (port == "") port = "7";
28
               
29
                try
30
                {
31
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(port));
32
                
33
                    try
34
                    {
35
                        socket.Connect(ipep);
36
                        Console.WriteLine("Connected to " + ipep.ToString());
37
                        tryAgain = false;
38
                    }
39
                    catch (SocketException e)
40
                    {
41
                        Console.Write("Unable to connect to server, want to try with another port? [y/n]: ");
42-
                        if (ca.ToLower() == "y" || ca.ToLower() == "yes") tryAgain = true;
42+
43
                        if (ca.ToLower() == "y" || ca.ToLower() == "yes" || ca == "") tryAgain = true;
44
                        else tryAgain = false;
45
                    }
46
                }
47
                catch (FormatException e)
48
                {
49
                    Console.WriteLine("This is not a number, try again.");
50
                }
51
52
            }
53
        }
54
55
        static void Main(string[] args)
56
        {
57
            byte[] data = new byte[1024];
58
            string input, stringData;
59
                  
60
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
61
62
            tryConnection(socket);
63
64
            int recv = 0;
65
            stringData = Encoding.ASCII.GetString(data, 0, recv);
66
            Console.WriteLine(stringData);
67
68
            while (true)
69
            {
70
                input = Console.ReadLine();
71-
                if (input == "close") break;
71+
72
                if (input == "App:CloseConnection(1)" || input == "") break;
73
                else
74
                {
75
                    socket.Send(Encoding.ASCII.GetBytes(input));
76
                    data = new byte[1024];
77
                    recv = socket.Receive(data);
78
                    stringData = Encoding.ASCII.GetString(data, 0, recv);
79
                    Console.WriteLine("Reply from server: " + stringData);
80
                }
81
            }
82
83
            closeConnection(socket);
84
        }
85
    }
86
}