Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:15:40+00:00 2026-06-05T12:15:40+00:00

i’m trying to connect from asp web application to tcp server. I did this

  • 0

i’m trying to connect from asp web application to tcp server. I did this using application in C#, and now i’d like to see if it will be working from web browser.

My code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace TcpWebClient

{
    public partial class _Default : System.Web.UI.Page
{

    private byte[] data = new byte[1024];
    IPEndPoint ipep = new IPEndPoint(
            IPAddress.Parse("192.168.1.20"), 12000);

    Socket server = new Socket(AddressFamily.InterNetwork,
                   SocketType.Stream, ProtocolType.Tcp);


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            server.Connect(ipep);
        }
        catch (SocketException)
        {              
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        NetworkStream ns = new NetworkStream(server);

        if (ns.CanWrite)
        {
            ns.Write(Encoding.ASCII.GetBytes("DI"), 0, 2);
            ns.Flush();
        }
        else
        {
        }
        TextBox1.Text = null;
        TextBox1.Text = Encoding.ASCII.GetString(data, 0, ns.Read(data, 0, data.Length));
    }

}
}

What is working?: I have connection to tcp server. But when i want to send and receive some data i’m getting errors:

IOException was unhandled by user code and something like this: Operation is not supported on connected sockets (don't know if my translation to english is ok).

What i’m doing wrong?

A first chance exception of type ‘System.IO.IOException’ occurred in System.dll

EDIT:

Something like this is working.. but every tick of timer i need to reconnect to the server.. is it possible to have one connection, then every tick of timer read/send data without reconnecting?

 protected void Timer1_Tick(object sender, EventArgs e)
    {

            server.Connect(ipep);
            NetworkStream stream = new NetworkStream(server);

            Byte[] data = System.Text.Encoding.ASCII.GetBytes("data");
            stream.Write(data, 0, data.Length);
            data = new Byte[256];
            Int32 bytes = stream.Read(data, 0, 8);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            TextBox1.Text = responseData;
            server.Close();

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:15:44+00:00Added an answer on June 5, 2026 at 12:15 pm

    You actually answered yourself: The connection is not openned in your second event handler (Button2_click), because you open on button1 click, and it, of coure ends its lifecycle togethere wit the page, an instance of which is created per request.

    You can save the opened connection in a static variable (if you dont mind the connection being shared application – wide for all the users), but i really am not sure you souldnt really just go with the code you posted in your edit.

    if you want the connectoin to be availibale application wide, define its variable as a static one (but make sure you initialize / connect before the first attempt to use), You can use the Lazy<T> helper classes provided in .net 4 to make sure the connection is initialized once, only on the first time it is needed.

    If you need the connection to be accessible in other classes as well, you can create a seaparate singleton class to wrap it (and initialize it there, in a same way)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.