- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我构建了一个 Java 服务器程序,它是出勤跟踪器应用程序的一部分。它接收来自客户端应用程序的命令,并执行连接到我们设置的数据库的服务器功能。问题是,一旦我们启动程序,它会在 session 开始时自动向客户端发送两个 U+FFFD 字符(带有问号符号的菱形),从而破坏客户端代码,并在客户端返回错误。我无法弄清楚代码中发生这种情况的位置,因为打印到命令行的内容就是传递给客户端的内容。我什至不知道从哪里开始寻找。这是主文件的代码,如果需要,我会将整个项目上传到 Github 上。我知道有很多代码需要查看,但如果有人能够立即发现问题,我会很高兴。谢谢您的帮助!
Github:https://github.com/aepries/stacServ.git
package stacserv;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.text.Normalizer;
public class StacServ {
/**
* @param args the command line arguments
*/
public static JFrame mainFrame = null;
public static JTextArea chatText = null;
public static StringBuffer toAppend = new StringBuffer("");
public static void initGUI(){
JPanel chatPane = new JPanel(new BorderLayout());
chatText = new JTextArea(20, 20);
chatText.setLineWrap(true);
chatText.setForeground(Color.blue);
chatText.setFont(chatText.getFont().deriveFont(20f));
JScrollPane chatTextPane = new JScrollPane(chatText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
chatPane.add(chatTextPane, BorderLayout.CENTER);
chatPane.setPreferredSize(new Dimension(800, 400));
// Set up the main pane
JPanel mainPane = new JPanel(new BorderLayout());
mainPane.add(chatPane, BorderLayout.CENTER);
// Set up the main frame
mainFrame = new JFrame("Client Messages");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setContentPane(mainPane);
mainFrame.setSize(mainFrame.getPreferredSize());
mainFrame.setLocation(300, 300);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) throws Exception {
initGUI();
ServerSocket m_ServerSocket = new ServerSocket(1025);
int id = 0;
StacServ.chatText.append("Server is now running and listening on port 1025 " + "\n");
while (true) {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
}
}
class ClientServiceThread extends Thread {
Socket clientSocket;
int clientID = -1;
String userName = null;
boolean running = true;
private MySQLAccess dbconnection;
HashMap<Socket, Integer> connections = new HashMap<Socket, Integer>();
public ArrayList<ClassData> queryResult = new ArrayList<ClassData>();
StringBuilder sb = new StringBuilder();
String commandName = null;
String clientCommand = null;
String restofString = null;
String[] tokens = new String[2];
ClientServiceThread(Socket s, Integer i) {
clientSocket = s;
clientID = i;
connections.put(s, i);
StacServ.chatText.append("Current Connections" + "\n");
for(HashMap.Entry entry: connections.entrySet()){
StacServ.chatText.append(entry.getKey() + ", " + entry.getValue() + "\n");
}
}
@Override
public void run() {
try
{
dbconnection = new MySQLAccess();
dbconnection.connect("138.86.104.164","STACDB","SEClass","BearsRock");
} catch (Exception e) {
//System.out.println(e);
//e.printStackTrace();
}
System.out.println("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
StacServ.chatText.append("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName()+"\n");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
ObjectOutputStream objectOutput = new ObjectOutputStream(clientSocket.getOutputStream());
while (running) {
out.flush();
in.mark(0);
in.reset();
clientCommand = in.readLine();
try{
if(clientCommand == null){
} else {
clientCommand = clientCommand.replace("\"", "");
tokens = clientCommand.split(" ", 2);
commandName = tokens[0];
restofString = tokens[1];
}
} catch(ArrayIndexOutOfBoundsException e) {
//Handle ArrayIndexOutOfBoundsException
}
if (commandName.equalsIgnoreCase("LOGO")) {
System.out.print("Stopping client thread for client : " + clientID);
StacServ.chatText.append("Stopping client thread for client : " + clientID+"\n");
out.println("LOGO" + " logged out."+"\n");
out.flush();
running = false;
}
else if (commandName.equalsIgnoreCase("REGU")){
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String uName = keys[0];
String uPass = keys[1];
String fName = keys[2];
String lName = keys[3];
StacServ.chatText.append("Attemplting to add client: " + fName + " " + lName);
dbconnection.createUser(uName, uPass, fName, lName);
out.println("REGR S");
out.flush();
}
else if (commandName.equalsIgnoreCase("CRCR")){
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String className = keys[0];
String institution = keys[1];
String startDate = keys[2];
String endDate = keys[3];
String ipAddress = keys[4];
String meetDOW = keys[5];
StacServ.chatText.append("Attemplting to add class: " + className);
dbconnection.createClass(className, institution, startDate, endDate, ipAddress, meetDOW);
out.println("CRER S" + className);
out.flush();
}
else if (commandName.equalsIgnoreCase("CSRC")){
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String className = keys[0];
String institution = keys[1];
if(institution == " "){
institution = "%";
}
StacServ.chatText.append("Attemplting to find class: " + className +" " + "\n");
ResultSet rs = dbconnection.searchClasses(className, institution);
//ResultSet rs = dbconnection.searchClasses(className, institution);
while(rs.next()){
queryResult.add(new ClassData(rs.getString("classID"), rs.getString("className"), rs.getString("adminID"), rs.getString("institution"), rs.getString("startDate"), rs.getString("endDate"), rs.getString("PublicIPAddress")));
String ctClassID = rs.getString("classID");
String ctClassName = rs.getString("className");
String ctAdminID = rs.getString("adminID");
String ctinstitution = rs.getString("institution");
String ctstartDate = rs.getString("startDate");
String ctendDate = rs.getString("startDate");
String ctipAddress = rs.getString("endDate");
out.println("CDTR S" + ctClassID + ctClassName + ctAdminID + ctinstitution + ctstartDate + ctendDate + ctipAddress);
StacServ.chatText.append("CDTR S" + " " + ctClassID + " " + ctClassName + " " + ctAdminID + " " + ctinstitution + " " + ctstartDate + " " + ctendDate + " " + ctipAddress + "\n");
}
}
else if (commandName.equalsIgnoreCase("CTDL")){
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String classID = keys[0];
int classId = Integer.parseInt(classID);
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
StacServ.chatText.append("Attemplting to find class with ID: " + classId + "\n");
ResultSet rs = dbconnection.searchClasses(classId);
while(rs.next()){
queryResult.add(new ClassData(rs.getString("classID"), rs.getString("className"), rs.getString("adminID"), rs.getString("institution"), rs.getString("startDate"), rs.getString("endDate"), rs.getString("PublicIPAddress")));
String ctClassID = rs.getString("classID");
String ctClassName = rs.getString("className");
String ctAdminID = rs.getString("adminID");
String ctinstitution = rs.getString("institution");
String ctstartDate = rs.getString("startDate");
String ctendDate = rs.getString("startDate");
String ctipAddress = rs.getString("endDate");
out.println("CDTR S" + ctClassID + ctClassName + ctAdminID + ctinstitution + ctstartDate + ctendDate + ctipAddress);
StacServ.chatText.append("CDTR " + "S" + ctClassID + ctClassName + ctAdminID + ctinstitution + ctstartDate + ctendDate + ctipAddress + "\n");
}
}
else if (commandName.equalsIgnoreCase("ELST")){
StacServ.chatText.append("ELST TEST " + userName +"\n");
System.out.println("Client Says :" + clientCommand + "\n");
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
int userID = dbconnection.getUserID(userName);
ResultSet userClasses = dbconnection.getStudentsClasses(userID);
String dummy = Integer.toString(userID);
StacServ.chatText.append("userID is: " +dummy +"\n");
StacServ.chatText.append("Attemplting to find classes for user: " + userName + "\n");
sb = new StringBuilder("");
while(userClasses.next()){
String ctClassID = userClasses.getString("classID");
sb.append(ctClassID);
sb.append(" ");
}
out.println("ELSR S" + sb.toString() + "\n");
StacServ.chatText.append("ELSR S" + sb.toString() + "\n");
}
else if (commandName.equalsIgnoreCase("CLST")){
userName = "ejohn";
StacServ.chatText.append("ELST TEST " + userName +"\n");
System.out.println("Client Says :" + clientCommand + "\n");
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
int userID = dbconnection.getAdminsID(userName);
ResultSet userClasses = dbconnection.getAdminClasses(userID);
String dummy = Integer.toString(userID);
StacServ.chatText.append("userID is: " +dummy +"\n");
StacServ.chatText.append("Attemplting to find classes for user: " + userName + "\n");
userName = "CCDawg";
sb = new StringBuilder("");
while(userClasses.next()){
String ctClassID = userClasses.getString("classID");
sb.append(ctClassID);
sb.append(" ");
}
out.println("ELSR S" + sb.toString() + "\n");
StacServ.chatText.append("ELSR " + "S " + sb.toString() + "\n");
}
else if(commandName.equalsIgnoreCase("ENRL")){
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
userName = "CCDawg";
String flag = "0";
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String classID = keys[0];
String deviceID = keys[1];
int classIDs = Integer.parseInt(classID);
dbconnection.addDevice(userName, classIDs, deviceID, flag);
}
else if(commandName.equalsIgnoreCase("CDRP")){
userName = "PillCosby";
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String classID = keys[0];
int classIDs = Integer.parseInt(classID);
dbconnection.unenrollStudent(classIDs, userName);
}
else if (commandName.equalsIgnoreCase("REGA")){
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String uName = keys[0];
String uPass = keys[1];
String fName = keys[2];
String lName = keys[3];
StacServ.chatText.append("Attempting to add Admin: " + fName + " " + lName);
dbconnection.createAdmin(uName, uPass, fName, lName);
out.println("REGA " + "S"+"\n");
out.flush();
}
else if (commandName.equalsIgnoreCase("LOGA")){
ResultSet rs = null;
System.out.println("Client Says :" + clientCommand+"\n");
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String uName = keys[0];
String uPass = keys[1];
StacServ.chatText.append("Attempting to Login Admin: " + uName+"\n");
rs = dbconnection.checkAdmin(uName);
if(rs != null){
while(rs.next()){
String check = rs.getString(1);
if(uName.equals(check)){
check = rs.getString(2);
if(uPass.equals(check)){
StacServ.chatText.append("Admin: " + uName + " logged in."+"\n");
out.flush();
out.println("LOGR S");
userName = uName;
out.flush();
}
else{
StacServ.chatText.append("Admin: " + uName + "Incorrect username or password"+"\n");
out.flush();
out.println("LOGR " + "F"+"\n");
out.flush();
}
}
}
}
else{
StacServ.chatText.append("Admin: " + uName + "is not registered in the system");
out.println("LOGA " + "F"+"\n");
out.flush();
}
}
else if (commandName.equalsIgnoreCase("LOGU")){
ResultSet rs = null;
System.out.println("Client Says :" + clientCommand);
StacServ.chatText.append("Client Says :" + clientCommand+"\n");
StacServ.chatText.append(restofString +"\n");
//restofString = restofString.replaceAll("[\u0000-\u001f]", "");
String[] keys = restofString.split("\"?( |$)(?=(([^\"]*\"){2})*[^\"]*$)\"?");
String uName = keys[0];
String uPass = keys[1];
uName = uName.replace("\"", "");
uPass = uPass.replace("\"", "");
StacServ.chatText.append("Attempting to log in: " + uName+"\n");
rs = dbconnection.checkUser(uName);
if(rs != null){
while(rs.next()){
String check = rs.getString(1);
if(uName.equals(check)){
check = rs.getString(2);
if(uPass.equals(check)){
StacServ.chatText.append("User: " + uName + " logged in."+"\n");
out.println("LOGR S");
userName = uName;
out.flush();
}
else{
StacServ.chatText.append("User: " + uName + "\n" + "Incorrect password" +"\n");
out.println("LOGR F" + "\n");
out.flush();
}
}
}
}
else{
StacServ.chatText.append("Admin: " + uName + "is not registered in the system");
out.println("LOGR F");
out.flush();
}
}
else {
//out.println(clientCommand);
out.flush();
}
}
} catch (Exception e) {
//e.printStackTrace();
}
}
}
最佳答案
您确定要使用ObjectOutputStream
吗?
它将向底层的OutputStream
输出一个序列化 header 。如果您想避免这种情况,但仍然有原始数据输出的方法(如果您有面向行的协议(protocol),则可能不需要),那么 DataOutputStream
提供相同的方法,但不带序列化 header 。
对于基于行的协议(protocol),StreamWriter
是更好的选择。
关于Java 服务器程序在启动时吐出两个 U+FFFD 字符。需要帮助弄清楚它在哪里执行此操作以及为什么执行此操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855330/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
为什么在 C# 中添加两个 char 结果是 int 类型? 例如,当我这样做时: var pr = 'R' + 'G' + 'B' + 'Y' + 'P'; pr 变量变为 int 类型。我希望它是
下面的代码可以编译,但 char 类型的行为与 int 类型的行为不同。 特别是 cout ::ikIsX >() ::ikIsX >() ::ikIsX >() using names
我正在寻找一个正则表达式,它可以匹配长度为 1 个或多个字符但不匹配 500 的内容。这将在 Rails 路由文件中使用,特别是用于处理异常。 路线.rb match '/500', to: 'err
对于 C 编程作业,我正在尝试编写几个头文件来检查所谓的“X 编程语言”的语法。我最近才开始,正在编写第一个头文件。这是我编写的代码: #ifndef _DeclarationsChecker_h_
为什么扩展的 ascii 字符(â、é 等)被替换为 字符? 我附上了一张图片...但我正在使用 PHP 从 MySQL 中提取数据,其中一些位置有扩展字符...我使用的是 Arial 字体。 您可以
我有一个与 R 中的断线相关的简单问题。 我正在尝试粘贴,但在获取(字符/数字)之间的断线时遇到问题。请注意,这些值包含在向量中(V1=81,V2=55,V3=25)我已经尝试过这段代码: cat(p
如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然? 是否有用于此目的的任何跨平台源代码? 最佳答案 是的,在 中你有mbstowcs()和 wcsto
函数 fromCharCode 不适用于国际 ANSI 字符。例如,对于 ID 为 192 到 223 的俄语 ANSI (cp-1251) 字符,它返回特殊字符。如何解决这个问题? 我认为,需要将A
如果不喜欢,我想隐藏 id,但不起作用 SELECT * FROM character, character_actor WHERE character.id NOT LIKE character_a
现在这个程序成功地反转了键盘输入的单词。但是我想在我反转它之前“保存”指针中的单词,所以我可以比较两者,反转的和“原始的”,并检查它们是否是回文。我还没有太多经验,可能会出现比我知道的更多的错误,但我
Memcpy 和 memcmp 函数可以接受指针变量吗? char *p; char* q; memcpy(p,q,10); //will this work? memcmp(p,q,10); //w
恐怕我对一个相当过饱和的主题的细节有疑问,我搜索了很多,但找不到一个明确的答案来解决这个特定的明显-imho-重要的问题: 使用UTF-8将byte[]转换为String时,每个字节(8bit)都变成
我有一个奇怪的问题。我需要从 stat 命令打印输出字符串。 我已经编写了获取一些信息的代码。 import glob import os for file in glob.glob('system1
我正在使用 Java 并具有其值如下所示的字符串, String data = "vale-cx"; data = data.replaceAll("\\-", "\\-\\"); 我正在替换其中的“
String urlParameters = "login=test&password=te&ff"; 我有一个String urlParams,& - 是密码的一部分,如何使其转义,从而不被识别为分
大家好,我只想从此字符串中提取第一个字母: String str = "使 徒 行 傳 16:31 ERV-ZH"; 我只想获取这些字符: 使 徒 行 傳 并且不包括 ERV-ZH 仅数
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
所以, 我有一个字符**;它本质上是一个句子,带有指向该句子中每个单词的指针;即 'h''i''\0''w''o''r''l''d''\0''y''a''y''!''\0' 在这种情况下,我希望使用可
这个问题在这里已经有了答案: Using quotation marks inside quotation marks (12 个答案) 关闭 7 年前。 如何打印 " 字符? 我知道打印 % 符号
我是一名优秀的程序员,十分优秀!