Created
          December 5, 2017 03:28 
        
      - 
      
- 
        Save droidMakk/18448ce79428319ab0c7c51ed5f78d7e to your computer and use it in GitHub Desktop. 
    Simple NodeJS TCP Client & Server
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | //----------- Server Code------------------------ | |
| var net = require('net'); | |
| var srv = net.createServer(function(socket){ | |
| socket.on('data',function(data){ | |
| console.log(data.toString()); | |
| }) | |
| socket.end('Love, from server') | |
| }).on('error',function(err){ | |
| throw err; | |
| }) | |
| srv.listen({ | |
| host: 'localhost', | |
| port: 80 | |
| },function(){ | |
| console.log('Listening at 80') | |
| }) | |
| //----------------Client Code---------------------- | |
| var net = require('net') | |
| var client = net.createConnection({ | |
| port: 80, | |
| host: "127.0.0.1" | |
| },function(){ | |
| console.log("Connected to server") | |
| client.write("off"); | |
| }) | |
| client.on('data',function(){ | |
| console.log(data.toString()) | |
| }) | |
| client.on('end',function(){ | |
| console.log('Ended connection') | |
| }) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment