# C# Can't answer on PING

**URL:** <https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213>\
**Category:** Chat and IRC\
**Created:** [August 19, 2019, 7:07am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213 "2019-08-19T07:07:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 7:07am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/1 "2019-08-19T07:07:33Z")

</div>

Hello, I’m making bot on C# and I can’t figure out, why it still disconnects me after PING, here is my response

```
if (raw.Contains("PING"))
{
             SendIrcMessage("PONG :tmi.twitch.tv");
             BeginInvoke(new Action(() => textBox1.Text += $"{DateTime.Now.Hour}:{DateTime.Now.Minute} ping {raw}\r\n"));
             return;
}

public void SendIrcMessage(string message)
{
    outputStream.WriteLine(message);
    outputStream.Flush();
}

TcpClient tcpClient = new TcpClient("irc.chat.twitch.tv", 6667);
StreamWriter outputStream = new StreamWriter(tcpClient.GetStream());

```

I saw some topics with same problem so it seems that my response is correct but it doesn’t work for some reason.  
Yes, I do it with winforms for my own reasons.  
And I don’t want to use twitchlib (btw I can’t use it with wf) or any else lib.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [August 19, 2019, 10:21am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/2 "2019-08-19T10:21:32Z")

</div>

> [@PING twitch chat bot](https://discuss.dev.twitch.com/t/ping-twitch-chat-bot/22042/6):
>
> As per the docs. [https://dev.twitch.tv/docs/irc/guide/#connecting-to-twitch-irc](https://dev.twitch.tv/docs/irc/guide/#connecting-to-twitch-irc) About once every five minutes, the server will send you a PING :tmi.twitch.tv . To ensure that your connection to the server is not prematurely terminated, reply with PONG :tmi.twitch.tv . You should reply to a PING with a PONG But you should also send your OWN PINGs to which Twitch will PONG in case you missed a PING/PONG this’ll help keep your connection alive. Also shouldn’t your code be s.send(s, line.…

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 10:47am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/3 "2019-08-19T10:47:07Z")

</div>

Yes, I have pingsender

```
public void PingSender(StreamWriter outputStream)
{
    while (true)
    {
        outputStream.WriteLine("PING :tmi.twitch.tv");
        outputStream.Flush();
        Thread.Sleep(300000);
    }
}

```

And sorry, how did you put code in such window?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [August 19, 2019, 10:53am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/4 "2019-08-19T10:53:09Z")

</div>

Indent by four spaces or

 ![image](https://d2opxh93rbxzdn.cloudfront.net/original/2X/4/4fcc9d53b072a00ba35e77f71e1a23071dbbe54e.png)

Unless you are threading somewhere your code will be stuck in the while (true)/sleep loop and not process anything else no?

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 10:54am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/5 "2019-08-19T10:54:06Z")

</div>

`Task.Run(() => Functions.PingSender(outputStream));`

Yes, it’s a thread.

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 11:00am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/6 "2019-08-19T11:00:53Z")

</div>

I’m sorry if I gave not enough information. But it looks like I did everything right but respose to a PIGN disconnects me anyway.

---

<div class="post-metadata">

**Author:** ![Six](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/six/32/2334_2.png) [@Six](https://discuss.dev.twitch.com/u/Six)\
**Post date:** [August 19, 2019, 2:49pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/7 "2019-08-19T14:49:49Z")

</div>

I’m confused why you’re sending a `PING` message to twitch since those should only ever be received by a client, not sent. However your response message looks like it’s formatted correctly. I would check to make sure that `StreamWriter` properly appends `\r\n` since any other combination violates the IRC spec and might be seen as a garbage message. Are you able to successfully send other messages with your `SendIrcMessage(...)` function?

For reference, this is my writer which works as of last night. (Also in C#)

```cs
public void
Pong(string trailing = "")
{
    string message = "PONG";
    if (trailing.IsValid())
    {
        message += " :" + trailing;
    }

    Send(message);
}

public void
Send(string format, params object[] arguments)
{
    ExceptionUtil.ThrowIfInvalid(format, nameof(format));

    string message = !arguments.IsValid() ? format : string.Format(format, arguments);
    if (!CanSend(message))
    {
        return;
    }

    byte[] bytes = Encoding.UTF8.GetBytes(message + "\r\n");            
    stream.Write(bytes, 0, bytes.Length);
    stream.Flush();

    OnDataSent.Raise(this, new DataEventArgs(bytes, message));
}

```

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 3:06pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/9 "2019-08-19T15:06:22Z")

</div>

If I can send Ping to twitch or message to twitch chat that makes my `SendIrcMessage(...)` function correct? `stream.WriteLine()` adds \r\n, I guess or I wouldn’t be able to connect.

```
outputStream.WriteLine("CAP REQ :twitch.tv/tags");
outputStream.WriteLine("CAP REQ :twitch.tv/commands"); 
outputStream.WriteLine("CAP REQ :twitch.tv/membership"); 
outputStream.WriteLine("PASS " + password);
outputStream.WriteLine("NICK " + username);
outputStream.WriteLine("USER " + username + " 8 * :" + username);
foreach(string channel in Channels) outputStream.WriteLine("JOIN #" + channel);           
outputStream.Flush();

```

And that works fine. I can send Ping (btw I don’t know the reason too, but moderator said that I should, I mean, I can check connection with twitch through pings).  
Your `stream` is `StreamWriter`?

---

<div class="post-metadata">

**Author:** ![Six](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/six/32/2334_2.png) [@Six](https://discuss.dev.twitch.com/u/Six)\
**Post date:** [August 19, 2019, 3:25pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/10 "2019-08-19T15:25:11Z")

</div>

Hm, interesting. Since you are able to connect I guess it can’t be writer or your function.

My stream is just a normal `Stream` that is [instantiated](https://github.com/RokuHodo/TwitchNet/blob/experimental/Clients/Irc/IrcClient.cs#L270) using a `NetworkStream`.

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 3:30pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/11 "2019-08-19T15:30:21Z")

</div>

Is it possible, that my Brandmauer or anivirus blocks something? I just can’t understand the problem. And I don’t even know if twitch server received my pong.

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [August 19, 2019, 3:46pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/12 "2019-08-19T15:46:33Z")

</div>

You see the ping in your textBox1? How is `raw` populated (how do you read)?

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 3:54pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/13 "2019-08-19T15:54:02Z")

</div>

Yep, just a standart ping (all pongs before ping from twitch)  
 ![](https://d3ovvz0v2ajtpu.cloudfront.net/uploads/default/original/2X/a/af36ea54ec5c4a3d8dbc7100dbca4c17f1045370.png)

---

<div class="post-metadata">

**Author:** ![Six](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/six/32/2334_2.png) [@Six](https://discuss.dev.twitch.com/u/Six)\
**Post date:** [August 19, 2019, 3:58pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/14 "2019-08-19T15:58:09Z")

</div>

Are you actually sending the full line `:tmi.twitch.tv PONG tmi.twitch.tv :irc.twitch.tv` to Twitch, or only parts of that?

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 4:06pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/15 "2019-08-19T16:06:12Z")

</div>

I tried several variants `:tmi.twitch.tv PONG tmi.twitch.tv :irc.twitch.tv`, `PONG tmi.twitch.tv`, `PONG irc.twitch.tv`. Did I miss something?

---

<div class="post-metadata">

**Author:** ![Six](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/six/32/2334_2.png) [@Six](https://discuss.dev.twitch.com/u/Six)\
**Post date:** [August 19, 2019, 4:11pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/16 "2019-08-19T16:11:36Z")

</div>

The first message include what’s called a “prefix”. It’s not prohibited by the Spec, but per the IRC spec:

> Clients should not use prefix when sending a message from  
> themselves; if they use a prefix, the only valid prefix is the  
> registered nickname associated with the client. If the source  
> identified by the prefix cannot be found from the server’s internal  
> database, or if the source is registered from a different link than  
> from which the message arrived, the server must ignore the message  
> silently.

So essentially if you do send a prefix, it must only ever be `:your_nick_here COMMAND :message\r\n`

To be safe, I would just omit the prefix entirely. Your `PONG` response would then just look like:

```auto
PONG :tmi.twitch.tv\r\n

```

In your case, the `\r\n` is covered by your `StreamWriter.Writeline(...)`

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [August 19, 2019, 4:18pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/17 "2019-08-19T16:18:21Z")

</div>

Just `PONG` works with TMI. You don’t need anything special around it.

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 19, 2019, 4:31pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/18 "2019-08-19T16:31:19Z")

</div>

Nope, neither `PONG :tmi.twitch.tv\r\n`, not `PONG` works for me. Btw I have another bot that works fine but on Node.js with tmi.js so everything should be alrigth, but still disconnects the c# one.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [August 19, 2019, 5:07pm UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/19 "2019-08-19T17:07:27Z")

</div>

> [@Trimetral](#):
>
> And that works fine. I can send Ping (btw I don’t know the reason too, but moderator said that I should, I mean, I can check connection with twitch through pings).

Just because I’m a moderator doesn’t make me correct.

I have found that at times Twitch can leave it’s ping pongs a little spaced out. So I send my own ping’s in order to help keep the connection alive.

Off hand I forget what the IRC spec says on that matter. But it seems to work well for my code and usecase.

---

<div class="post-metadata">

**Author:** ![Trimetral](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@Trimetral](https://discuss.dev.twitch.com/u/Trimetral)\
**Post date:** [August 20, 2019, 8:28am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/20 "2019-08-20T08:28:57Z")

</div>

So if you say that sendin your Pings to server is enough and that means my Pings didn’t count?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [August 20, 2019, 10:37am UTC](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213/21 "2019-08-20T10:37:10Z")

</div>

I am wondering if you are not reading chat and then you are being disconnected because the “send message to client buffer” becomes too large and Twitch disconnects you.

[Next page](https://discuss.dev.twitch.com/t/c-cant-answer-on-ping/22213.md?page=2)
