FileName: Moreserver.java
import java.io.*;
import java.net.*;
import java.util.*;
/**
* <p>title: Multi-threaded server </p>
* <p>description: This example uses multithreading to implement multiple service functions. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>filename: </p>
* @version 1.0
*/
class Moreserver
{
public static void Main (String [] args) throws IOException
{
System.out.println ("Server starting...\n");
//Service with 8000 ports
serversocket Server = new ServerSocket (8000);
while (true)
{
//block until customer is connected
Socket SK = server.accept ();
System.out.println ("accepting connection...\n");
//Start service thread
New Serverthread (SK). Start ();
}
}
}
//using threads, servicing multiple clients
class Serverthread extends Thread
{
private Socket SK;
Serverthread (Socket SK)
{
this.sk = SK;
}
//Thread run entity
public void Run ()
{
BufferedReader in = null;
printwriter out = null;
try{
InputStreamReader ISR;
ISR = new InputStreamReader (Sk.getinputstream ());
in = new BufferedReader (ISR);
out = new PrintWriter (
New BufferedWriter (
New OutputStreamWriter (
Sk.getoutputstream ()), true);
while (true) {
//receives requests from clients and returns different information according to different commands.
String cmd = In.readline ();
System.out.println (CMD);
if (cmd = null)
break;
cmd = Cmd.touppercase ();
if (Cmd.startswith ("BYE")) {
out.println ("BYE");
break;
}else{
out.println ("Hello, I am the server!") ");
}
}
}catch (IOException e)
{
System.out.println (e.tostring ());
}
finally
{
System.out.println ("Closing connection...\n");
//FINAL release of resources
try{
if (in!= null)
In.close ();
if (out!= null)
Out.close ();
if (SK!= null)
Sk.close ();
}
catch (IOException e)
{
System.out.println ("Close err" +e);
}
}
}
}
//filename: Socketclient.java
import java.io.*;
import java.net.*;
class Socketthreadclient extends Thread
{
public static int count = 0;
//constructors, implementing services
public socketthreadclient (inetaddress addr)
{
count++;
BufferedReader in = null;
printwriter out = null;
Socket SK = null;
try{
//Using 8000-Port
SK = new Socket (addr, 8000);
InputStreamReader ISR;
ISR = new InputStreamReader (Sk.getinputstream ());
in = new BufferedReader (ISR);
//Set up output
out = new PrintWriter (
New BufferedWriter (
New OutputStreamWriter (
Sk.getoutputstream ()), true);
//Send request to server
System.out.println ("Count:" +count);
out.println ("Hello");
System.out.println (In.readline ());
out.println ("BYE");
System.out.println (In.readline ());
}
catch (IOException e)
{
System.out.println (e.tostring ());
}
finally
{
out.println ("End");
//Free resources
Try
{
if (in!= null)
In.close ();
if (out!= null)
Out.close ();
if (SK!= null)
Sk.close ();
}
catch (IOException e)
{
}
}
}
}
//Client
public class socketclient{
public static void Main (string[] args) throws Ioexception,interruptedexception
{
inetaddress addr = Inetaddress.getbyname (null);
for (int i=0;i<10;i++)
new Socketthreadclient (addr);
Thread.CurrentThread (). Sleep (1000);
}
}