Socket Programming

 A sample program to create a server which returns the date time . A socket is created which listens port 13. A connection is established when it accepts a client.  Server writes to the connection using output stream to communicate with the client. Once its time to close the connection, both server and client closes the connection and server waits for next connection.    

DatetimeServer.java

import java.net.*;

import java.io.*;

import java.util.Date;


public class DatetimeServer {

  public final static int port = 13;


  public static void main(String args[]) {

     try(ServerSocket server = new ServerSocket(port)) {

        while(true) {

           try(Socket connection = server.accept()) {

              Writer out = new OutputStreamWriter(connection.getOutputStream());

              Date now = new Date();

              out.write(now.toString()+"\r\n");

              out.flush();

              connection.close();

       

           } catch(IOException ex) {

              ex.printStackTrace();

           }

        } 


     } catch (IOException ex) {

        ex.printStackTrace();

     }

  }


}

Client creates a socket connection and attempts to connect to the remote host. Once the socket is established, set the timeout. Using input stream reader, reads the data from the socket. Usually socket sends any bytes but in this case its set to ASCII.

DatetimeClient.java

import java.net.*;
import java.io.*;
import java.util.Date;
import java.text.*;
public class DatetimeClient {
static Date parseDate(String s) throws ParseException {
String[] pieces = s.split(" ");
String dateTime = pieces[1] + " " + pieces[2] + " UTC";
DateFormat format = new SimpleDateFormat("yy-MM-dd hh:mm:ss z");
return format.parse(dateTime);
}

public static void main(String args[]) throws IOException, ParseException{
String host = args.length > 0 ? args[0] : "time.nist.gov";
try (Socket socket = new Socket(host, 13)) {
socket.setSoTimeout(15000);
InputStream in = socket.getInputStream();
StringBuilder time = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, "ASCII");
for (int c = reader.read(); c != -1; c = reader.read()) {
time.append((char) c);
}
System.out.println(time.toString());
}
}
}




MultiThreadedDatetimeServer.java

import java.net.*;
import java.io.*;
import java.util.Date;

public class MultiThreadedDatetimeServer {
public final static int PORT = 13;

public static void main(String[] args) {
try (ServerSocket server = new ServerSocket(PORT)) {
while (true) {
try {
Socket connection = server.accept();
Thread task = new DaytimeThread(connection);
task.start();
} catch (IOException ex) {
}
}
} catch (IOException ex) {
System.err.println("Couldn't start server");
}
}

private static class DaytimeThread extends Thread {
private Socket connection;

DaytimeThread(Socket connection) {
this.connection = connection;
}

@Override
public void run() {
try {
Writer out = new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() + "\r\n");
out.flush();
} catch (IOException ex) {
System.err.println(ex);
} finally {
try {
connection.close();
} catch (IOException e) {
}
}
}
}
}


Pooled

Comments

Popular posts from this blog

MongoDB

Git

JAXB