using System; using System.Net.Sockets; using System.Text; using System.Windows.Forms; namespace Client { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static Socket ClientSock; private void Form1_Load(object sender, EventArgs e) { ClientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ClientSock.Connect("127.0.0.1", 1050); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { NetworkStream Clientstream = new NetworkStream(ClientSock); byte[] buffer1 = System.Text.Encoding.ASCII.GetBytes(textBox1.Text); ClientSock.Send(buffer1); Clientstream.Flush(); listBox1.Items.Add("Me: " + textBox1.Text); textBox1.Clear(); } } private void button1_Click(object sender, EventArgs e) { byte[] buffer = new byte[100]; NetworkStream Clientstream = new NetworkStream(ClientSock); ClientSock.Receive(buffer); string Data = Encoding.ASCII.GetString(buffer); listBox1.Items.Add("Server: " + Data); Clientstream.Flush(); } } } using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Windows.Forms; namespace Server1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static Socket ServerSock; private void Form1_Load(object sender, EventArgs e) { ServerSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint SockAdd = new IPEndPoint(IPAddress.Any, 1050); ServerSock.Bind(SockAdd); ServerSock.Listen(100); ServerSock = ServerSock.Accept(); } private void button1_Click(object sender, EventArgs e) { try { NetworkStream Serverstream = new NetworkStream(ServerSock); byte[] buffer2 = new byte[100]; ServerSock.Receive(buffer2); string Data = Encoding.ASCII.GetString(buffer2); listBox1.Items.Add("Maryam: " + Data); Serverstream.Flush(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { NetworkStream Serverstream = new NetworkStream(ServerSock); byte[] buffer3 = new byte[100]; buffer3 = System.Text.Encoding.ASCII.GetBytes(textBox1.Text); ServerSock.Send(buffer3); Serverstream.Flush(); listBox1.Items.Add("Me: " + textBox1.Text); textBox1.Clear(); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)