-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.cs
96 lines (81 loc) · 2.83 KB
/
Chat.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HeroForge_OnceAgain
{
public partial class Chat : Form
{
public Chat()
{
InitializeComponent();
}
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;
private void button1_Click(object sender, EventArgs e)
{
try
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox91.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] bytesFrom = new byte[10000];
int read = serverStream.Read(bytesFrom, 0, bytesFrom.Length);
string returndata = System.Text.Encoding.ASCII.GetString(bytesFrom, 0, read);
if (read > 0)
{
msg(returndata);
textBox91.Text = "";
textBox91.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
richTextBox1.Text = richTextBox1.Text + Environment.NewLine + " >> " + ex.Message;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
clientSocket.Connect("127.0.0.1", 8888);
label37.Text = "RPGChat - Servidor Conectado ...";
msg(textBoxNick.Text+" logado");
}
catch (Exception ex)
{
MessageBox.Show("Ocorreu um erro ao tentar conectar com o servidor.");
}
}
public void msg(string mesg)
{
richTextBox1.Text = richTextBox1.Text + " >> " + mesg + Environment.NewLine;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
// set the current caret position to the end
richTextBox1.SelectionStart = richTextBox1.Text.Length;
// scroll it automatically
richTextBox1.ScrollToCaret();
}
public void openSocket()
{
if (clientSocket.Connected)
{
clientSocket.Close();
}
clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("127.0.0.1", 8888);
label37.Text = "Client Socket Program - Server Connected ...";
}
}
}