I am having problems with my chat program in relaying information between the two clients. I have 1 program that when I launch it creates 1 client and 1 server.
2) user inputs port for server and launches server(s)
3) then the client GUI launches and the user enters a name and the port of the server they want to connect to.
I have it so my Client1 talks to Server2, but cannot figure out how to get the information passed to Server2 to relay to Client2's Chat Screen.
Server
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.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using ChatRoom;
using System.Runtime.Remoting.Channels.Http;
namespace RemoteServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonConfirm_Click(object sender, EventArgs e)
{
int channelNum;
string temp;
temp = textBoxPort.Text;
channelNum = Convert.ToInt32(temp);
HttpChannel ServerChannel = new HttpChannel(channelNum);
Console.WriteLine("httpChannel Set");
this.Close();
ChannelServices.RegisterChannel(ServerChannel, false);
Console.WriteLine("channel registered");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "ChatRoom",
WellKnownObjectMode.SingleCall);
Console.WriteLine("working");
Console.WriteLine("Port number is " + channelNum);
Console.ReadKey();
string tempText;
Class1 test = new Class1();
tempText = test.OutputWindow();
Console.WriteLine(tempText);
Console.ReadKey();
}
}
}
Client
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 ChatRoom;
using RemoteServer;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;
using System.Diagnostics;
using System.Timers;
namespace ClientServerGui
{
public partial class Form1 : Form
{
private Class1 server;
private Thread thread;
private string UserName;
public Form1()
{
InitializeComponent();
}
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void ConfigureServer(string port)
{
HttpClientChannel ClientChannel = new HttpClientChannel();
ChannelServices.RegisterChannel(ClientChannel, false);
RemotingConfiguration.RegisterWellKnownClientType(typeof(Class1), "http://localhost:"
+ textBoxPort.Text + "/ChatRoom");
MessageBox.Show("Server connection");
}
private void buttonSend_Click(object sender, EventArgs e)
{
string send = UserName + ": " + textBoxSend.Text;
while( textBoxSend.Text != "")
{
server.sendMessage(send);
server.test();
textBoxSend.Clear();
buttonSend.Focus();
}
textBoxChatWindowDisplay.Text += send;
textBoxChatWindowDisplay.Text = server.OutputWindow();
}
public void chatReloadTimer_Tick(object sender, EventArgs e)
{
textBoxChatWindowDisplay.Text = server.OutputWindow();
StartServer();
}
private void buttonConnect_Click(object sender, EventArgs e)
{
if (textBoxUserName.Text != "")
{
UserName = textBoxUserName.Text;
if (textBoxPort.Text == "")
{
textBoxPort.Text = "9000";
}
ConfigureServer(textBoxPort.Text);
server = new Class1();
server.addUserName(UserName);
thread = new Thread(new ThreadStart(StartServer));
thread.Start();
buttonSend.Focus();
System.Windows.Forms.Timer chatReloadTimer = new System.Windows.Forms.Timer();
chatReloadTimer.Interval = (100); chatReloadTimer.Tick += new EventHandler(chatReloadTimer_Tick);
chatReloadTimer.Start();
}
}
private void StartServer()
{
string currentChatWindow;
ArrayList usersArrayList = server.userList();
this.Invoke(new MethodInvoker(delegate() { listBoxUserList.Items.Clear(); }));
foreach (string x in usersArrayList)
{
this.Invoke(new MethodInvoker(delegate() { listBoxUserList.Items.Add(x); }));
}
currentChatWindow = server.OutputWindow();
this.Invoke(new MethodInvoker(delegate() { textBoxChatWindowDisplay.Text = currentChatWindow; }));
}
private void Form1_Load(object sender, EventArgs e)
{
Process start = new Process();
start.StartInfo.FileName = "RemoteServer";
start.Start();
MessageBox.Show("Continue?", "Continue", MessageBoxButtons.OKCancel);
buttonSend.Focus();
}
}
}
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace ChatRoom
{
public class Class1:MarshalByRefObject
{
private static ArrayList userNames = new ArrayList();
private string chatWindow;
public void sendMessage(string message)
{
Console.WriteLine(message);
if (message != null)
{
chatWindow += message + Environment.NewLine;
}
}
public void test()
{
Console.WriteLine("pizza");
}
public void addUserName(string UserName)
{
String test = UserName;
Console.WriteLine(test);
userNames.Add(test);
Console.WriteLine("0");
}
public void deleteUserName(string userName)
{
if (userName != null)
{
userNames.Remove(userName);
}
}
public ArrayList userList()
{
return userNames; }
public string OutputWindow()
{
return chatWindow;
}
}
}