Socket 客户端和服务端,多个客户端和服务端通,客户端与客户端之间通信!

服务端

package luck;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Servr {
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		int port = 8000;
		//创建一个serversocket监听在8000端口上
		ServerSocket server = new ServerSocket(port);
		while(true){
			System.out.println("1");
			//server试着尝试接收其他socket的连接请求,server的accept方式是阻塞式的
			Socket socket = server.accept();
			//建立一个线程来接收客户端发来的消息
			new Thread(new Task(socket)).start();
			//建立另一个线程发送消息到客户端
			new Thread(new sendMessage(socket)).start();
		}
	}
	/*
	 * 处理socket请求
	 */
	static class Task implements Runnable {
		private Socket socket;
		public Task(Socket socket){
			this.socket = socket;
		}
		public void run() {
			try {
				handleSocket();
			} catch (Exception e) {
				if(e.getMessage().equals("Connection reset")){
					System.out.println("客户端 主动关闭socket");
				}
			}
		} 
		/*
		 * 跟客户端进行通信
		 */
		private void handleSocket() throws Exception {  
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
			int len;
			StringBuffer sb = new StringBuffer();
			String temp = "";
			while(!(temp = br.readLine()).equals("end")){
				sb = new StringBuffer();
				if( (len = temp.indexOf("of")) != -1){
					sb.append(temp.substring(0, len));
				}
				sb.append(temp);
				System.out.println(" from client :"+sb);
			}
		}
	}
	/**
	 * 服务端发送消息给客户端
	 * @author luck
	 */
	static class sendMessage implements Runnable {
		private Socket socket;
		public sendMessage(Socket socket){
			this.socket = socket;
		}
		@Override
		public void run() {
			Writer write;
			try {
				write = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
				while (true) {
					Scanner in=new Scanner(System.in);
					write.write(in.next());
					write.write("\n");
					write.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} 
		
	}
}

客户端 (可扩展多个客户端通信,客户端和客户端之间通信)


package luck;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.util.Scanner;
public class ClientSocketOne {
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String ip =  "127.0.0.1";//链接服务器的ip
		int port = 8000;//连接服务器的端口
		//与服务端建立链接
		Socket client = new Socket(ip,port); 
		//一个线程接收消息
		new Thread(new Writes(client)).start();
		//一个线程发送消息
		new Thread(new Send(client)).start(); 
	}
	static class Writes implements Runnable {
		private Socket client;
		public Writes(Socket client){
			this.client = client;
		}
		public void run() {
			try {
				socketServer();
			} catch (IOException e) {
				if(e.getMessage().equals("Connection reset")){
					System.out.println("服务端关闭");
				}
			}
		}
		/*
		 * 跟服务端进行通信
		 */
		private void socketServer() throws IOException{
			BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
			int len;
			String temp ="";
			StringBuffer sb;
			while(client.isConnected() && !(temp = br.readLine()).equals("end")){
				sb = new StringBuffer();
				if((len = temp.indexOf("of")) != -1){
					sb.append(temp.substring(0, len));
				}
				sb.append(temp);
				System.out.println("from server  : "+sb);
			}
		}
	}
	/**
	 * 客户端发送消息给服务端
	 * @author luck
	 *
	 */
	static class Send implements Runnable {
		private Socket client;
		public Send(Socket client){
			this.client = client;
		}
		public void run() {
			try {
				socketServer();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		/*
		 * 跟服务端进行通信
		 */
		private void socketServer() throws IOException{
			Writer writer = new OutputStreamWriter(client.getOutputStream(), "UTF-8");
			while (true) {
				Scanner in=new Scanner(System.in); 
				writer.write(in.next());
				writer.write("\n");
				writer.flush();
			}
		}
	}
}

 

你可能感兴趣的:(scoket通信)