M在socket编程新... I HV创建一个从队列中的客户端存储名称& ID服务器,然后将其保存在另一个队列客户端提供的所有输入。当客户端写入'测试'时,服务器检索所有存储的数据作为客户端类型的一些值,即整数...当客户端类型'恢复'服务器再次开始存储客户端的给定输入队列...如果客户端类型'退出'服务器发回客户端的名称和id并开始等待ne客户端。接收这些信息并关闭套接字。服务器的客户端套接字编程输出困难
问题: m在从队列中检索数据时遇到问题。当我输入退出时,我可以看到通过for循环回顾的名称和ID。如果我把这一行outToClient.writeBytes(“Thank You!”+'\ n');在for循环之后,它会显示客户端的名称& id,但客户端工作正常。
中的if else条件同时检查“测试”再次声明,我在检索数据面临的问题。服务器要求整数..客户类型一个整数,然后我不从服务器获取数据。
这里是我的代码
服务器端:
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;
}
}
}
}
yea ...这是家庭作业nm试图做到这一点... m不擅长java所以有困难.... m会去尝试PrintWriter n让你知道..thnx – ZoeHime
我只替换了dataoutputstream与PrintWriter一样:PrintWriter outToClient = null; outToClient = new PrintWriter(connectionSocket.getOutputStream(),true);和数据打印的地方,如:outToClient.println(“谢谢你!你现在可以继续......”+'\ n'); 现在有点慌乱,因为输出不是因为它是...可以üPLZ有点或可能引用任何有用的链接?米检查一些,但仍然我会很感激,如果你的帮助 – ZoeHime
你能用英语而不是文字说话吗?你可以用你现在拥有的东西来更新你的代码吗? –