gpt4 book ai didi

java - 通过socket同时接收两条消息

转载 作者:行者123 更新时间:2023-11-30 07:00:30 25 4
gpt4 key购买 nike

我正在使用套接字进行训练,为此我正在本地主机中提供聊天服务。我的问题是,当向服务器发送查询时,要使用昵称,服务器必须向我发送确认或错误(昵称可以使用或不使用)。但只有当客户端向服务器发送第二个查询时,服务器才会向客户端给出答案:同时确认和回答第二个查询。这不是我想要的,我已经尝试查找服务器是否收到查询并且它收到了。

我的服务器代码提供用户名:

static private void resolveUsername(Selector sel, SelectionKey key, String nick) throws IOException {
for (SelectionKey k : sel.keys()) {
if(k.isValid() && k.channel() instanceof SocketChannel)

if(((User)k.attachment()).getNick().equals(nick)) {
SocketChannel sct = (SocketChannel)key.channel();
sct.write(enc.encode(CharBuffer.wrap("ERROR")));
return;
}
}

if( ((User)key.attachment()).state() == State.INSIDE) {
for (SelectionKey k : sel.keys()){
if(k.isValid() && k.channel() instanceof SocketChannel){
if(!((User)k.attachment()).getNick().equals(((User)key.attachment()).getNick())){
SocketChannel sct = (SocketChannel)key.channel();
sct.write(enc.encode(CharBuffer.wrap("NEWNICK "+((User)key.attachment()).getNick() +" "+ nick))); }
}
}
}
((User)key.attachment()).setNick(nick);
SocketChannel sct = (SocketChannel)key.channel();
sct.write(enc.encode(CharBuffer.wrap("OK")));
}

我的服务器代码发送消息:

message = it.getNick() +":"+ message;

for (SelectionKey k : sel.keys()) {
if(k.isValid() && k.channel() instanceof SocketChannel) {
SocketChannel sct = (SocketChannel)k.channel();
sct.write(enc.encode(CharBuffer.wrap(message)));

}
}

我的客户端在单独的线程中读取代码:

 public void run() {
while(open) {
String modifiedSentence ="";

try {
modifiedSentence = inFromServer.readLine();
} catch(IOException e) {
System.out.println("ERROR");}
System.out.println(modifiedSentence);

if(!modifiedSentence.equals(""))
chatArea.append(modifiedSentence+'\n');
}
}

我向服务器发送查询:

public void newMessage(String message) throws IOException {
outToServer.writeBytes(message+'\n');
}

更新:我用netcat测试了从服务器端接收消息,并且接收正确,所以我认为是客户端的问题。但我认为我做的一切都是正确的,如果您需要,我会放置我的客户端代码。

public class ChatClient  {
JFrame frame = new JFrame("Chat Client");
private JTextField chatBox = new JTextField();
private JTextArea chatArea = new JTextArea();
private Thread t;
private Socket clientSocket;
private DataOutputStream outToServer;

private BufferedReader inFromServer;

public void printMessage(final String message) {
chatArea.append(message);
}


public ChatClient(String server, int port) throws IOException {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(chatBox);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.SOUTH);
frame.add(new JScrollPane(chatArea), BorderLayout.CENTER);
frame.setSize(500, 300);
frame.setVisible(true);
chatArea.setEditable(false);
chatBox.setEditable(true);
chatBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
newMessage(chatBox.getText());
} catch (IOException ex) {
} finally {
chatBox.setText("");
}
}
});



clientSocket = new Socket(server, port);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
t =new Thread(new Reader(chatArea, clientSocket,inFromServer));
t.start();

}

public void newMessage(String message) throws IOException {

outToServer.writeBytes(message+'\n');

}

public void run() throws IOException {

while(!clientSocket.isClosed()){
}
Reader.open=false;


}

public static void main(String[] args) throws IOException {
ChatClient client = new ChatClient(args[0], Integer.parseInt(args[1]));
client.run();
}

}



class Reader implements Runnable {
private JTextArea chatArea;
private Socket clientSocket;
public static Boolean open = true;
private BufferedReader inFromServer;

public Reader(JTextArea chatArea,Socket clientSocket,BufferedReader inFromServer){
this.chatArea = chatArea;
this.clientSocket = clientSocket;
this.inFromServer = inFromServer;
}
public void run(){
while(open){
System.out.println("TTT\n");
String modifiedSentence ="";

try{

modifiedSentence = inFromServer.readLine();
}catch(IOException e){
System.out.println("ERROR");
}


chatArea.append(modifiedSentence+'\n');
}


}
}

感谢您的宝贵时间。

最佳答案

I used netcat to test receiving messages from the server , and it receives correctly , so I think the client side is what is wrong. But I think I 'm doing all correctly …

您最有可能做得不正确的是,在客户端中您使用 .readLine() ,它读取一行文本,而在服务器中,您不会用换行符('\n')或回车符('\r')终止所有消息。

关于java - 通过socket同时接收两条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41015170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com