Let's Go!

java socket 实现服务端与客户端------一对多的服务端和客户机

上一篇 / 下一篇  2009-04-28 17:16:19 / 个人分类:JAVA学习&编程相关

//一对多的服务端和客户机:一个服务端能同时服务多个客户端,需要使用多线程来实现

//服务端:调用具体的处理线程完成处理
package com.socket.multiserver;
import java.io.*;
import java.net.*;
import com.socket.multiserver.ChildTh;

public class MyMultiServer {
 public static void main(String[] args) throws IOException{
  ServerSocket server=new ServerSocket(5678);
  while (true){
                Socket client=server.accept();
                ChildTh child=new ChildTh(client);//用socket实例初始化具体的处理线程对象
                //使用Runnable接口和使用extends Thread的区别:
                // 如果是使用Runnable接口,需要这么写:
                   new Thread(child).start();               
                //注意:不能写成:child.run();
                // 如果使用extends Thread,只需要child.start()就可以,因为start()是Thread的方法
                }
        }
}       

 

============================================================

//处理线程类:
package com.socket.multiserver;
import java.io.*;
import java.net.*;

public class ChildTh implements Runnable{
        private Socket client;
        public ChildTh(Socket client){
                this.client=client;
        }
       
        public void run(){       
           try{       
             BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
             PrintWriter ut=new PrintWriter(client.getOutputStream());
             while(true){
                     String str=in.readLine();
                     System.out.println(str);
                     out.println("has receive....");
                     out.flush();
                     if(str.equals("end"))
                        break;
             }       
                       
             client.close();
            }catch(Exception e){
                  System.out.println("error in the close the socket!");                       
                  e.printStackTrace();
              }
              finally{
                                                               
              }
        }
       
}

 

===========================================================

//客户端
package com.socket.multiserver;
import java.net.*;
import java.io.*;

public class MyMultiClient{
 static Socket client;
 
 public static void main(String[] args)throws Exception{
 
  client=new Socket(InetAddress.getLocalHost(),5678);
 
         BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
         PrintWriter ut=new PrintWriter(client.getOutputStream());
        
         //从标准输入接收数据
  BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
 
  while(true){
           String str=wt.readLine();
           //String str = "jonathan";
           out.println(str);
           out.flush();
           if(str.equals("end")){
                    break;
          }
           System.out.println(in.readLine());
  }
  client.close();
 }
}

http://www.360doc.com/showWeb/0/0/174225.aspx

三个java文件

编译:javac -d . *.java
执行:java  包.类名

implements Runnable :
        Runnable t = new OurRunnableClass();
        Thread th = new Thread(ot);
        th.start();

java多线程:复写run()方法




TAG:

 

评分:0

我来说两句

Open Toolbar