473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Sockets - Multiple clients

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

Sep 13 '07 #1
1 1960
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).

Rpublic 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:

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

RClient:
R-------------------------------
R-------------------------------
Rpublic Boolean Connect(String IPAddress)
R {
R try {
R ClientSocket = new Socket(AddressFamily.InterNetwork,
RSocketType.Stream, ProtocolType.Tcp);
R System.Net.IPAddress ip =
RSystem.Net.IPAddress.Parse(IPAddress);
R int iPortNo = Convert.ToInt16(SEND_PORT);
R System.Net.IPEndPoint ipEnd = new
RSystem.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}

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

R....

R}
RServer:
R-------------------------
R-------------------------
Rpublic Boolean beginListen()
R {
R try {

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

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

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

Rprivate 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,
RiRx, chars, 0);
R String szData = new String(chars);
R AppendRxText(szData);
R WaitForData(Worker);
R }
R}

RI don't quite know what I'm doing, I think I need to allow multiple
Rsocket connections but I've had no luck so far.
RCan anyone ched any light on this?
RCheers
RChris

Sep 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Cory Nelson | last post by:
I've created a new C++ sockets library - small, cross-platform, IPv4 and IPv6. If anyone would like to critique it, I'd appreciate it. Doesn't support the more exotic protocols - only TCP and UDP....
11
by: Bonj | last post by:
I've been following a socket programming tutorial to make a simple TCP communication program, seemingly without hitches, it appears to work fine. However the structure of it is to have a server...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
15
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include...
2
by: ZorpiedoMan | last post by:
I'm new to the world of sockets, and this question is not VB specific: If multiple clients access the same server on the same port, and the server is set up to do some async communication, does...
2
by: ZorpiedoMan | last post by:
I'm new to the world of sockets, and this question is not VB specific: If multiple clients access the same server on the same port, and the server is set up to do some async communication, does...
1
by: Macca | last post by:
Hi, I am writing a C# server app that listens for clients over a TCP/IP link. The clients are embedded devices with their software written in C. The clients establish connections and then...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
4
by: nyhetsgrupper | last post by:
I'm writing a server application connection to multiple clients using sockets. I want one socket for each client, and the comunication needs to be async. I could use the async sockets methods...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.