Didn't format my last question quite well last time, so here we go again :) I'm simply trying to make it possible to write text on one application which is printed on the second one and vice-versa. Is there some way to use 2 threads and 2 streams (port 1000 and 1100 for example?) to do this? My code so far:
App1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;
namespace Server
{
class Serveris
{
public static void Write()
{
while (true)
{
TcpClient clientSocket = new TcpClient("localhost", 1100);
string str = Console.ReadLine();
BinaryWriter writer = new BinaryWriter(clientSocket.GetStream());
writer.Write(str);
}
}
public static void Read()
{
while (true)
{
TcpClient clientSocket = new TcpClient("localhost", 1000);
BinaryReader reader = new BinaryReader(clientSocket.GetStream());
Console.WriteLine(reader.ReadString());
}
}
static void Main(string[] args)
{
TcpListener ServerSocket = new TcpListener(1000);
ServerSocket.Start();
Thread ctThread = new Thread(Write);
Thread ctThread2 = new Thread(Read);
ctThread.Start();
ctThread2.Start();
}
}
}
App2:
The same just 'reversed' ports, 1000 for Write(), 1100 for Read() and ServerSocket
If this cannot work, maybe there's some other way to code this communication without multiple threads? It basically has to simulate a chat between 2 clients (which are also servers so the standalone server wouldn't have to send messages between them)