nala58008
nala58008
Reputation Top 1%
nala58008
4 Snippets  (188th place)
Published
1 Channel
Created
6 Channels
Following
569 points  (60th place)
Reputation
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer

Recent Snippets See all snippets by nala58008

public by nala58008  239  1  3  1

TcpServerExample

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devdungeon.tcpexamples;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author dtron
 */
public class TcpServerExample {

    public static void main(String[] args) throws Exception {
        ServerSocket listener = new ServerSocket(6789);

        while (true) {
            // Get the client data
            Socket connectionSocket = listener.accept();
            
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            String clientRequest = inFromClient.readLine();

            // Process client data and echo back respons as HTTP 200 ok
            System.out.println("Received from client: " + clientRequest);
            outToClient.write("HTTP/1.1 200 OK\n\n".getBytes());
            outToClient.writeBytes(clientRequest);
        }
    }
}
;

public by nala58008  213  1  3  0

TcpClientExample

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devdungeon.tcpexamples;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 *
 * @author dtron
 */
public class TcpClientExample {

    public static void main(String[] args) throws Exception {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.println("Enter some text to send to the server: ");
        sentence = inFromUser.readLine();

        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();

        System.out.println("FROM SERVER: " + modifiedSentence);
        clientSocket.close();

    }
}
;

public by nala58008  208  1  3  0

ThreadedTcpServerWorker

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devdungeon.tcpexamples.threadedserver;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dtron
 */
public class ThreadedTcpServerWorker extends Thread {

    protected Socket connectionSocket = null;

    public ThreadedTcpServerWorker(Socket connectionSocket) {
        this.connectionSocket = connectionSocket;
    }

    public void run() {
        System.out.println("Hello from a thread!");

        try {
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(this.connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(this.connectionSocket.getOutputStream());
            String clientRequest = null;

            clientRequest = inFromClient.readLine();
            System.out.println("Received from client: " + clientRequest);
            outToClient.write("HTTP/1.1 200 OK\n\n".getBytes());
            outToClient.write(clientRequest.getBytes());
            outToClient.close();
            inFromClient.close();
                    
        } catch (IOException ex) {
            Logger.getLogger(ThreadedTcpServerWorker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
;

public by nala58008  169  1  2  0

ThreadedTcpServer

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devdungeon.tcpexamples.threadedserver;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dtron
 */
public class ThreadedTcpServer {

    public static void main(String[] args) {
        // Set up server listener
        ServerSocket listener = null;
        try {
            listener = new ServerSocket(6789);
        } catch (IOException ex) {
            Logger.getLogger(ThreadedTcpServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Handle incoming connections
        while (true) {
            try {
                Socket connectionSocket = listener.accept();
                (new ThreadedTcpServerWorker(connectionSocket)).start();
            } catch (IOException ex) {
                Logger.getLogger(ThreadedTcpServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}nippet.						
;