Socket Management Principles - Client Server

I am ashamed to even ask this question, but after a tedious google search (starting with MSDN ...), I decided to post it:

Learning how to program on a client server has just begun (using C #) and trying to write my first code using tcpClient. I write both server and client side. here is my question: Constantly the client sends one line to the server, and then the server sends the String back to the client and so on. Can the server send 2 lines in a row? should he wait for the client to respond? is a client server principle

Once again, sorry for the lousy question. Thanks...

<> I will try to publish part of my code (long ...). I tried to cut out the eloquent parts, so I hope the code makes some sense ... (I designated the main problam with // *********)

public class MServer2 { Dictionary<String, String> nameAndPass = new Dictionary<String, String>(); Dictionary<String, List<String> > nameAndMail = new Dictionary<String, List<String>>(); public static void Main() { new MServer2(); } public MServer2() { TcpListener server = new TcpListener(8500); try { server.Start(); Console.WriteLine("started " + server); while (true) { TcpClient client = server.AcceptTcpClient(); Console.WriteLine("connection accepted " + client); new Server1(client, nameAndPass, nameAndMail); } } catch (Exception e) { Console.WriteLine("exception" + e); } finally { server.Stop(); } } class Server1 { TcpClient client; NetworkStream netStream; Dictionary<String, String> nameAndPass1 = new Dictionary<String, String>(); Dictionary<String, List<String>> nameAndMail1 = new Dictionary<String, List<String>>(); internal Server1(TcpClient client, Dictionary<String, String> nameandPassFromFile, Dictionary<String, List<String> > nameAndMailsFromFile) { nameAndPass1 = nameandPassFromFile; nameAndMail1 = nameAndMailsFromFile; this.client = client; Thread thr = new Thread(new ThreadStart(Run)); thr.Start(); } public void Run() { try { netStream = client.GetStream(); StreamReader reader = new StreamReader(netStream); StreamWriter writer = new StreamWriter(netStream); writer.AutoFlush = true; Console.WriteLine("beginning to receive loop"); writer.WriteLine("Choose your user name."); strFromClient = reader.ReadLine(); userName = strFromClient; writer.WriteLine("Choose your user password."); strFromClient = reader.ReadLine(); password = strFromClient; writer.WriteLine("Do you want to see the list of email addresses? (y/n)"); strFromClient = reader.ReadLine(); //*********************************************************************************** //HERE MY PROBLAM: //HERE THE CLIENT WILL GET A STRING SHOWING HIS EMAILS, AND I WANT HIM TO GET ANOTHER STRING ASKING "Do you want to add an email address? (y/n)", BUT IT LOOKS LIKE THE SERVER "WAITS" FOR A RESPONSE FROM THE CLIENT TO SHOW THE NEXT STRING... if (strFromClient == "y") { String tmpStr = null; List<String> tmp = nameAndMail1[userName]; for(int i=0; i<nameAndMail1[userName].Count; i++) { tmpStr += tmp[i] + " "; } writer.WriteLine(tmpStr); } writer.WriteLine("Do you want to add an email address? (y/n)"); strFromClient = reader.ReadLine(); } } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } 

EDIT VOL 2

OK! After 2 hours of suffering, I think I found problam thanks to Phil Frost (genius!) --- problam, probably in the client ... (Im souch a-hole!).

The server sends 2 lines in a row, but my stupid client-side implementation does not display a message (which is retrieved from the server) that does not follow the message sent by the client ...

So again I need your help. Here is a view of how I created the client form: enter image description here

The lack of experience led me to connect to the server when I clicked "connect to server", and only when I clicked "Send message" is a message from the server displayed. The problem is that 2 (or more) messages from the server are returned without sending a message from the client to the server - the client does not know that a new message has been received! Where, in my opinion, can I get messages from the server? ("where" means under which function, for example, right now this is happening in the sendMessage_click function).

Thanks again for the help!

+4
source share
3 answers

This question is neither lousy nor trivial. You touch on an important point in the development of the protocol.

AFAIK, there are 2 ways to discard this particular cat:

  • Dialogue (your path) ... The request is followed by the response, followed by the next request, etc. This has the great advantage of being easy, but the big disadvantage of only one command per connection that is being processed at any given time.
  • "Async" (both parties can send messages without waiting for a response). This has implementation complexity, you need some kind of request identifier so that it can know which answer belongs to which request: Imagine a client sending two requests, the first takes longer to process than the second: the answers will be in a different order than inquiries.

Choosing between these options is not always easy, but the trend is toward an asynchronous model.

Additional difficulties in the asynchronous taste include a mechanism to ensure that only one message is sent at a time (message interlacing is likely to lead to an uncontrolled stream) and timeout considerations.

+6
source

Yes, the server can send two lines in a row. But probably not if your server is locked on read() . Perhaps if you included additional information about your program, a more specific answer might be provided.

+2
source

In TCP, client-server and server-client channels are two independent flows. Any party can send at any time.

+1
source

All Articles