Sockets - Multiple clients

R

Riddle

Hi, I'm relatively new to socket programming and I'm having trouble
understanding them!
I've (using help from tinternet and a useful tutorial) made a client
and server that are able to talk to one another. The problem is when I
start two clients, and send messages to the server from each. Only the
one that was connected first sends the message.
Some code:

Client:
-------------------------------
-------------------------------
public Boolean Connect(String IPAddress)
{
try
{
ClientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress ip =
System.Net.IPAddress.Parse(IPAddress);
int iPortNo = Convert.ToInt16(SEND_PORT);
System.Net.IPEndPoint ipEnd = new
System.Net.IPEndPoint(ip.Address, iPortNo);
ClientSocket.Connect(ipEnd);
WaitForData();
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
return false;
}
}

public Boolean SendData(String Data)
{
try
{
Object objData = Data;
byte[] byData =
Encoding.ASCII.GetBytes(objData.ToString());
ClientSocket.Send(byData);
}

.....

}


Server:
-------------------------
-------------------------
public Boolean beginListen()
{
try
{

Listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any,
LISTEN_PORT);
tempEndPoint = (EndPoint)ipLocal;
Listener.Bind(ipLocal);
Listener.Listen(4);
Listener.BeginAccept(new
AsyncCallback(OnClientConnect), null);
}
.....
}

public void OnClientConnect(IAsyncResult asyn)
{
try
{
Worker = Listener.EndAccept(asyn);
WaitForData(Worker);
}
....
}

private void WaitForData(Socket soc)
{
try
{
if (WorkerCallBack == null)
{
WorkerCallBack = new
AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
// now start to listen for any data
soc.BeginReceiveFrom(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length, SocketFlags.None, ref tempEndPoint,
WorkerCallBack, theSocPkt);
}
}

private void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId =
(CSocketPacket)asyn.AsyncState;
int iRx = 0;
iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx,
chars, 0);
String szData = new String(chars);
AppendRxText(szData);
WaitForData(Worker);
}
}



I don't quite know what I'm doing, I think I need to allow multiple
socket connections but I've had no luck so far.
Can anyone ched any light on this?
Cheers
Chris
 
V

Vadym Stetsiak

Hello, Riddle!

IMO the problem with single client is in OnClientConnect method.
There is a call to EndAccept, but no call to schedule connection accept
(another BeginAccept is missing).

R> public void OnClientConnect(IAsyncResult asyn)
R> {
R> try {
R> Worker = Listener.EndAccept(asyn);
R> WaitForData(Worker);

Listener.BeginAccept(new
AsyncCallback(OnClientConnect), null);

R>
R> }
R> ...
R> }
R>
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Thu, 13 Sep 2007 01:40:01 -0700:

R> Hi, I'm relatively new to socket programming and I'm having trouble
R> understanding them!
R> I've (using help from tinternet and a useful tutorial) made a client
R> and server that are able to talk to one another. The problem is when
R> I start two clients, and send messages to the server from each. Only
R> the one that was connected first sends the message.
R> Some code:

R> Client:
R> -------------------------------
R> -------------------------------
R> public Boolean Connect(String IPAddress)
R> {
R> try {
R> ClientSocket = new Socket(AddressFamily.InterNetwork,
R> SocketType.Stream, ProtocolType.Tcp);
R> System.Net.IPAddress ip =
R> System.Net.IPAddress.Parse(IPAddress);
R> int iPortNo = Convert.ToInt16(SEND_PORT);
R> System.Net.IPEndPoint ipEnd = new
R> System.Net.IPEndPoint(ip.Address, iPortNo);
R> ClientSocket.Connect(ipEnd);
R> WaitForData();
R> }
R> catch (SocketException se)
R> {
R> Console.WriteLine(se.Message);
R> return false;
R> }
R> }

R> public Boolean SendData(String Data)
R> {
R> try {
R> Object objData = Data;
R> byte[] byData =
R> Encoding.ASCII.GetBytes(objData.ToString());
R> ClientSocket.Send(byData);
R> }

R> ....

R> }


R> Server:
R> -------------------------
R> -------------------------
R> public Boolean beginListen()
R> {
R> try {

R> Listener = new Socket(AddressFamily.InterNetwork,
R> SocketType.Stream, ProtocolType.Tcp);
R> IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any,
R> LISTEN_PORT);
R> tempEndPoint = (EndPoint)ipLocal;
R> Listener.Bind(ipLocal);
R> Listener.Listen(4);
R> Listener.BeginAccept(new
R> AsyncCallback(OnClientConnect), null);
R> }
R> ....
R> }

R> public void OnClientConnect(IAsyncResult asyn)
R> {
R> try {
R> Worker = Listener.EndAccept(asyn);
R> WaitForData(Worker);
R> }
R> ...
R> }

R> private void WaitForData(Socket soc)
R> {
R> try {
R> if (WorkerCallBack == null)
R> {
R> WorkerCallBack = new
R> AsyncCallback(OnDataReceived);
R> }
R> CSocketPacket theSocPkt = new CSocketPacket();
R> theSocPkt.thisSocket = soc;
R> // now start to listen for any data
R> soc.BeginReceiveFrom(theSocPkt.dataBuffer, 0,
R> theSocPkt.dataBuffer.Length, SocketFlags.None, ref tempEndPoint,
R> WorkerCallBack, theSocPkt);
R> }
R> }

R> private void OnDataReceived(IAsyncResult asyn)
R> {
R> try {
R> CSocketPacket theSockId =
R> (CSocketPacket)asyn.AsyncState;
R> int iRx = 0;
R> iRx = theSockId.thisSocket.EndReceive(asyn);
R> char[] chars = new char[iRx + 1];
R> Decoder d = Encoding.UTF8.GetDecoder();
R> int charLen = d.GetChars(theSockId.dataBuffer, 0,
R> iRx, chars, 0);
R> String szData = new String(chars);
R> AppendRxText(szData);
R> WaitForData(Worker);
R> }
R> }



R> I don't quite know what I'm doing, I think I need to allow multiple
R> socket connections but I've had no luck so far.
R> Can anyone ched any light on this?
R> Cheers
R> Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top