what i am trying to do is create a chat!
my main is a console application that receives information.
<code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Any, 5000);
server.Start();
Console.WriteLine("Server started");
string word = "";
savedObject saved = new savedObject();
while (true)
{
TcpClient connection = server.AcceptTcpClient();
Console.WriteLine("connection accepted");
ThreadPool.QueueUserWorkItem(saved.ProssecClient, connection);
}
}
}
}
class savedObject
{
string word;
public string AllWords(string sit)
{
word += sit + " ";
return word;
}
public string word2()
{
return word;
}
StreamReader[] sr1 = new StreamReader[100];
StreamWriter[] sw1 = new StreamWriter[100];
int m;
int a;
Dictionary<string, TcpClient> con = new Dictionary<string, TcpClient>();
Dictionary<string, StreamReader> conReader = new Dictionary<string, StreamReader>();
Dictionary<string, StreamWriter> conWriter = new Dictionary<string, StreamWriter>();
public void ProssecClient(object o)
{
string name = "";
string word2 = "";
TcpClient connection = o as TcpClient;
if (connection == null)
{
return;
}
StreamReader sr = new StreamReader(connection.GetStream());
sr1[a++] = new StreamReader(connection.GetStream());
sw1[m++] = new StreamWriter(connection.GetStream());
name = sr.ReadLine();
Console.WriteLine(name+" has joined the server");
con.Add(name, connection);
conReader.Add(name, sr1[a]);
conWriter.Add(name, sw1[m]);
try
{
while (true)
{
int i = 0;
word2 = AllWords(sr.ReadLine());
for (i = 0; i < 10; i++)
{
if (sw1[i] != null)
{
sw1[i].WriteLine(name+" : "+word2);
sw1[i].Flush();
}
}
}
}
catch { Console.WriteLine( name+ " client left"); }
}
}
</code>
This code works with the clients code. here is the clients side code. it is capable of sending and receiving messages from
other users:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpClient connection = new TcpClient("127.0.0.1", 5000);
StreamReader sr = new StreamReader(connection.GetStream());
StreamWriter sw = new StreamWriter(connection.GetStream());
string name2 = "";
while (true)
{
Console.WriteLine("Enter your name and press submit");
name2=Console.ReadLine();
if (name2 != "")
{
sw.WriteLine(name2);
break;
}
}
Console.WriteLine("Loop is over");
Thread t2 = new Thread(Reader);
t2.IsBackground = true;
t2.Start(connection);
while (true)
{
sw.WriteLine(Console.ReadLine());
sw.Flush();
}
}
public static void Reader(object o)
{
TcpClient con = o as TcpClient;
if (con == null)
return;
StreamReader sr = new StreamReader(con.GetStream());
while (true)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine();
}
Console.WriteLine(sr.ReadLine());
Console.WriteLine();
}
}
}
}
My goal here is to convert the client side code to a winform. so far i did that, but it gets stuck when i press the button:
<pre lang="msil">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
TcpClient connection;
StreamReader sr;
StreamWriter sw;
public Form1()
{
InitializeComponent();
try
{
connection = new TcpClient("127.0.0.1", 5000);
sr = new StreamReader(connection.GetStream());
sw = new StreamWriter(connection.GetStream());
}
catch
{
MessageBox.Show("Couldnt form a connection");
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
sw.WriteLine(richTextBox1.Text);
sw.Flush();
richTextBox2.Text = sr.ReadLine();
}
catch
{
MessageBox.Show("Cant Type Message");
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
i dont need a sample of a code that i dont understand, i just need to put make the winform send and accept data.