Client Server Broadcasting String and Track the Client
Provider. java:
[Java]
Import java. io .*;
Import java.net .*;
Public class Provider {
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider (){}
Void run ()
{
Try {www.2cto.com
// 1. creating a server socket
ProviderSocket = new ServerSocket (2004, 10 );
// 2. Wait for connection
System. out. println ("Waiting for connection ");
Connection = providerSocket. accept ();
System. out. println ("Connection already ed from" + connection. getInetAddress (). getHostName ());
// 3. get Input and Output streams
Out = new ObjectOutputStream (connection. getOutputStream ());
Out. flush ();
In = new ObjectInputStream (connection. getInputStream ());
SendMessage ("Connection successful ");
// 4. The two parts communicate via the input and output streams
Do {
Try {
Message = (String) in. readObject ();
System. out. println ("client>" + message );
If (message. equals ("bye "))
SendMessage ("bye ");
}
Catch (ClassNotFoundException classnot ){
System. err. println ("Data already ed in unknown format ");
}
} While (! Message. equals ("bye "));
}
Catch (IOException ioException ){
IoException. printStackTrace ();
}
Finally {
// 4: Closing connection
Try {
In. close ();
Out. close ();
ProviderSocket. close ();
}
Catch (IOException ioException ){
IoException. printStackTrace ();
}
}
}
Void sendMessage (String msg)
{
Try {
Out. writeObject (msg );
Out. flush ();
System. out. println ("server>" + msg );
}
Catch (IOException ioException ){
IoException. printStackTrace ();
}
}
Public static void main (String args [])
{
Provider server = new Provider ();
While (true ){
Server. run ();
}
}
}
Requester. java:
[Java]
Import java. io .*;
Import java.net .*;
Public class Requester {
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester (){}
Void run ()
{
Try {
// 1. creating a socket to connect to the server
RequestSocket = new Socket ("localhost", 2004 );
System. out. println ("Connected to localhost in port 2004 ");
// 2. get Input and Output streams
Out = new ObjectOutputStream (requestSocket. getOutputStream ());
Out. flush ();
In = new ObjectInputStream (requestSocket. getInputStream ());
// 3: Communicating with the server
Do {
Try {
Message = (String) in. readObject ();
System. out. println ("server>" + message );
SendMessage ("Hi my server, I am your Client ");
SendMessage ("I want to Send you some Data ");
SendMessage ("Please accept my Data through the Socket ");
SendMessage ("Thank you ");
Message = "bye ";
SendMessage (message );
}
Catch (ClassNotFoundException classNot ){
System. err. println ("data already ed in unknown format ");
}
} While (! Message. equals ("bye "));
}
Catch (UnknownHostException unknownHost ){
System. err. println ("You are trying to connect to an unknown host! ");
}
Catch (IOException ioException ){
IoException. printStackTrace ();
}
Finally {
// 4: Closing connection
Try {
In. close ();
Out. close ();
RequestSocket. close ();
}
Catch (IOException ioException ){
IoException. printStackTrace ();
}
}
}
Void sendMessage (String msg)
{
Try {
Out. writeObject (msg );
Out. flush ();
System. out. println ("client>" + msg );
}
Catch (IOException ioException ){
IoException. printStackTrace ();
}
}
Public static void main (String args [])
{
Requester client = new Requester ();
Client. run ();
}
}
The testing result's printscreen: