【问题标题】:Java Socket Chat, some messages not sendingJava Socket Chat,一些消息没有发送
【发布时间】:2014-03-25 01:38:52
【问题描述】:

我最近开始学习 Sockets 和其他类似的网络东西,我用 GUI 做了一个简单的聊天程序,它有点工作,问题是如果命令消息发送到服务器,客户端必须把相同消息两次,如果服务器发送消息,客户端必须回复一次。

这是我的客户端/服务器类:

package org.codingllamas.Chat;

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

import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;

public class SC {

static Socket clientSocket;
static BufferedReader inFromServer;
static DataOutputStream outToServer;

public static void clientSetup(int port,String ip) throws IOException, BadLocationException {
     String modifiedSentence;
     Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Connected to </b>" + ip + ":" + port + "<br>",0,0,HTML.Tag.B);
     while (true) {
     ///BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
     clientSocket = new Socket(ip,port);
     outToServer = new DataOutputStream(clientSocket.getOutputStream());
     inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
     modifiedSentence = inFromServer.readLine();     
     //System.out.println(modifiedSentence);
     Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server: </b>" + modifiedSentence + "<br>",0,0,HTML.Tag.B);
     //clientSocket.close();
     }
}

public static void clientSend(String msg) throws IOException, BadLocationException{
    outToServer.writeBytes(msg + "\r");
    outToServer.flush();
    Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}

static ServerSocket welcomeSocket;
static Socket connectionSocket;
static BufferedReader inFromClient;
static DataOutputStream outToClient;

public static void serverSetup(int port) throws Exception {
    String clientSentence;
    welcomeSocket = new ServerSocket(port);
    Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server Started on port: " + port + "</b><br>",0,0,HTML.Tag.B);
    while(true){
       connectionSocket = welcomeSocket.accept();
       inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
       clientSentence = inFromClient.readLine();
       outToClient = new DataOutputStream(connectionSocket.getOutputStream());

       Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Parther: </b>" + inFromClient.readLine() + "<br>",0,0,HTML.Tag.B);
    }
}

public static void serverSend(String msg) throws IOException, BadLocationException {
    //DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
    outToClient.writeBytes(msg + "\r");
    outToClient.flush();

    Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}
}

还有一张照片:

提前致谢。

【问题讨论】:

    标签: java sockets chat


    【解决方案1】:
    1. 不要使用DataOutputStream,使用BufferedWriter.
    2. 不要为每个 modifiedSentence? 创建新的套接字:在客户端的生命周期内使用单个 Socket,并循环读取行,并在获得 null 时关闭套接字。
    3. 不要像客户端现在那样从客户端读取一行 accept(): 启动一个新线程来读取行。

    【讨论】:

    • 我将代码更改为:pastebin.com/FjDSvZBf 客户端获取服务器发送的所有消息,但出于某种奇怪的原因,服务器只获取客户端发送的所有其他消息。 (即客户端发送“1”、“2”、“3”、“4”、“5”、“6”,服务器得到“2”、“4”、“6”)
    猜你喜欢
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2021-12-04
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多