The Caps Lock Java Socket Server

Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior.

Features:

  • A client should enter a string and the server would answer the same string, with each symbol in up case, when possible.
  • Default port at 8080.
  • One client at time.
  • No multi threading. I said its a simple server.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server {
	private static final int DEFAULT = 8080;
 
	public Server() {
		this(DEFAULT);
	}
 
	public Server(int port) {
		ServerSocket sock;
 
		try {
			sock = new ServerSocket(port);
			System.out.println(String.format("Listening on port %d.", port));
 
			while (true) {
				try {
					Socket client = sock.accept();
					System.out.println("A new connection was accepted.");
 
					BufferedReader in = new BufferedReader(
							new InputStreamReader(client.getInputStream()));		
					OutputStreamWriter out = new OutputStreamWriter(client
							.getOutputStream());
					String input = "";
 
					while (!input.equals("exit")) {
						input = in.readLine();
						if (input.equals("shutdown")) {
							System.exit(0);
						}
						out.write(input.toUpperCase() + "\r\n");
						out.flush();
					}
 
					in.close();
					out.close();
					client.close();
					System.out.println("Connection closed.");
				} catch (NullPointerException npe) {
					System.out.println("Connection closed by client.");
				}
			}
		} catch (IOException ioe) {
			System.err.println(ioe);
			System.exit(-1);
		}
	}
 
	public static void main(String[] args) throws IOException {
		new Server();
	}
}

Usage:

$ javac Server.java
$ java Server
Listening on port 8080.

In another terminal:

$ telnet localhost 8080
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
hi
HI
The quick brown fox jumps over the lazy dog.
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
exi
EXI
exit
EXIT
Connection closed by foreign host.

3 thoughts on “The Caps Lock Java Socket Server

  1. users can add new ships can u help me??

    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;

    public class SpaceShip extends JFrame implements KeyListener, MouseListener, Runnable
    {
    int k=0;
    public static void main(String[] args)
    { new SpaceShip().setVisible(true); }

    Thread runner;
    int speed = 100; //sleeps for 100 milliseconds, decrease if you want faster and increse if you want it slower
    JLabel lblShip = new JLabel(new ImageIcon(“c://spaceship.jpg”));
    JLabel lblShip2 = new JLabel(new ImageIcon(“c://spaceship1.jpg”));

    //1. gemi değişkenleri:
    static int x = 450, y = 0, width = 50, height = 50, incAmountX = 0, incAmountY = 0;
    boolean shoot = false;
    int shootX = 0, shootY = 0;

    //2. gemi değişkenleri:
    static int x2 = 0, y2 = 200, width2 = 50, height2 = 50, incAmountX2 = 0, incAmountY2 = 0;
    boolean shoot2 = false;
    int shootX2 = 0, shootY2 = 0;

    public SpaceShip()
    {
    super(“SpaceShip Wars”);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setLayout(null);
    lblShip.setBounds(x, y, 35, 30);
    add(lblShip);
    lblShip2.setBounds(x2, y2, 35, 30);

    add(lblShip2);

    addKeyListener(this);
    addMouseListener(this);
    runner = new Thread(this);
    runner.start();
    }

    public void keyPressed(KeyEvent e)
    {
    shoot = shoot2 = false;
    switch(e.getKeyCode())
    {
    //1. gemi kontrolleri ok tuşları
    case KeyEvent.VK_UP: incAmountY = -1; incAmountX = 0; break;
    case KeyEvent.VK_DOWN: incAmountY = 1; incAmountX = 0; break;
    case KeyEvent.VK_LEFT: incAmountX = -1; incAmountY = 0; break;
    case KeyEvent.VK_RIGHT: incAmountX = 1; incAmountY = 0; break;

    //2. gemi kontrolleri w-a-s-d tuşları
    case KeyEvent.VK_W: incAmountY2 = -1; incAmountX2 = 0; break;
    case KeyEvent.VK_S: incAmountY2 = 1; incAmountX2 = 0; break;
    case KeyEvent.VK_A: incAmountX2 = -1; incAmountY2 = 0; break;
    case KeyEvent.VK_D: incAmountX2 = 1; incAmountY2 = 0; incAmountX3 = 1; incAmountY3 = 0;
    break;

    //gemileri durdurmak için escape
    case KeyEvent.VK_ESCAPE: incAmountX = incAmountY = incAmountX2 = incAmountY2 = 0; break;
    }
    lblShip.setBounds(x, y, width, height);
    lblShip2.setBounds(x2, y2, width2, height2);

    }
    public void run()
    {
    while(true)
    {
    try
    {
    runner.sleep(speed);
    x += incAmountX;
    y += incAmountY;
    lblShip.setBounds(x, y, width, height);
    x2 += incAmountX2;
    y2 += incAmountY2;
    lblShip2.setBounds(x2, y2, width2, height2);
    lblShip3.setBounds(x3, y3, width3, height3);
    x3 += incAmountX3;
    y3 += incAmountY3;
    repaint();
    }
    catch (InterruptedException e)
    { e.printStackTrace(); }
    }
    }
    public void paint(Graphics g)
    {
    super.paint(g);
    if(shoot)
    g.drawLine(shootX, shootY, x + 20, y + 50);
    else if(shoot2)
    g.drawLine(shootX2, shootY2, x2 + 20, y2 + 50);
    }
    public void mousePressed(MouseEvent e)
    {
    //sol tuş basılıysa 1. gemi ateş eder:
    if(e.getButton() == 1)
    {

    shoot = true;
    shootX = e.getX();
    shootY = e.getY();
    System.out.println(shootX + ” ” + shootY + ” : ” + x2 + ” “+ y2);
    //2. gemi menzilde mi diye kontrol et, eğer menzildeyse oyunu bitir:
    if(shootX >= x2 – 60 && shootX = y2 – 80 && shootY = x – 30 && shootX2 = y – 40 && shootY2 <= y + 40)
    {
    runner.stop();
    JOptionPane.showMessageDialog(null, "2. gemi 1. gemiyi vurdu, oyun bitti");
    }
    }
    repaint();
    }
    //elini mouse'tan kaldırınca ateşi kessin.
    public void mouseReleased(MouseEvent arg0)
    {
    shoot = shoot2 = false;
    repaint();
    }
    public void keyReleased(KeyEvent arg0)
    { }
    public void keyTyped(KeyEvent arg0)
    { }
    public void mouseClicked(MouseEvent e)
    { }
    public void mouseEntered(MouseEvent arg0)
    { }
    public void mouseExited(MouseEvent arg0)
    { }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *