Giter Site home page Giter Site logo

tcpsock's Introduction

Aşağıda, Node.js kullanarak bir TCP sunucusu oluşturmanız ve bu sunucu üzerinden gelen verileri bir HTML sayfasında görüntülemeniz için kullanabileceğiniz örnek bir kod bloğu bulunmaktadır. İlk olarak, Node.js'in kurulu olduğundan emin olun.

// Gerekli modülleri içe aktarın
const net = require("net");
const http = require("http");
const fs = require("fs");

// TCP sunucusunu oluşturun
const tcpServer = net.createServer((socket) => {
  console.log("Yeni bir istemci bağlandı.");

  // İstemciden gelen verileri dinleyin ve HTTP sunucusuna iletilmek üzere bir HTML sayfasına yazın
  socket.on("data", (data) => {
    const htmlContent = `
      <!DOCTYPE html>
      <html>
      <head>
        <title>TCP Veri Gösterimi</title>
      </head>
      <body>
        <h1>Alınan Veri:</h1>
        <p>${data}</p>
      </body>
      </html>
    `;

    // HTML içeriğini bir dosyaya kaydedin
    fs.writeFile("tcp_data.html", htmlContent, (err) => {
      if (err) {
        console.error("Dosya kaydedilirken hata oluştu:", err);
      }
    });
  });
});

// HTTP sunucusunu oluşturun
const httpServer = http.createServer((req, res) => {
  // Kaydedilen HTML dosyasını okuyun ve istemciye yanıt olarak gönderin
  fs.readFile("tcp_data.html", (err, data) => {
    if (err) {
      res.writeHead(500, { "Content-Type": "text/plain" });
      res.end("Hata: HTML dosyası bulunamadı.");
    } else {
      res.writeHead(200, { "Content-Type": "text/html" });
      res.write(data);
      res.end();
    }
  });
});

// TCP sunucusunu belirtilen portta dinlemeye başlayın
const tcpPort = 3000;
tcpServer.listen(tcpPort, () => {
  console.log(`TCP Sunucusu ${tcpPort} portunda dinleniyor.`);
});

// HTTP sunucusunu belirtilen portta dinlemeye başlayın
const httpPort = 8080;
httpServer.listen(httpPort, () => {
  console.log(`HTTP Sunucusu ${httpPort} portunda dinleniyor.`);
});

Bu kod, TCP sunucusunu oluşturur, istemciden gelen verileri bir HTML sayfasına kaydeder ve bir HTTP sunucusu kullanarak bu HTML sayfasını istemcilere sunar. TCP sunucusu 3000 portunda dinlerken, HTTP sunucusu 8080 portunda dinler. Veriler, HTML sayfasının içinde görüntülenir.

tcpsock's People

Contributors

kadirefendi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.