in god sake help me iam sinking in deep blue sea
-
i told all of you before that i am designing new chat messenger but i really find some great troubles in my application i really want help from anybody knows how to deal with C# networking and its facilities in that type of things......... >>>> at the server side<<<<<<< here i created void method to handle the connection of clients upon specific port ... and i created new thread to handle the connection of multiusers who will connect to the server at any time and i replaced my thread in the constructor of my main program as .............................................. Thread readthread = new thread( new threadstart(runserver)); readthread.start(); ................... and that method in that delegate is like that ...........................................
public void runserver() { int counter = 1; try { server = new TcpListener(5151); server.Start(); //loop until recieveing connection request from the clients do { try { //inform at the server side that it is waiting for connection //from clients listBox1.Items.Add("Waiting for connection"); //intialize new socket connection upon client request connection = server.AcceptSocket(); //intialize new network stream connection socketstream = new NetworkStream(connection); //intialize new binary reader and binary writer to the network reader = new BinaryReader(socketstream); writer = new BinaryWriter(socketstream); //inform the user that there was new client connection writer.Write("server :" + " Welcome Client"); listBox1.Items.Add("Connection " + counter + " Recieved"); string message =""; do { message = reader.ReadString(); listBox1.Items.Add(message); }while(connection.Connected); } catch(Exception) { break; } listBox1.Items.Add("Client Terminated the connection"); connection.Close(); reader.Close(); writer.Close(); socketstream.Close(); ++counter; }while(true); } catch(System.Exception caught) { MessageBox.Show(caught.Message); } }
that worked with me when i started my server application and started my client application . It only accepted one client connection but when i tried to start new client window it didn't accept it and nothing was written on my second client window like the proper message in the previous code ((Writer.write("Server:Welco -
i told all of you before that i am designing new chat messenger but i really find some great troubles in my application i really want help from anybody knows how to deal with C# networking and its facilities in that type of things......... >>>> at the server side<<<<<<< here i created void method to handle the connection of clients upon specific port ... and i created new thread to handle the connection of multiusers who will connect to the server at any time and i replaced my thread in the constructor of my main program as .............................................. Thread readthread = new thread( new threadstart(runserver)); readthread.start(); ................... and that method in that delegate is like that ...........................................
public void runserver() { int counter = 1; try { server = new TcpListener(5151); server.Start(); //loop until recieveing connection request from the clients do { try { //inform at the server side that it is waiting for connection //from clients listBox1.Items.Add("Waiting for connection"); //intialize new socket connection upon client request connection = server.AcceptSocket(); //intialize new network stream connection socketstream = new NetworkStream(connection); //intialize new binary reader and binary writer to the network reader = new BinaryReader(socketstream); writer = new BinaryWriter(socketstream); //inform the user that there was new client connection writer.Write("server :" + " Welcome Client"); listBox1.Items.Add("Connection " + counter + " Recieved"); string message =""; do { message = reader.ReadString(); listBox1.Items.Add(message); }while(connection.Connected); } catch(Exception) { break; } listBox1.Items.Add("Client Terminated the connection"); connection.Close(); reader.Close(); writer.Close(); socketstream.Close(); ++counter; }while(true); } catch(System.Exception caught) { MessageBox.Show(caught.Message); } }
that worked with me when i started my server application and started my client application . It only accepted one client connection but when i tried to start new client window it didn't accept it and nothing was written on my second client window like the proper message in the previous code ((Writer.write("Server:WelcoThere is one problem in your code. All the code runs on one thread - so you just can handle one connection. To avoid this effect you should start one thread per client. At the point where you accept the socket you shoul openn a new thread and give it the socket. As a result the main thread isn't blocked anymore and can accept new connections. So it should be possible to handle multiple connections.
-
There is one problem in your code. All the code runs on one thread - so you just can handle one connection. To avoid this effect you should start one thread per client. At the point where you accept the socket you shoul openn a new thread and give it the socket. As a result the main thread isn't blocked anymore and can accept new connections. So it should be possible to handle multiple connections.
you mean my friend that i will add new thread after the code that accept the socket connection upon client request you mean my code will be like that ------------------------------------------------- public void runserver() { int counter = 1; try { server = new TcpListener(5151); server.Start(); //loop until recieveing connection request from the clients do { try { //inform at the server side that it is waiting for connection //from clients listBox1.Items.Add("Waiting for connection"); //intialize new socket connection upon client request connection = server.AcceptSocket(); ////////////////////////////////////////////////////// //here what did you told me... you mean like that Thread Anotherthread = new thread(new threadstart(runserver)); Anotherthread.start(); ///////////////////////////////////////////////////// //intialize new network stream connection socketstream = new NetworkStream(connection); //intialize new binary reader and binary writer to the network reader = new BinaryReader(socketstream); writer = new BinaryWriter(socketstream); //inform the user that there was new client connection writer.Write("server :" + " Welcome Client"); listBox1.Items.Add("Connection " + counter + " Recieved"); string message =""; do { message = reader.ReadString(); listBox1.Items.Add(message); }while(connection.Connected); } catch(Exception) { break; } listBox1.Items.Add("Client Terminated the connection"); connection.Close(); reader.Close(); writer.Close(); socketstream.Close(); ++counter; }while(true); } catch(System.Exception caught) { MessageBox.Show(caught.Message); } } Miss With The Best And Die Like The Rest
-
you mean my friend that i will add new thread after the code that accept the socket connection upon client request you mean my code will be like that ------------------------------------------------- public void runserver() { int counter = 1; try { server = new TcpListener(5151); server.Start(); //loop until recieveing connection request from the clients do { try { //inform at the server side that it is waiting for connection //from clients listBox1.Items.Add("Waiting for connection"); //intialize new socket connection upon client request connection = server.AcceptSocket(); ////////////////////////////////////////////////////// //here what did you told me... you mean like that Thread Anotherthread = new thread(new threadstart(runserver)); Anotherthread.start(); ///////////////////////////////////////////////////// //intialize new network stream connection socketstream = new NetworkStream(connection); //intialize new binary reader and binary writer to the network reader = new BinaryReader(socketstream); writer = new BinaryWriter(socketstream); //inform the user that there was new client connection writer.Write("server :" + " Welcome Client"); listBox1.Items.Add("Connection " + counter + " Recieved"); string message =""; do { message = reader.ReadString(); listBox1.Items.Add(message); }while(connection.Connected); } catch(Exception) { break; } listBox1.Items.Add("Client Terminated the connection"); connection.Close(); reader.Close(); writer.Close(); socketstream.Close(); ++counter; }while(true); } catch(System.Exception caught) { MessageBox.Show(caught.Message); } } Miss With The Best And Die Like The Rest
I wrote a program that I call GameServer that can be run either in server or client mode (i.e. you would run one instance to fill the role of "server" then you can have multiple "client" instances). The server accepts and identifies the individual clients and even has "chat" windows that are displayed on each client box that get updated from every connected client. If you would like to see this code I can zip it up and e-mail it to you - just let me know. You could pull the essential elements out of the code and use what you need. dpb Darryl Borden Principal IT Analyst dborden@eprod.com