Topic: 求助一个相关Serve Client的问题

  Print this page

1.求助一个相关Serve Client的问题 Copy to clipboard
Posted by: neusoftonline
Posted on: 2004-12-13 00:10

想让服务器接受到客户端所发的信息,并回复,用什么样的方法比较好?我研究1天了,还是没有头绪DisapprovedDisapproved

2.Re:求助一个相关Serve Client的问题 [Re: neusoftonline] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-12-13 08:42

不知道,下面这个贴子对你有没有帮助,如果有具体的问题,欢迎继续共同探讨
http://www.cjsdn.net/post/view?bid=53&id=120938&tpg=1&ppg=1&sty=1&age=0#120938

3.Re:求助一个相关Serve Client的问题 [Re: neusoftonline] Copy to clipboard
Posted by: neusoftonline
Posted on: 2004-12-13 22:29

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);
      }
    
    
    
  }
}

4.Re:求助一个相关Serve Client的问题 [Re: neusoftonline] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-12-14 10:18

你所说的在DOS下传送信息具体是指什么呢?
可以说的具体一点吗Smile

5.Re:求助一个相关Serve Client的问题 [Re: littledeer1974] Copy to clipboard
Posted by: neusoftonline
Posted on: 2004-12-14 13:41

恩,比如说客户端在连接到服务器后,给服务器发送一个信息,例如BOY,服务器自动给他回一个信息,“男孩”。没有数据库的连接。

6.Re:求助一个相关Serve Client的问题 [Re: neusoftonline] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-12-14 14:21

首先可以尝试,一个服务器和一个客户端的类似的数据交换,象上边给你的例子那样,接下来应该不是很难吧
服务器端和客户断建立起连接之后,每一个客户端的线程生成的MESSAGE带上ID发送的话,服务器端就应该能够识别吧
那样的话不知道是否就算是实现了你说的功能,你可以试试看Smile


   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