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

JAVA基础篇—模拟服务器与客户端通信

时间:2017-08-19 22:31:43      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:net   highlight   tput   模拟   main   .net   client   csharp   out   

第一种:

客户端class Client

package 服务器发送到客户端;

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

public class Client {
     public static void main(String[] args) {
    	 try {
				Socket socket=new Socket("127.0.0.1", 6666);
				System.out.println("??????????????");
					   
 		        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 		        PrintWriter writer=new PrintWriter(socket.getOutputStream());
				BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String readline;
				readline=br.readLine();
				
				while (!readline.equals("end")) {
					writer.println(readline);
					writer.flush();
					System.out.println("????????"+readline);
					System.out.println("????????"+in.readLine());
					readline=br.readLine();
				}
				writer.close();
				in.close();
				socket.close();		
    	 } catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("????????"+e);
			}
     }
}
	

  服务器端class Server

package 服务器发送到客户端;

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

public class Server {
    	public static void main(String[] args) throws IOException{
    		Server server=new Server();
    		server.onServer();
    		
		}
    	public void onServer(){
    		ServerSocket server=null;
      	try {
    		try {
				server=new ServerSocket(6666);
				System.out.println("????????????");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("????????"+e);
			}
    		Socket socket=null;
    		try {
				socket=server.accept();
				System.out.println("????????????");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("Error."+e);
			}
    		String line;
  
				BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
				PrintWriter writer=new PrintWriter(socket.getOutputStream());
				BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
				System.out.println("??????"+in.readLine());
				line=br.readLine();
				while (!line.equals("end")) {
					writer.println(line);
					writer.flush();
					System.out.println("?????????"+line);
					System.out.println("??????"+in.readLine());
					line=br.readLine();
				}
				writer.close();
				in.close();
				server.close();
				socket.close();				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    		
    	}
}

  第二种:

客户端class TCPClient

package 服务器发送到客户端;

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

public class TCPClient {
     public static void main(String[] args) throws UnknownHostException,IOException{
    	 try {
				Socket socket=new Socket("127.0.0.1", 6666);
				System.out.println("??????????????");
					   
		        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		        PrintWriter writer=new PrintWriter(socket.getOutputStream());//?????
		        String readline;
				readline=br.readLine();
				writer.println(readline);
				writer.flush();
				System.out.println("????????"+readline);
				
				BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//?????????													
					System.out.println("????????"+in.readLine());
				
				writer.close();
				in.close();
				socket.close();		
 	 } catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("????????"+e);
			}
	}
}

  服务器端class TCPServer

package 服务器发送到客户端;

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

public class TCPServer {
    public static void main(String[] args) throws IOException{
		ServerSocket server=null;
  	try {
		try {
			server=new ServerSocket(6666);
			System.out.println("????????????");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("????????"+e);
		}
		Socket socket=null;
		try {
			socket=server.accept();
			System.out.println("????????????");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Error."+e);
		}
		String line;
			BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//?????????
			line=in.readLine();
			System.out.println("??????"+line);
			
			
			ArrayList<Integer> list = new ArrayList<Integer>();
			Scanner scanner=new Scanner(line);
			while (scanner.hasNext()) {
				list.add(scanner.nextInt());
			}
			// ?????????????????????,?????????????????鷵???
			
				
			Collections.sort(list);
			
			PrintWriter writer=new PrintWriter(socket.getOutputStream());//?????						
				writer.println(list);
				writer.flush();
				System.out.println("?????????"+list);
				
			
			writer.close();
			in.close();
			server.close();
			socket.close();		
			scanner.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}
}

  

JAVA基础篇—模拟服务器与客户端通信

标签:net   highlight   tput   模拟   main   .net   client   csharp   out   

原文地址:http://www.cnblogs.com/lc-java/p/7397909.html

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