2

I've been watching the following pair of videos on how to create a TCP/IP connection : Server and Client What I need to do is create a game (flipcard) in which I send only 2 coordinates for a matrix. Nothing more. Or less.

(the code will follow)

From what I've observed, he closes and reopens the connection, everytime he send a message. It's working. But what do I modify in the code to keep the connection open and detect when a message arrived ?

All my google-searches brought me to these 2 methods:

  1. I have had luck using the socket object directly (rather than the TCP client). (But I do not want to use sockets)
  2. Also uses sockets

When I click to start the server

        private void bStartServer_Click(object sender, EventArgs e)
    {
        Thread tcpServerRunThread = new Thread(new ThreadStart(TCPServerRun));
        tcpServerRunThread.Start();
    }

Server Runs and waits for a connection

    private void TCPServerRun()
    {
        TcpListener tcpListener = new TcpListener(IPAddress.Any, 2016);
        tcpListener.Start();
        updateUI("Listening");
        while (true)
        {

            TcpClient client = tcpListener.AcceptTcpClient();
            updateUI("Connected");
            Thread tcpHandlerThread = new Thread(new ParameterizedThreadStart(tcpHandler));
            tcpHandlerThread.Start(client);
        }
    }

I handle my message

    private void tcpHandler(object client)
    {
        TcpListener tcpListener = new TcpListener(IPAddress.Any, 5004);
        TcpClient mClient = (TcpClient)client;
        NetworkStream stream =  mClient.GetStream();
        byte[] message = new byte[1024];
        stream.Read(message,0, message.Length);
        updateUI("New message = " + Encoding.ASCII.GetString(message));
        stream.Close();
        mClient.Close();
   } 

NOTE: updateUI is a function made by him to update a listbox item

Client side - almost like the server side `private void button1_Click(object sender, EventArgs e) { Thread mThread = new Thread(new ThreadStart(ConnectAsClient)); mThread.Start(); }

    private void ConnectAsClient()
    {
        //                 aici putem inlocui cu adresa de IP dorita             
        //client.Connect(IPAddress.Parse(textBox.Text), 5004);
        client = new TcpClient();
        //client.Connect(IPAddress.Parse("127.0.0.1"), 2016);     // for when I'm not connected to network
        client.Connect(IPAddress.Parse("my_ip_here"), 2016);   // for when I am connected to the network
        updateUI("connected");
        NetworkStream stream = client.GetStream();
        string s = "Hello world!";
        byte[] message = Encoding.ASCII.GetBytes(s);
        stream.Write(message, 0, message.Length);
        this.updateUI("Message sent!");
       stream.Close();
       // client.Close();
    }`

If I'm not wrong, everytime he connects again, he creates a new connection, and not open the old one, right? (It's my first time using TCP/IP, learning on my own, so I'd rather not use sockets, if possible)

-----------------------------------------------------------------------

Full link to my code (slighty modified from the videos provided at the beginning):

  1. TCPClient
  2. TCPServer
Community
  • 1
  • 1
SnuKies
  • 1,578
  • 1
  • 16
  • 37
  • you do not need to close it. but if I remember right you need to send a response message to the client before you can receive the next message – Steve Jan 09 '17 at 23:14

1 Answers1

0

You are reconnecting every time you send the message. Use two different methods for this; one for connecting to the server and then another one for actually sending the message. Like so:

TcpClient client;
private void ConnectAsClient()
{           
    //client.Connect(IPAddress.Parse(textBox.Text), 5004);
    client = new TcpClient();
    //client.Connect(IPAddress.Parse("127.0.0.1"), 2016);
    client.Connect(IPAddress.Parse("my_ip_here"), 2016);
    updateUI("connected");
}
void SendMessage()
{
    NetworkStream stream = client.GetStream();
    string s = "Hello world!";
    byte[] message = Encoding.ASCII.GetBytes(s);
    stream.Write(message, 0, message.Length);
    this.updateUI("Message sent!");
}