Sending and Receiving Messages over TCP

Sending and Receiving at the same time

Page 1 of 1

2 Replies - 34739 Views - Last Post: 25 February 2009 - 09:55 AM Rate Topic: -----

#1 Todilo   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 86
  • Joined: 13-November 07

Sending and Receiving Messages over TCP

Posted 24 February 2009 - 02:01 PM

Im trying to create a simple tcp server/client application. Ultimately the server connects to x number of clients and when a client double-clicks on the application in the tray it sends a "Go" to the server, which says "Go" to all clients connected and then the clients play a sound file.

But now to my question, how do I go about reading and writing at the same time? Should I have 2 seperate threads, one which uses the stream from the server(talking about the client part now, because it's the only one in a problem) and checks for incomming data, and one which waits for user input and sends (if OKAYED by the stream)? What alternatives are there?

The server parts handles each client in a seperate thread and one is in a while loop pending for new connections. The client only have one thread, main thread.
Using networkstream.read now, should I use streamwriter,reader?


My code so far is, working to the extend it receives messages from server but only after writing something after the server send the message:
http://todilo.pastebin.com/m1c7aa220

Thank you for reading!

Is This A Good Question/Topic? 0
  • +

Replies To: Sending and Receiving Messages over TCP

#2 manzoor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Sending and Receiving Messages over TCP

Posted 25 February 2009 - 03:59 AM

Yup, two threads will work fine, though you can use asynchronous sockets which will cause the execution of the code to be split into two threads. When an asynchronous function is invoked, the processing of the network functionality breaks off and runs in another thread, while the application continues to run on the original thread. But know that each I/O request can lead to an extra overhead for the operating system.

You should use NetworkStream for writing, while for reading consider using a StreamReader but with using the NetworkStream as its underlying stream (you may say obviously :)).

I hope it helped you out...

This post has been edited by manzoor: 25 February 2009 - 04:00 AM

Was This Post Helpful? 1
  • +
  • -

#3 Todilo   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 86
  • Joined: 13-November 07

Re: Sending and Receiving Messages over TCP

Posted 25 February 2009 - 09:55 AM

Ok now I put one thread for reading and one for writing in the client(and main thread sleeps in a while loop). Basicly all it can do now it send a message, and then receives a message(streamreader.read() , would'nt let me read a whole line) and then the writer is broken and nothing more can be send!

Help, I am getting crazy working on this. But thanks so far though.

Client code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace DotaHorn
{
	class Program
	{
		static NetworkStream clientStream;
		static bool write = false;
		static void Main(string[] args)
		{
			Console.WriteLine("Starting up");
			TcpClient client = new TcpClient();

			IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5898);

			client.Connect(serverEndPoint);
			clientStream = client.GetStream();
			Thread readThread = new Thread(readFromServer);
			Thread writeThread = new Thread(writeGoToServer);
			readThread.Start();
			writeThread.Start();
			while (true) { Thread.Sleep(1000); }
		}

		private static void writeGoToServer()
		{
			ASCIIEncoding encoder = new ASCIIEncoding();
			byte[] buffer;
			while (true)
			{
				Console.WriteLine("Check if can write");
				
					buffer = encoder.GetBytes("Go");
					clientStream.Write(buffer, 0, buffer.Length);
					clientStream.Flush();
					Console.WriteLine("Wrote Go");
					Thread.Sleep(4000);
				

			}
		}

		private static void readFromServer()
		{
			StreamReader sr = new StreamReader(clientStream);
			while (true)
			{
				Console.WriteLine("Check if data is available");
				if (clientStream.DataAvailable)
				{
					Console.WriteLine("Data was available");
					while (sr.Peek() != -1)
					{
						Console.WriteLine("Starts to read:");
						Console.WriteLine(sr.Read());
						Console.WriteLine("Done!");
					}
				}
				Thread.Sleep(1000);
			}
		}

	}
}


This post has been edited by Todilo: 25 February 2009 - 10:03 AM

Was This Post Helpful? 1

Page 1 of 1