我现在想要达到的效果就是当客户端发送一个名字到服务器,服务器会存储起这个名字,然后再加上已经存储的名字一起发送回相应客户端。例如客户1发送了Jack,服务器返回Jack,当客户2发送Rose的时候,服务器会返回Jack/nRose
我是用Eclipse来写的,现在的问题是返回的结果不正确,我又不知道怎么调试,希望大家帮我找出问题的同时也教一下我怎么调试这种与网络相关的程序。谢谢import java.net.*;
import java.io.*;/**
 * A wrapper class of Socket which contains 
 * methods for sending and receiving messages
 * @author M. L. Liu
 */
public class MyStreamSocket extends Socket {
   private Socket  socket;
   private BufferedReader input;
   private PrintWriter output;   MyStreamSocket(InetAddress acceptorHost,
                  int acceptorPort ) throws SocketException,
                                   IOException{
      socket = new Socket(acceptorHost, acceptorPort );
      setStreams( );   }   MyStreamSocket(Socket socket)  throws IOException {
      this.socket = socket;
      setStreams( );
   }   private void setStreams( ) throws IOException{
      // get an input stream for reading from the data socket
      InputStream inStream = socket.getInputStream();
      input = 
         new BufferedReader(new InputStreamReader(inStream));
      OutputStream outStream = socket.getOutputStream();
      // create a PrinterWriter object for character-mode output
      output = 
         new PrintWriter(new OutputStreamWriter(outStream));
   }   public void sendMessage(String message)
              throws IOException {
      output.print(message + "\n");   
      //The ensuing flush method call is necessary for the data to
      // be written to the socket data stream before the
      // socket is closed.
      output.flush();               
   } // end sendMessage   public String receiveMessage( )
throws IOException, InterruptedException {
      // read a line from the data stream
      String message = input.readLine( );  
      return message;
   } //end receiveMessage   public void close( )
throws IOException {
      socket.close( );
   }
} //end class
import java.net.*;
import java.io.*;
public class NamePiledupClientHelper {
static final String endMessage = ".";
private MyStreamSocket mySocket;
private InetAddress serverHost;
private int serverPort;
public NamePiledupClientHelper(String hostName,
String portNum)throws SocketException, 
UnknownHostException ,IOException{
this.serverHost = InetAddress.getByName(hostName);
this.serverPort = Integer.parseInt(portNum);
this.mySocket = new MyStreamSocket(this.serverHost,this.serverPort);
System.out.println("Connection request made");
// TODO Auto-generated constructor stu
}
public String getPileup(String message)throws SocketException ,
IOException, InterruptedException{
String remessage;
mySocket.sendMessage(message);
Thread.sleep(1000);
remessage = mySocket.receiveMessage();
return remessage;
}
public void done() throws SocketException,IOException{
mySocket.sendMessage(endMessage);
mySocket.close();
}
}

解决方案 »

  1.   

    import java.io.*;
    public class NamePiledupClient {
    static final String endMessage = ".";
    public static void  main(String [] args) {
    InputStreamReader is = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(is);
    try {
    System.out.println("Welcome to the name pile-up system.\n"+
    "What is the name of the server host?");
    String hostName = br.readLine();
    if (hostName.length() == 0) {
    hostName = "localhost";
    }
    System.out.println("What is the port number of the server host?");
    String portNum = br.readLine();
    if (portNum.length() == 0) {
    portNum = "7";
    }
    NamePiledupClientHelper helper = new NamePiledupClientHelper(hostName, portNum);
    boolean done = false;
    String Message,echo;
    while(!done){
    System.out.println("Enter a name to receive a piled-up " +
    "string from the server,"+"or a single period to quit");
    Message = br.readLine();
    if ((Message.trim()).equals(".")) {
    done = true;
    helper.done();
    }
    else {
    echo = helper.getPileup(Message);
    System.out.println(echo);
    }
    }
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }
    }
    }import java.io.*;
    import java.net.*;
    public class NamePiledupServer {
    public static void  main(String [] args) {
    int serverPort = 7; //default port
    if( args.length ==1)
    serverPort = Integer.parseInt(args[0]);
    try {
    ServerSocket myConnectionSocket = new ServerSocket(serverPort);
    //System.out.println("EchoSever ready.");
    while (true) {
    //System.out.println("Waiting for connection");
    //MyStreamSocket myDataSocket = new MyStreamSocket(myConnectionSocket.accept());

    //System.out.println("Connection accepted");
    Thread theThread = new Thread(new NamePiledupServerThread(myConnectionSocket));
    theThread.start();
    }
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }

    }
    }import java.io.*;
    import java.net.ServerSocket;
    class NamePiledupServerThread  implements Runnable{
    static final String endMessage = ".";
    static String piledupMessage="";
    MyStreamSocket myDataSocket;
    public NamePiledupServerThread(ServerSocket myConnectionSocket)throws IOException {
    //this.myDataSocket = myDataSocket;
    // TODO Auto-generated constructor stub
    this.myDataSocket =  new MyStreamSocket(myConnectionSocket.accept());
    }
    public synchronized void ChangePileup(String name)
    {
    piledupMessage = piledupMessage + name + "\n";
    }
    public void run()
    {
    boolean  done = false;
    String message,test;
    try {
    while (!done) {
    message = myDataSocket.receiveMessage();
    if ((message.trim()).equals(endMessage)) {
    System.out.println("Session over.");
    myDataSocket.close();
    done = true;
    } else {
    ChangePileup(message);
    test = piledupMessage;
    myDataSocket.sendMessage(piledupMessage);
    }
    }
    } catch (Exception e) {
    // TODO: handle exception
    }
    }
    }代码比较长,因为是按照书本上的客户端和服务器三层体系结构写的。请大家耐心看一下,帮我找出问题。谢谢
      

  2.   

         楼主是在做聊天软件吧,之前我也弄过类似的。 我的存储方式是类似链表,如果客户端登录,发送到服务器,服务器把名字链接到已有客户后面,发送的时候,只需要找到客户链表的头,加个while循环就可以一个个发送了。
         
        
       
      

  3.   

    楼上对了,我想实现就是类似的功能。不过现在数据量并不是特别大,我直接就用一个String来存储数据了。但是我现在遇到的问题是,我在调试服务器端的时候,有时是服务器端停在断点那里,可是客户端就已经收到消息了,我就感觉到很郁闷,为什么服务器端还没发消息,客户就已经接受到消息呢?
      

  4.   

    问题解决了。是换行符引起的问题,因为我现在每次receive消息是以换行符作为结束条件的,所以我每次只能收到一个名字,解决办法就是将数据转换成byte,再发送。