MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MainClass {

    public static void main(String[] args) throws IOException {

        int port = 2345;
        ServerSocket ss = new ServerSocket(port);
        while (true) {
            try {
                Socket s = ss.accept();

                String response = "Hello " + s.getInetAddress() + " on port " + s.getPort() + "\r\n";
                response += "This is " + s.getLocalAddress() + " on port " + s.getLocalPort() + "\r\n";
                OutputStream out = s.getOutputStream();
                out.write(response.getBytes("US-ASCII"));
                out.flush();
                s.close();
            } catch (IOException ex) {
            }
        }
    }
}