zoukankan      html  css  js  c++  java
  • java socket 编程经典实例

    服务器监听、并接收每个客户端的信息再群发到每个客户端

    服务端

    package com.java.xiong.Net17;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    
    public class RunableSocket implements Runnable {
    	// 定义当前线程处理的Socket
    	private Socket socket = null;
    	// 该线程对用的输入流
    	private BufferedReader read = null;
    
    	public RunableSocket(Socket s) throws IOException {
    		this.socket = s;
    		read = new BufferedReader(
    				new InputStreamReader(socket.getInputStream()));
    	}
    
    	@Override
    	public void run() {
    		String line=null;
    		try{
    			while((line=getClentData())!=null){
    				for(Socket s:MyServer.list){
    					//向每个客户端输出信息
    					PrintStream print=new PrintStream(s.getOutputStream());
    					print.println(line);
    				}
    			}
    			
    		}catch(IOException  io){
    			io.printStackTrace();
    		}
    
    	}
    	//读取客户端数据的方法
    	public String getClentData(){
    		String line=null;
    		try{
    			line=read.readLine();
    		}catch(IOException io){
    			MyServer.list.remove(socket);
    		}
    		return line;
    	}
    
    }
    


    package com.java.xiong.Net17;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyServer {
    	
    	public static List<Socket> list=new ArrayList<Socket>();
    	
    	public static void main(String [] args) throws Exception{
    		ServerSocket  server=new ServerSocket(30001);
    		while(true){
    			Socket scoket=server.accept();
    			list.add(scoket);
    			new Thread(new RunableSocket(scoket)).start();
    		}
    		
    	}
    
    }
    

    客户端

    package com.java.xiong.Net17;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    //将服务器返回的数据打印出来
    public class RubableClient implements Runnable {
    
    	private Socket socket;
    	private BufferedReader read;
    
    	public RubableClient(Socket socket) throws IOException {
    		this.socket = socket;
    		this.read = new BufferedReader(new InputStreamReader(
    				this.socket.getInputStream()));
    	}
    
    	@Override
    	public void run() {
    		try{
    			String line=null;
    			while((line=read.readLine())!=null){
    				System.out.print(line);
    			}
    			
    		}catch(IOException  io){
    			io.printStackTrace();
    		}
    
    	}
    
    }
    
    package com.java.xiong.Net17;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class MyClient {
    
    	public static void main(String[] args) throws Exception, IOException {
    
    		//向服务器发送请求
    		Socket socket = new Socket("127.0.0.1", 30001);
    		new Thread(new RubableClient(socket)).start();
    		//获取输出流
    		PrintStream print = new PrintStream(socket.getOutputStream());
    		String line = "";
    		BufferedReader read = new BufferedReader(new InputStreamReader(
    				System.in));
    		while((line=read.readLine())!=null){
    			//写入Socket对应的输出流
    			print.println(line);
    		}
    	}
    
    }
    



  • 相关阅读:
    本地向GitHub提交403错误解决办法(自己的创建的项目)
    zepto-ajax跨域请求接口 jsonp 静态页面数据展示
    自定义获取url方法
    使用swiper 轮播插件ajax 请求加载图片时,无法滑动问题
    背景透明,文字不透明解决办法
    js DOM Element属性和方法整理----转载
    图像处理作业5——SIFT算法与全景图像生成
    应用连续高斯模糊后得到的σ是多少?
    Pandas学习笔记——综合练习
    Pandas学习笔记5——合并
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3400152.html
Copyright © 2011-2022 走看看