服务器客户端socket编程输出困难

时间:2011-11-29 18:00:57

标签: java

新的套接字编程...我想创建一个存储名称和服务器的服务器来自队列中的客户端的id然后它将客户端给出的所有输入存储在另一个队列中。当客户端写'test'时,服务器检索所有存储的数据,因为客户端键入一些值,即整数...当客户端类型'resume'服务器再次开始在队列中存储客户端的给定输入时...如果客户端类型'退出'服务器发回客户端的名称和ID,并开始等待客户端。接收这些信息并关闭套接字。

面临的问题: m在从队列中检索数据时遇到问题。当我输入exit时,我可以看到我正在通过for循环重新搜索的名称和ID。如果我把这行outToClient.writeBytes(“谢谢!”+'\ n');在for循环之后,它显示客户端的名称& id但是客户端会离开。

在if else条件下再次检查'test'时我在检索数据时遇到问题。服务器请求integer..client类型一个整数,然后我不从服务器获取数据。

这是我的代码

服务器端:

import java.io.*;
import java.net.*;
import java.util.*;

class server
{
  public static void main(String argv[]) throws Exception
{
 String clientSentence;
 String replySentence;
 ServerSocket welcomeSocket= new ServerSocket(6789);

while(true)
{
System.out.println("#########Server Waiting#########");
Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));          
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

Queue<String> qe = new LinkedList<String>();

outToClient.writeBytes("Enter your Name and ID Please..."+'\n');
for(int i=0;i<=1;i++)
{
 clientSentence = inFromClient.readLine();
 qe.add(clientSentence); 
}

outToClient.writeBytes("Thank you! You may now proceed further..."+'\n'); 

Queue<String> chatq = new LinkedList<String>();
clientSentence = inFromClient.readLine();

while(!clientSentence.equals("exit"))
{

  if(clientSentence.equals("test"))
  {
    outToClient.writeBytes("Enter Integers to fetch data or 'resume' to continue..."+'\n');
    clientSentence = inFromClient.readLine();

    while(!clientSentence.equals("resume"))
    {
        replySentence = chatq.remove();
        outToClient.writeBytes(replySentence+'\n');
        clientSentence = inFromClient.readLine();
    }

    if(clientSentence.equals("resume"))
    {
      outToClient.writeBytes("You may now proceed again..."+'\n');
      clientSentence = inFromClient.readLine();

    } 
  }

  else
  {
    chatq.add(clientSentence);
    clientSentence = inFromClient.readLine();
  }        

}

if(clientSentence.equals("exit"))
{
  outToClient.writeBytes("Client Name & ID: "+'\n');

 for(int i=0;i<=1;i++)
  {
    replySentence = qe.remove();
    outToClient.writeBytes(replySentence+'\n');
  } 


}  
} 
}
}

客户端:

import java.io.*;
import java.net.*;
import java.util.*;

class client
{
   public static void main(String argv[]) throws Exception
   {
   String sentence;
   String modifiedSentence;

   BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

   InetAddress inetAddress=InetAddress.getLocalHost();
   System.out.println(inetAddress);

   Socket clientSocket = new Socket(inetAddress,6789);
   while(true)
   {

      DataOutputStream outToServer =  new DataOutputStream(clientSocket.getOutputStream());       

      BufferedReader inFromServer = new  BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

      modifiedSentence=inFromServer.readLine();
      System.out.println("From Server: "+modifiedSentence+'\n');

      for(int i=0;i<=1;i++)
      {
       sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence+'\n');   
      }
       modifiedSentence=inFromServer.readLine();
       System.out.println("From Server: "+modifiedSentence + '\n');



        sentence = inFromUser.readLine();
         while(!sentence.equals("exit"))
         {

if(sentence.equals("test"))
{
  outToServer.writeBytes(sentence+'\n');
  modifiedSentence=inFromServer.readLine();
  System.out.println("From Server: "+modifiedSentence + '\n');
  sentence = inFromUser.readLine();

  while(!sentence.equals("resume"))
  {
    outToServer.writeBytes(sentence+'\n');
    modifiedSentence=inFromServer.readLine();
    System.out.println("From Server: "+modifiedSentence + '\n');
    sentence = inFromUser.readLine();
  }

  if(sentence.equals("resume"))
  {
    outToServer.writeBytes(sentence+'\n');
    modifiedSentence=inFromServer.readLine();
    System.out.println("From Server: "+modifiedSentence + '\n');
    sentence = inFromUser.readLine();

  }

}

else
{
  outToServer.writeBytes(sentence+'\n');
  sentence = inFromUser.readLine();
 }
}

  if(sentence.equals("exit"))
  {
  outToServer.writeBytes(sentence+'\n');
  modifiedSentence=inFromServer.readLine();
  System.out.println("From Server: "+modifiedSentence + '\n');
  for(int i=0;i<=1;i++)
  {
    modifiedSentence=inFromServer.readLine();
    System.out.println(modifiedSentence + '\n');
  } 

  clientSocket.close(); 
  break;
}

}

}
}

1 个答案:

答案 0 :(得分:0)

我认为这是[homework]但是......

您正在将二进制(DataOutputStream)与文本(BufferedReader)混合。你应该使用其中一种,否则你一定会迷惑自己。

看来你想发送文字,所以我会使用BufferedReader和PrintWriter。 DataOutputStream更适合二进制协议。