-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cs
More file actions
55 lines (46 loc) · 1.76 KB
/
user.cs
File metadata and controls
55 lines (46 loc) · 1.76 KB
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
using System;
using System.Net.Sockets;
using System.Text;
class ChatRoomClient {
private const string serverIp = "0.0.0.0";
private const int serverPort = 8888;
private string username;
public void Start() {
Console.Write("Enter your username: ");
username = Console.ReadLine();
try {
TcpClient client = new TcpClient(serverIp, serverPort);
NetworkStream stream = client.GetStream();
Console.WriteLine("Connected to chat room server.");
string joinMessage = "Join:" + username;
byte[] joinBuffer = Encoding.ASCII.GetBytes(joinMessage);
stream.Write(joinBuffer, 0, joinBuffer.Length);
byte[] initialBuffer = new byte[1024];
int initialBytesRead = stream.Read(initialBuffer, 0, initialBuffer.Length);
string initialMessage = Encoding.ASCII.GetString(initialBuffer, 0, initialBytesRead);
Console.WriteLine(initialMessage);
while (true) {
string message = Console.ReadLine();
if (message.ToLower() == "exit") {
byte[] exitBuffer = Encoding.ASCII.GetBytes("exit");
stream.Write(exitBuffer, 0, exitBuffer.Length);
break;
}
message = username + " " + message;
byte[] buffer = Encoding.ASCII.GetBytes(message);
stream.Write(buffer, 0, buffer.Length);
}
stream.Close();
client.Close();
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
class Program {
static void Main(string[] args) {
ChatRoomClient client = new ChatRoomClient();
client.Start();
}
}