Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:求助一个相关Serve Client的问题 [Re:neusoftonline]
neusoftonline





发贴: 6
积分: 0
于 2004-12-13 22:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
littledeer你好,您的贴子我已读过。可能我没把意思说明白,我想实现一个能接收多个用户的服务器端,并让服务器和客户端之间能互相发送信息。现在服务器端和客户端已经做好,但是在DOS下互相传送信息还不知道从何入手,请大家指点。以下是服务器端和客户端程序:
服务器端:
import java.io.*;
import java.net.*;

class ServeOneJabber extends Thread
{
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  public ServeOneJabber(Socket s) throws IOException
  {
    socket=s;
    in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
    start();
  }
  public void run()
  {
    try
    {
      while(true)
      {
        String str=in.readLine();
        if(str.equals("END"))
        break;
        System.out.println("Echoing : "+str);
        out.println(str);
      }
      System.out.println("closing......");
    }
    catch(IOException e)
    {}
    finally
    {
      try
      {
        socket.close();
      }
      catch(IOException e)
      {}
    }
  }
}
public class MulitiJabberServer
{
  static final int PORT=8080;
  public static void main(String[] args)throws IOException
  {
    ServerSocket s=new ServerSocket(PORT);
    System.out.println("Server Started");
    try
    {
      while(true)
      {
        Socket socket=s.accept();
        try
        {
          new ServeOneJabber(socket);
        }
        catch(IOException e)
        {
          socket.close();
        }
      }
    }
    finally
    {  s.close();}
    
  }
}

客户端:
import java.io.*;
import java.net.*;

class JabberClientThread extends Thread
{
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  private static int counter=0;//客户数量
  private int id=counter++;
  private static int threadcount=0;
  public static int threadCount()
  {
    return threadcount;
  }
  public JabberClientThread(InetAddress addr)
  {
    System.out.println("Making client "+id);
    threadcount++;//每产生1个客户端,+1
    try
    {
      socket =new Socket(addr,MulitiJabberServer.PORT);
    }
    catch(IOException e)
    {
      
    }
    try{
      in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
      //out=new PrintWriter(socket.getOutputStream);
      out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
      start();
    }
    catch(IOException e)
    {
    }
    try
    {
      socket.close();
    }
    catch(IOException e2){}
  }
  public void run()
  {
    try
    {
      for(int i=0;i<12;i++)
      {
        out.println("Client "+id+" :"+i);
        String str=in.readLine();
        System.out.println(str);
      }
      out.println("END");
    }
    catch(IOException e)
    {}
    finally
    {
      //最终总是要关闭
      try
      {
        socket.close();
      }
      catch(IOException e)
      {}
      //结束线程
      threadcount--;
    }
  }
}
public class MultiJabberClient
{
  static final int MAX_THREADS=40;
  
    public static void main(String[] args)throws IOException,InterruptedException
  {
    
    InetAddress addr=InetAddress.getByName(null);
    
      while(true)
      {
        if(JabberClientThread.threadCount()<MAX_THREADS)
        new JabberClientThread(addr);
        Thread.currentThread().sleep(100);
      }
    
    
    
  }
}




搞笑QQ图片

话题树型展开
人气 标题 作者 字数 发贴时间
4161 求助一个相关Serve Client的问题 neusoftonline 51 2004-12-13 00:10
3324 Re:求助一个相关Serve Client的问题 littledeer1974 115 2004-12-13 08:42
3368 Re:求助一个相关Serve Client的问题 neusoftonline 3083 2004-12-13 22:29
3283 Re:求助一个相关Serve Client的问题 littledeer1974 34 2004-12-14 10:18
3322 Re:求助一个相关Serve Client的问题 neusoftonline 61 2004-12-14 13:41
3418 Re:求助一个相关Serve Client的问题 littledeer1974 137 2004-12-14 14:21

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923