Back  | Code Snippets |  [Bill's Home]

Java Sockets

Java ZIP
Socket Documentation
SocketServer Documentation
Mastering Java Source Code

va ZIP

How to?

The Java program uses two main threads for the client and the server. One reads data from the keyboard and send it to the socket, and the other reads from the socket and sends to the output screen. The files are writethread1.java (which takes from the keyboard and sends to the network connection), writethread2.java (which takes from the network connection and sends to the output screen), client.java (which makes a connection to a remote address, and a remote port) and sever.java (which waits for a connection on a given port). A sample run is:

To test the code, do the following:

1.

Run the server in one command window with server 1001 (which will listen on port 1001), then run the client in another window with client 127.0.0.1 1001 (which will connect to the local server). You should be able to make a connection, and send and receive messages in the two windows.

2.

Run the server in one command window on one machine with server 1001 (which will listen on port 1001). Determine the server IP address with IPCONFIG. Next run the client on another computer with client w.x.y.z 1001 (where w.x.y.z is the IP address of the server). You should be able to make a connection, and send and receive messages in the two computers.

 

 

 

 

 

The code has been designed using threads, as a program without threads would have problems with synchronisation, as the program could be stuck waiting for keyboard input, and not checking for incoming data. The structure is:

The code for the client is:

// Written by W.Buchanan, Aug 2003
// client.java
// Run the program with java client 127.0.0.1 1000 for port 1000 on 127.0.0.1

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;

public class client extends Thread

{
public static void main( String arg[]) throws IOException
{
String addr="127.0.0.1";
int port1=1000;

 if (arg.length>=1) addr=arg[0]; // destination
 if (arg.length>=2) port1=Integer.parseInt(arg[1]); // receiving port


 System.out.println("Using incoming port: " + port1 +
      " Destination address : " + addr);

 try
 {

  Socket sock1 = new Socket(addr,port1);

  System.out.println("Input : " + sock1.getInetAddress() + " Port : "
        + sock1.getPort());

  System.out.print("Creating Write1 Thread...");
  writethread1 w1Thd = new writethread1(sock1);
  writethread2 w2Thd = new writethread2(sock1);
  w1Thd.start();
  w2Thd.start();
 }
 catch(IOException err)
 {
  System.out.println(err.getMessage());
 }
 finally
 {
 System.out.println("End of the program");
 }
}
}



  

and the server is:

// Written by W.Buchanan, Aug 2003
// Server.java
// Run the program with java server 1000 for port 1000

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;

public class server extends Thread
{
 public static void main( String arg[]) throws IOException
 {
 int port1=1000, debug=0;

  if (arg.length>=1) port1=Integer.parseInt(arg[0]); // receiving port

  System.out.println("Using incoming port: " + port1 );
  try
  {

   System.out.println("Listening...");
   ServerSocket sock = new ServerSocket(port1);
   Socket sock1 = sock.accept();
   System.out.println("Accepting");

   System.out.println("Input : " + sock1.getInetAddress() + " Port : "
     
+ sock1.getPort());

   System.out.print("Creating Write1 Thread...");
   writethread1 w1Thd = new writethread1(sock1);
   writethread2 w2Thd = new writethread2(sock1);
   w1Thd.start();
   w2Thd.start();
  }
  catch(IOException err)
  {
   System.out.println(err.getMessage());
  }
  finally
  {
   System.out.println("End of the program");
  }
 }
}


 

and the writethread1:

 

// Written by W.Buchanan, Aug 2003
// writethread1.java
// This thread reads from the keyboard and sends to the stream

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;

public class writethread1 extends Thread
{

Socket sock1=null;


writethread1(Socket s1){
sock1=s1;
}

public void run()
{
byte[] buff= new byte[2000];
int len;
try
{

DataInputStream is = new DataInputStream (System.in);
DataOutputStream out = new DataOutputStream(sock1.getOutputStream());

while (true)
{
try
{
len=is.read(buff);
out.write(buff,0,len);
} catch (IOException err) {}
}

} catch (IOException err) {}
}
}

 

and writethread2.java is:

 

// Written by W.Buchanan, Aug 2003
// writethread2.java
// This thread reads from the input stream and sends to the output

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;

public class writethread2 extends Thread
{

Socket sock1=null;

writethread2(Socket s1){
sock1=s1;
}

public void run()
{
byte[] buff= new byte[2000];
int len;

try
{
DataInputStream is = new DataInputStream (sock1.getInputStream());
DataOutputStream out = new DataOutputStream(System.out);


while (true)
{
try
{
len=is.read(buff);
out.write(buff,0,len);
}
catch (IOException err) {}
}


} catch (IOException err) {}
}
}