Java Communication design (Updating)

Source: Internet
Author: User

Design communications software
This blog is a note of my class -- Designing Embeded communication software. and the teacher is a greate expert, and I really appreciate the teacher. and the following are some notes that he teaches us how to design the communicaions software, and the software is programmed by java technology.
The first program is very easy, but it's very important to learn the principle of how the client program get the information from the server program.

 
Here are the procedures:

The Server. java:
 
[Java]
Import java. io. *; // can make use of the classes written for handling inout outoput operations
Import java.net. *; // Classes supporting TCP/IP based client-server connections
 
Public class Server {
Public static void main (String args []) {
String data = "Hello Client I am Server, Have you Have Ed my Data ";
Try {

System. out. print ("Server waiting for client! \ N ");
ServerSocket srvr = new ServerSocket (1234 );
Socket skt = srvr. accept ();
System. out. print ("Client has connected to Server! \ N ");
PrintWriter out = new PrintWriter (skt. getOutputStream (), true );
System. out. print ("Sending string: '" + data + "' \ n ");
Out. print (data );
Out. close ();
Skt. close ();
Srvr. close ();
}
Catch (Exception e ){
System. out. print ("Server Can't find the client! \ N ");
}
}
 
}

The client. java:
[Java]
Import java. io .*;
Import java.net .*;
 
Class Client {
Public static void main (String args []) {
Try {

System. out. print ("Client connecting to Server... \ n ");
Socket skt = new Socket ("localhost", 1234 );
BufferedReader in = new BufferedReader (new
InputStreamReader (skt. getInputStream ()));
System. out. print ("Connected to Server... \ n ");
System. out. print ("Received string from Server: '\ n ");
 
While (! In. ready ()){}
System. out. println (in. readLine (); // Read one line and output it
 
System. out. print ("'\ n ");
In. close ();
}
Catch (Exception e ){
System. out. print ("Client Can't find the Server! \ N ");
}
}
}

And the output is:

The second program wil be taught on the next week ......
 
 

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.