A Java class that writes to and reads from a remote socket

In this article I share some source code for a Java class that reads and writes to a remote socket.

I’m not going to describe this much today, but I put the source code for this Java together from a number of other sources on the internet. In short, it uses a Java Socket to connect to a port on a remote server, sends a command (a string) to that server to be executed, and then reads the output from the command that is executed. As a result, I assume that all information sent is text (no binary data).

How this Java socket client works

In the real world, this Java code works as a socket client, and calls a Ruby script I’ve installed on several remote servers. That Ruby script runs under xinetd on the remote Linux systems, executes the commands I pass to it, and returns the results to me.

Although I’ve hard-coded a number of variables in this Java class, you can modify it very easily to work for your needs when it comes to open and connect to remote sockets.

Without any further introduction, here is the source code for my Java SocketClient class:

import java.io.*;
import java.net.*;

public class SocketClient
{
  Socket sock;
  String server = "ftpserver";
  int port = 5550;
  String filename = "/foo/bar/application1.log";
  String command = "tail -50 " + filename + "\n";
  
  public static void main(String[] args)
  {
    new SocketClient();
  }
  
  public SocketClient()
  {
    openSocket();
    try 
    {
      // write to socket
      BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
      wr.write(command);
      wr.flush();
      
      // read from socket
      BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      String str;
      while ((str = rd.readLine()) != null)
      {
        System.out.println(str);
      }
      rd.close();
    } 
    catch (IOException e) 
    {
      System.err.println(e);
    }
  }
  
  private void openSocket()
  {
    // open a socket and connect with a timeout limit
    try
    {
      InetAddress addr = InetAddress.getByName(server);
      SocketAddress sockaddr = new InetSocketAddress(addr, port);
      sock = new Socket();
  
      // this method will block for the defined number of milliseconds
      int timeout = 2000;
      sock.connect(sockaddr, timeout);
    } 
    catch (UnknownHostException e) 
    {
      e.printStackTrace();
    }
    catch (SocketTimeoutException e) 
    {
      e.printStackTrace();
    }
    catch (IOException e) 
    {
      e.printStackTrace();
    }
  }
}

I’m sorry I don’t have time today to explain this code, but as a quick summary, if you were looking for some Java code that shows how to write to a Java socket, and also read from a Java socket, I hope this is helpful.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.