Java Communication design(Updating)
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 received 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……
补充:软件开发 , Java ,