Name: Brian Mwangi Makhembu
Reg no: CS282-6772/2013
Unit Code: BIT 2312
Unit: Client Server Computing System
Practical CAT
Talk Server Java Application
Server-side code
import [Link].*;// contains all network libraries
import [Link].*;// allows input output string
import [Link].*; //Scanner to prompt user input
public class TalkServer extends Thread
{
private ServerSocket serverSocket; //declare a server socket object. Since
it's the client, only needs ports
Scanner input = new Scanner([Link]);
public TalkServer(int port) throws IOException //function to define server
socket, and specify timeout
{
serverSocket = new ServerSocket(port);
[Link](20000);
}
public void run() //function to handle communication between server and
client
{
while(true) // loop to keep the client running indefinitely
{
try
{
Socket server = [Link](); // create a socket
object with a connection from a client
DataInputStream in = new
DataInputStream([Link]());
[Link]([Link]()); // read the output from the
server, and print it out on console
DataOutputStream out = new
DataOutputStream([Link]());
[Link]("Hi, I'm "+ [Link]() + "\
ntalk to me!"); // send the initial message and
while (true){
String st = new String([Link]()); // read the message
from the client
[Link](st); // print out client's message to
screen
[Link]("SERVER>> " + [Link]() + "\
nresponse<< "); // send reply to client
}
}catch(SocketTimeoutException s) // catch timeout error
{
[Link]("socket timed out!");
break;
}catch(IOException e)
{
[Link]("Error occurred; ");
[Link]();
break;
}
}
}
public static void main(String [] args) //main function, where execution
should start
{
int port = 5050;
try
{
Thread t = new TalkServer(port);
[Link]();
}catch(IOException e)
{
[Link]();
}
}
}
Client-Side Code
import [Link].*;
import [Link].*;
import [Link].*;
public class TalkClient
{
public static void main(String [] args)
{
Scanner input = new Scanner([Link]);
String serverName = "localhost";
int port = [Link]("5050");
try
{
Socket client = new Socket (serverName, port);
OutputStream outToServer = [Link]();
DataOutputStream out = new DataOutputStream(outToServer);
[Link]("Hello from " + [Link]());
InputStream inFromServer = [Link]();
DataInputStream in = new DataInputStream(inFromServer);
[Link]([Link]());
while(true){
String toServer = "CLIENT>> ";
toServer = toServer + [Link]();
[Link](toServer + "\nresponse<< ");
String st = new String ([Link]());
[Link](st);
}
}catch(IOException e)
{
[Link]();
}
}
}
SCREENSHOTS
Server screenshot
Client Screenshot