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:

 

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:

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……

 

 

    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值