Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
I from electrical engineering and i know very little about programming.
can you help me with developing an application for single server multiple client(few hundreds) communication(TCP/IP protocol), which has to be asynchronous and server capable of replying to the data requested by the client.
And the reply must be sent to a particular client that has sent the request.


I have gone through few articles in this blog but it couldn't solve my problem



hope you will reply me soon and do me this needful and help me in my project.

Thank You
Tarun kashyap
Posted

What have you tried till now? Try to apply what ever knowledge you have gained from reading the articles you mentioned. Code project has a lot of articles on the topics you have mentioned. Give it a try and ask specific questions, people would be more than willing to help when they see your effort.
 
Share this answer
 
Thank you very much for responding to my post.


I have written a code base on some articles and i am able to connect to the server from multiple clients. My client application is a windows form application and server is a console application. After connecting to few clients and try to send the data from the client to server there is a overlap error that is occurring(actually i have my desktop in my office so i am unable to explain about the exact error). And once i try to close any of the clients connected the server console is getting corrupted in a sense an error message is scrolling on the screen which is unreadable.
 
Share this answer
 
Server program (Console application)
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication
{
class Program
{
public Socket socketStream;
private BinaryWriter writer;
private BinaryReader rader;



static void Main(string[] args)
{


//IPEndPoint serverSocket = new IPEndPoint(IPAddress.Any, 1215);

Int32 port = 1215;
IPAddress localAddr = IPAddress.Parse("10.14.40.228");
TcpListener serverSocket = new TcpListener(localAddr, port);
TcpClient clientSocket = default(TcpClient);
int counter = 0;

serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");

counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
socketStream = new NetworkStream(clientSocket);
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}

clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}

//Class to handle each client request separatly
public class handleClinet
{
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;

while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

rCount = Convert.ToString(requestCount);
serverResponse = "Server to clinet(" + clNo + ") " + rCount;
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);

}



catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}

finally
{
clientSocket.Close();
serverSocket.Stop();
}


}
}
}
}



Client program(windows form)
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.Sockets;
namespace june2clnt
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream;
        public Form1()
        {
            InitializeComponent();
        }

         private void Form1_Load_1(object sender, EventArgs e)
     {
             //msg("Client Started");
            clientSocket.Connect("10.14.40.228", 1215);
            //label1.Text = "Client Socket Program - Server Connected ...";
     }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            if (serverStream.CanWrite)
            {
                int text = System.Convert.ToInt32(textBox1.Text);
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox1.Text);
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }
            else
            {
                clientSocket.Close();
                serverStream.Close();
                return;
            }
            if (serverStream.CanRead)
            {
                byte[] inStream = new byte[10025];
                serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                msg("Data from Server : " + returndata);
            }
            else
            {
                clientSocket.Close();
                serverStream.Close();
                return;
            }
        }
     public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

    }
}





error when sending data from client
System.ArgumentOutofrangeException: Lenght cannot be less than Zero.
Parameter name: length
at system.String.InternalSubstrigWithChecks<int32 int32="" boolean="" falwayscopy="">
at System.String.Substring<int32 int32="" length="">


Can you help me solve the error !!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900