/
(echo)Program.cs
75 lines (64 loc) · 2.34 KB
/
(echo)Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TcpEchoClient
{
class TcpEchoClient
{
static void Main(string[] args)
{
Console.Title = "Client";
Console.WriteLine("Starting echo client...");
int port=12346,match=0;
Int32 portt = 13000;
string a = "",b="";
TcpClient client = null;
while (match == 0)
{
Console.WriteLine("Please enter the server address");
a = Console.ReadLine();
//a = "127.0.0.1";
Console.WriteLine("Please enter the port number");
b = Console.ReadLine();
portt = Convert.ToInt32(b);
IPAddress localAddr = IPAddress.Parse(a);
try
{
client = new TcpClient(localAddr.ToString(), portt);
match = 1;
Console.WriteLine("Connection established successfully!");
}
catch {
match = 0;
client = null;
Console.WriteLine("Unable to establish connection!");
}
}
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream) { AutoFlush = true };
while (true)
{
Console.Write("Enter text to send: ");
string lineToSend = "";
lineToSend +=Console.ReadLine();
writer.WriteLine(lineToSend);
if (lineToSend != "end-of-session")
{
Console.WriteLine("Sending to server: " + lineToSend);
string lineReceived = reader.ReadLine();
Console.WriteLine("Server response: " + lineReceived);
}
else
{
Console.WriteLine("Good Bye");
Thread.Sleep(1000);
return;
}
}
client.Close();
}
}
}