码迷,mamicode.com
首页 > 编程语言 > 详细

星期三—用JAVA制作简易的通讯软件

时间:2017-07-12 20:03:14      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:output   tac   adl   readline   sys   服务器   启动   bre   method   

服务端:

package com.chinasofti.corejava.ch22;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Server {
	private static BufferedReader br=null;
	private static PrintWriter pw=null;
	private static ServerSocket ss;
	private static Socket s;
	static Scanner scanner=new Scanner(System.in);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			ss=new ServerSocket(5500);
			System.out.println("服务器正常启动。。。。");
		    s=ss.accept();//阻塞方法
		    System.out.println("连接成功"+s.getRemoteSocketAddress());
		    br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			while(true){
				String string=br.readLine();
				System.out.println("Server读到:"+string);
				System.out.println("Server端请输入:");
				String str=scanner.next();
				pw.println(str);
				pw.flush();
				if(str.equals("exit")){
					break;
				}
			}
		} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
		}
		try {
			pw.close();
			br.close();
		} catch (IOException e1) {
				// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

} 
端口:
package com.chinasofti.corejava.ch22;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client {
	private static PrintWriter pw=null;
	private static BufferedReader br=null;
	private static Socket s;
	static Scanner scanner=new Scanner(System.in);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Socket s=new Socket(InetAddress.getLocalHost(),5500);
			pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			while(true){
				System.out.println("Client端请输入:");
				String str = scanner.next();
				pw.println(str);
				pw.flush();
				String string=br.readLine();
				System.out.println("Client读到:"+string);
				if(str.equals("exit")){
					break;
				}
			} 
		}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		try {
			br.close();
			pw.close();
		} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
		}
	}
}

  

星期三—用JAVA制作简易的通讯软件

标签:output   tac   adl   readline   sys   服务器   启动   bre   method   

原文地址:http://www.cnblogs.com/wx1691790309/p/7157128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!