0

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)

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
GytisK
  • 169
  • 4
  • 21
  • Please see my answer here: http://stackoverflow.com/questions/26917176/sockets-in-c-how-can-i-asynchronously-read-and-write-data-through-a-networkstr/26917806#26917806 In that example, the server simply echoes whatever the client sends; for your purposes, I assume you can easily figure out how to modify the example so that the server side can send user-entered text as well. Note that you don't need two `Socket`s to have two-way communication. Just read and write using the same `Stream` for the connection's `Socket`. – Peter Duniho Nov 15 '14 at 23:17
  • I already have multi-client 'chat' where multiple clients can write to server at the same time, and the server echoes everything, that's easy to do, but completely not what I need here. Sure it could be modified, but so could my version, and that's what I am asking for here, the way to modify what I have to work as 1-1 chat – GytisK Nov 16 '14 at 15:40
  • Sorry, I don't understand what you're having difficulty with. The only difference is where the server gets the text to send: in the echo-server example, it gets the text from the data received, while in a chat server, it gets the text from the user. Other than that, they are exactly the same. The main things I see wrong with the code you posted is that you're using two sockets when one would do, and there's no evidence of any code that actually initiates a connection. But if you already have the echo example working, those issues should be trivial for you to fix. So what's the actual question? – Peter Duniho Nov 16 '14 at 19:59
  • The example I got working is multi-clients sending messages to server that prints them on his console. My question is how do I fix the code that I posted (NOT the multi-client one that I told you about) to work. AKA How do I get this to work with one socket, how do I pass it etc. – GytisK Nov 16 '14 at 22:39
  • I would advise you not to try. Start with the code that works and modify that. The code that works is much closer to the correct implementation than the code that doesn't. – Peter Duniho Nov 16 '14 at 23:41

0 Answers0