Click here to Skip to main content
15,900,461 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do I bounce an old thread? Pin
J4amieC26-Jan-09 4:27
J4amieC26-Jan-09 4:27 
GeneralRe: How do I bounce an old thread? Pin
Mustafa Ismail Mustafa26-Jan-09 4:30
Mustafa Ismail Mustafa26-Jan-09 4:30 
QuestionProblem to if communicate with video capture device! Pin
Alex_xso26-Jan-09 3:27
Alex_xso26-Jan-09 3:27 
AnswerRe: Problem to if communicate with video capture device! Pin
Mustafa Ismail Mustafa26-Jan-09 3:57
Mustafa Ismail Mustafa26-Jan-09 3:57 
GeneralRe: Problem to if communicate with video capture device! Pin
Alex_xso26-Jan-09 5:02
Alex_xso26-Jan-09 5:02 
Questionclient/server problem [modified] Pin
staticv26-Jan-09 3:20
staticv26-Jan-09 3:20 
AnswerRe: client/server problem Pin
Mustafa Ismail Mustafa26-Jan-09 3:55
Mustafa Ismail Mustafa26-Jan-09 3:55 
GeneralRe: client/server problem Pin
staticv26-Jan-09 4:09
staticv26-Jan-09 4:09 
Well, whenever a user is connected I store its connection and username in a hashtable,
and when the server receives a message, I create a for loop and gets everyone's specific stream and then send the message to them. But at that time the clients are waiting for the input, rather than reading what the server has send. Only that client reads who have send the message to the server.

Do you want me to post the code?

Heres the code for the server

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.IO; 
using System.Threading; 
using System.Collections; 
using System.Text; 

public class ClientHandler 
{ 
   public TcpClient clientSocket; 

   public void RunClient() 
   { 
      // Create the stream classes 
      StreamReader readerStream = new StreamReader(clientSocket.GetStream()); 
      NetworkStream writerStream = clientSocket.GetStream(); 

      string returnData = readerStream.ReadLine(); 
      string userName = returnData; 

      Console.WriteLine("Welcome " + userName + " to the Server"); 

      while (true) 
      { 
         returnData = readerStream.ReadLine(); 

         if (returnData.IndexOf("QUIT") > −1) 
         { 
            Console.WriteLine("Bye Bye " + userName); 
            break; 
         } 

         Console.WriteLine(userName + ": " + returnData); 
         returnData += "\r\n"; 

         byte[] dataWrite = Encoding.ASCII.GetBytes(returnData) 
         writerStream.Write(dataWrite,0,dataWrite.Length); 
      } 

      clientSocket.Close(); 
   } 
} 

public class EchoServer 
{ 
   const int ECHO_PORT = 8080; 

   public static void Main(string [] arg) 
   { 
      try 
      { 
         // Bind the server to the local port 
         TcpListener clientListener = new TcpListener(ECHO_PORT); 

         // Start to listen 
         clientListener.Start(); 

         Console.WriteLine("Waiting for connections…"); 

         while (true) 
         { 
            // Accept the connection 
            TcpClient client = clientListener.AcceptTcpClient(); 

            ClientHandler cHandler = new ClientHandler(); 

            // Pass value to the ClientHandler object 
            cHandler.clientSocket = client; 

            // Create a new thread for the client 
            Thread clientThread = new Thread(new ThreadStart(cHandler.RunClient)); 
            clientThread.Start(); 
         } 

         clientListener.Stop(); 
      } 
      catch(Exception exp) 
      { 
         Console.WriteLine("Exception: " + exp); 
      } 
   } 
} 



and heres the code for client

using System; 
using System.Net; 
using System.IO; 
using System.Net.Sockets; 
using System.Text; 

public class EchoClient 
{ 
   const int ECHO_PORT = 8080; 

   public static void Main(string [] arg) 
   { 
      Console.Write("Your UserName:"); 
      string userName = Console.ReadLine(); 
      Console.WriteLine("-----Logged In----->"); 

      try 
      { 
         // Create a connection with the ChatServer 
         TcpClient eClient = new TcpClient("127.0.0.1", ECHO_PORT); 

         // Create the stream classes 
         StreamReader readerStream = new StreamReader(eClient.GetStream()); 
         NetworkStream writerStream = eClient.GetStream(); 

         string dataToSend; 

         dataToSend = userName; 
         dataToSend += "\r\n"; 

         // Send username to the server 
         byte[] data = Encoding.ASCII.GetBytes(dataToSend); 

         writerStream.Write(data,0,data.Length); 

         while(true) 
         { 
            Console.Write(userName + ":"); 

            // Read line from server 
            dataToSend = Console.ReadLine(); 
            dataToSend += "\r\n"; 

            data = Encoding.ASCII.GetBytes(dataToSend); 
            writerStream.Write(data, 0, data.Length); 

            // If QUIT is sent, then quit application 
            if (dataToSend.IndexOf("QUIT") > −1) 
               break; 

            string returnData; 

            // Receive response from server 
            returnData = readerStream.ReadLine(); 

            Console.WriteLine("Server: " + returnData); 
         } 

         // Close TcpClient 
         eClient.Close(); 
      } 
      catch(Exception exp) 
      { 
         Console.WriteLine("Exception: " + exp); 
      } 
   } 
} 


Top Web Hosting Providers[^]

Do, or do not. There is no 'try'.

GeneralRe: client/server problem Pin
Mustafa Ismail Mustafa26-Jan-09 4:28
Mustafa Ismail Mustafa26-Jan-09 4:28 
GeneralRe: client/server problem [modified] Pin
staticv26-Jan-09 4:37
staticv26-Jan-09 4:37 
GeneralRe: client/server problem Pin
Mustafa Ismail Mustafa26-Jan-09 4:40
Mustafa Ismail Mustafa26-Jan-09 4:40 
GeneralRe: client/server problem Pin
staticv26-Jan-09 4:42
staticv26-Jan-09 4:42 
GeneralRe: client/server problem Pin
Mustafa Ismail Mustafa26-Jan-09 4:44
Mustafa Ismail Mustafa26-Jan-09 4:44 
GeneralRe: client/server problem Pin
staticv26-Jan-09 4:46
staticv26-Jan-09 4:46 
QuestionIn a service application, where do I add the functions that it should perform? Pin
anahita8726-Jan-09 1:24
anahita8726-Jan-09 1:24 
AnswerRe: In a service application, where do I add the functions that it should perform? Pin
musefan26-Jan-09 2:57
musefan26-Jan-09 2:57 
AnswerRe: In a service application, where do I add the functions that it should perform? Pin
#realJSOP26-Jan-09 4:57
professional#realJSOP26-Jan-09 4:57 
Questionvalidation Pin
abu rakan26-Jan-09 0:44
abu rakan26-Jan-09 0:44 
AnswerRe: validation Pin
musefan26-Jan-09 0:54
musefan26-Jan-09 0:54 
GeneralRe: validation Pin
Manas Bhardwaj26-Jan-09 0:59
professionalManas Bhardwaj26-Jan-09 0:59 
GeneralRe: validation Pin
musefan26-Jan-09 1:05
musefan26-Jan-09 1:05 
GeneralRe: validation Pin
butchzn27-Jan-09 0:47
butchzn27-Jan-09 0:47 
AnswerRe: validation Pin
Christian Graus26-Jan-09 1:03
protectorChristian Graus26-Jan-09 1:03 
GeneralRe: validation Pin
abu rakan26-Jan-09 10:49
abu rakan26-Jan-09 10:49 
GeneralRe: validation Pin
Kaushal Arora26-Jan-09 19:41
Kaushal Arora26-Jan-09 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.