Need Help in multithread C# socket programming!!!
-
Hi everyone! when i run the programs of server and client, they both run and also show the acknowledgement like >>Server Started! >>Client No:1 started! >>From client-1Message from Client >>Server to client<1>1 and so on for client 2, 3 ... but i want communication like if i type some text and then it display that text!!! I am using visual 2010... multithread Server program is
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
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());
}
}
}
}
}the Clie
-
Hi everyone! when i run the programs of server and client, they both run and also show the acknowledgement like >>Server Started! >>Client No:1 started! >>From client-1Message from Client >>Server to client<1>1 and so on for client 2, 3 ... but i want communication like if i type some text and then it display that text!!! I am using visual 2010... multithread Server program is
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
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());
}
}
}
}
}the Clie
yum 2010 wrote:
but i want communication like if i type some text and then it display that text!!!
You mean when you press "A", that the other side immediately receives that character and displays it?
yum 2010 wrote:
Plzzzzz guide me
Guiding someone trough a forest is a level beyond pointing the right direction :)
I are Troll :suss:
-
yum 2010 wrote:
but i want communication like if i type some text and then it display that text!!!
You mean when you press "A", that the other side immediately receives that character and displays it?
yum 2010 wrote:
Plzzzzz guide me
Guiding someone trough a forest is a level beyond pointing the right direction :)
I are Troll :suss:
-
Eddy Vluggen wrote:
You mean when you press "A", that the other side immediately receives that character and displays it?
yeah!!
-
Then send a message whenever a key is pressed :) You're already sending messages, I guess you can adapt it to send a message on every keystroke.
I are Troll :suss:
-
Eddy Vluggen wrote:
I guess you can adapt it to send a message on every keystroke.
hey can yu guide me that how can i do this?? :(
-
yum 2010 wrote:
hey can yu guide me that how can i do this?? :(
I can't. The code that you posted is a basic client/server example. I suggest you search for multiple chat-applications and study them.
I are Troll :suss: