Java communication software design2 (updating ...)

Source: Internet
Author: User

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:
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.