February 3rd, 2010 02:44 AM
#1
multiclient
hi,
i have a chat app and it runs.
but i notice that i made a single client only to send message to the host
how can i make my chat a multiclient receiver.
is it the host or the client that i have to change.
please advice
thanks
February 3rd, 2010 03:32 AM
#2
Re: multiclient
Depends, possibly both. How should we know what you have to change when we don't know anything about your application?
Well, I suppose you want your chat application to allow clients to talk to all users or to a particular user. Your server should be capable of accepting connections from multiple clients. The clients should be notified when someone logs in or out and display a list of available chat users. The server should be able to redirect the received messages to all users or only to one (or a group).
But first thing is that you define what you want for your application, and then you can focus on how to do it.
February 3rd, 2010 04:02 AM
#3
Re: multiclient
my script is very simple,
Code:
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;
namespace printerManager.V._1._0
{
public partial class SocketServerForm : Form
{
public AsyncCallback pfnWorkerCallBack;
public Socket m_socListener;
public Socket m_socWorker;
public SocketServerForm()
{
InitializeComponent();
}
private void cmdListen_Click_1(object sender, EventArgs e)
{
try
{
//create the listening socket...
m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8221);
//bind to local IP Address...
m_socListener.Bind(ipLocal);
//start listening...
m_socListener.Listen(4);
// create the call back for any client connections...
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
cmdListen.Enabled = false;
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
m_socWorker = m_socListener.EndAccept(asyn);
WaitForData(m_socWorker);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public class CSocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1];
}
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
delegate void FixCallBack(IAsyncResult asyn);
public void OnDataReceived(IAsyncResult asyn)
{
try
{
if (this.InvokeRequired)
{
FixCallBack dd = new FixCallBack(OnDataReceived);
this.Invoke(dd, new object[] { asyn });
}
else
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
//end receive...
int iRx = 0;
string mess;
iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
mess = txtDataRx.Text + szData;
txtDataRx.Text = mess;
WaitForData(m_socWorker);
}
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
Object objData = txtDataTx.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
m_socWorker.Send(byData);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void txtDataRx_TextChanged(object sender, EventArgs e)
{
}
}
}
its the server script
what part of the script should i change?
tia
Last edited by homer.favenir; February 3rd, 2010 at 08:30 PM .
February 3rd, 2010 06:57 AM
#4
Re: multiclient
You're not paying attention. How can we figure what parts of the scripts should be modified when you didn't explain how you expect your application to work?
February 3rd, 2010 07:24 AM
#5
Re: multiclient
i would like the server accepts multiclient, as of now it is accepting single client only...
thanks
February 3rd, 2010 08:37 AM
#6
Re: multiclient
Originally Posted by
homer.favenir
my script is very simple,
its the server script
what part of the script should i change?
tia
It would be better if you edit that post and use the [code] tag instead of [quote].
February 3rd, 2010 08:33 PM
#7
Re: multiclient
hi,
i already changed it
tia
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks