Socket 点对面 通信


Socket 点对面 通信
创建多客户连接的socket通信的方式,是在服务器端创建客户连接请求的监听线程,一旦客户发起请求,则服务器端用户创建和此客户端通信线程的和Socket,服务器把此客户端的通信交给此线程进行处理.同时继续在服务器的指定端口进行监听,来响应其他客户端的请求.


//TextFaceServer.java
  

package com.itheima.net.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 点对面  server
 */

//操作Socket服务器端的类
public class TextFaceServer { 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new FaceServer(8080);
		} catch (Exception e) {
			System.out.println("服务器端进行端口监听出现异常:"+e.getMessage());
		}
	}

}


class FaceServer extends ServerSocket{

	private int port;							//端口号
	public FaceServer(int port) throws IOException {
		super(port);
		this.port=port;
		System.out.println("服务器已启动,监听端口为:"+port);
		System.out.println("正在等待客户端的连接.....");
		try {
			while (true) {
				Socket socketCon=accept();
				new ServerThread(socketCon,port);
			}
		} catch (Exception e) {
			System.out.println("没有监听到客户端的信息.....");
		}finally{
			close();
		}
		
	}
}

//继承线程类 实现 服务器线程
class ServerThread extends Thread{
	private int port;					//端口
	private Socket socketCon;			//获得连接对象
	
	private BufferedReader in;
	private PrintWriter out;
	public ServerThread(Socket socketCon,int port) throws Exception {
		this.port = port;
		this.socketCon = socketCon;
		//获取客户端的数据流
		in=new BufferedReader(new InputStreamReader(this.socketCon.getInputStream(),"gb2312"));
		//获取写往客户端的数据输出流,true表示自动刷新
		out=new PrintWriter(socketCon.getOutputStream(),true);
		//向客户端发送连接信息
		out.println("服务器端连接成功.....");
		out.println("输入exit断开与服务器的连接");
		start();   //启动线程
	}
	
	//处理信息的方法
	public String infoUpperString(String line){
		return line.toUpperCase();
	}
	
	//线程的run方法
	public void run(){
		try {
			boolean done=false;
			while (!done) {
				String line=in.readLine();  //	读取客户端的每行信息
				if(line==null){
					done=true;
				}else{
					System.out.println("客户端传来的内容:"+line);
					String message=infoUpperString(line);
					
					out.println("从服务器端口发送的内容:"+message);
					if(line.trim().equals("exit"))
						done=true;
				}
			}
		  System.out.println("客户端终止发送信息......");
		  socketCon.close();  //关闭通信资源
		}catch (Exception e) {
			System.out.println("启动服务器端出现错误:"+e.getMessage());
		}
	}
	
}


//TextPointClient.java
package com.itheima.net.socket;

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

/**
 * 点对面   client
 * @author Bin
 *
 */
public class TextPointClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new PointClient("localhost",8080);
		} catch (Exception e) {
			System.out.println("测试客户端连接错误:"+e.getMessage());
		}
	}

}


class PointClient{        				//Socket点客户端
	private String host;				//Ip地址 或者域名
	private int port;					//端口
	public PointClient(String host, int port) {
		this.host = host;
		this.port = port;
		connectionSocket();
	}
	
	//连接的方法
		public void connectionSocket(){
			try {
				Socket socketConn;
				
				if(host.equals("localhost") || host.equals("127.0.0.1")){  //创建本地连接
					socketConn=new Socket(InetAddress.getLocalHost(),port);
				}else{
					socketConn=new Socket(InetAddress.getByName(host),port);  //创建远程连接
				}
				
				//获得从键盘的输入流
				BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
				
				//获得服务器内容的数据流
				PrintWriter out=new PrintWriter(socketConn.getOutputStream(),true); 
				
				//获得服务器端发送内容的缓冲流
				BufferedReader in=new BufferedReader(new InputStreamReader(socketConn.getInputStream()));
				
				System.out.println("服务器信息:"+in.readLine());  //从服务器端获取信息
				System.out.println("服务器信息:"+in.readLine());
				System.out.print("请输入>");
				boolean done=false;
				while(!done){
					String line=stdin.readLine(); 		//获得从键盘输入的每行字符 
					out.println(line);					//发送到服务器端
					if(line.equalsIgnoreCase("exit"))
						done=true;
					String info=in.readLine();
					System.out.println("服务器信息:"+info);
					if(!done)
						System.out.println("请输入>");
				}
				socketConn.close();
			} catch (SecurityException e) {
				System.out.println("连接服务器出现安全问题:"+e.getMessage());
			}catch (Exception e) {
				System.out.println("连接服务器出现I/O错误:"+e.getMessage());
			}
		}
	
	
}


你可能感兴趣的:(socket)