java socket 编程

客户端:

package com.infosys.luch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class client {

 /**
  * @param args
  */
 public static void main(String[] args) {

  try {
   // 向本机的4700端口发出客户请求
   Socket socket = new Socket(InetAddress.getLocalHost(), 4700);
   // 由系统标准输入设备构造BufferedReader对象
   BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
   // 由Socket对象得到输出流,并构造PrintWriter对象
   PrintWriter os = new PrintWriter(socket.getOutputStream(), true);
   // 由Socket对象得到输入流,并构造相应的BufferedReader对象
   BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   String readline;
   // 从系统标准输入读入一字符串
   readline = sin.readLine();
   while (true) {
    // 将从系统标准输入读入的字符串输出到Server
    os.println(readline);
    // 刷新输出流,使Server马上收到该字符串
    os.flush();
    // 在系统标准输出上打印读入的字符串
    System.out.println("Client_Client:" + readline);
    // 从Server读入一字符串,并打印到标准输出上
    System.out.println("Client_Server:" + is.readLine());
    // 从系统标准输入读入一字符串 + " " + new Date()
    readline = sin.readLine();
    if ("bye".equals(readline)) {
     break;
    }
   }
   //again send message to server.
   os.println(readline);
   sin.close();
   os.close(); // 关闭Socket输出流
   is.close(); // 关闭Socket输入流
   socket.close(); // 关闭Socket
  } catch (IOException e) {
   System.out.println("Error:" + e);
   e.printStackTrace();
  }
 }
}

 

服务器端:

package com.infosys.luch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class server{

 /**
  * @param args
  */
 public static void main(String[] args){

  try {
   ServerSocket server = null;
   try {
    // 创建一个ServerSocket在端口4700监听客户请求
    server = new ServerSocket(4700);
   } catch (Exception e) {
    // 出错,打印出错信息
    System.out.println("can not listen to:" + e);
   }
   Socket socket = null;
   try {
    // 使用accept()阻塞等待客户请求,有客户请求到来则产生一个Socket对象,并继续执行
    socket = server.accept();
   } catch (Exception e) {
    // 出错,打印出错信息
    System.out.println("Error." + e);
   }
   String line;
   // 由Socket对象得到输入流,并构造相应的BufferedReader对象
   BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   // 由Socket对象得到输出流,并构造PrintWriter对象
   PrintWriter os = new PrintWriter(socket.getOutputStream(), true);
   // 从标准输入读入一字符串
   line = is.readLine();
   // 如果该字符串为 "bye",则停止循环
   while (true) {
    // 向客户端输出该字符串
    os.println(line);
    // 刷新输出流,使Client马上收到该字符串
    os.flush();
    // 在系统标准输出上打印读入的字符串
    System.out.println("Server_Server:" + line);
    if ("bye".equals(line)) {
     break;
    }
    line = is.readLine(); 
   }
   os.close(); // 关闭Socket输出流
   is.close(); // 关闭Socket输入流
   socket.close(); // 关闭Socket
   server.close(); // 关闭ServerSocket
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 

你可能感兴趣的:(java,exception,server,String,socket,Class)