我的Java老师进行的练习对我有很大的问题。从理论上讲,练习必须具有以下几点:
-套接字-客户端-服务器-服务器使用MySql做某些事情-登录-Md5保存密码-安全套接字
有了这个,我决定进行理论上的聊天应该是一件容易的事,但是……我完全迷失了。
我或多或少地制作了基础(安全套接字,服务器,客户端),但即使这样也不起作用,但从理论上讲,IDE绝对不会失败。
有人可以帮我吗?
代码如下:
ChatClient使客户端工作,并加载界面和功能:
public class ChatClient
{
private Socket s;
private ClientPanel panel;
public static void main(String[] args)
{
new ChatClient();
}
public ChatClient()
{
try
{
Window();
s = new Socket("localhost" , 5557);
ClientControl control = new ClientControl(s, panel);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void Window()
{
JFrame v = new JFrame();
panel = new PanelCliente(v.getContentPane());
v.pack();
v.setVisible(true);
v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
ServerChat可以使用安全套接字创建服务器聊天,这是练习的必要条件之一:
public class ServerChat extends Thread
{
public static void main(String[] args)
{
int port= 5090;
SSLServerSocketFactory sslserver = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
try
{
SSLServerSocket sslsocket = (SSLServerSocket)sslserver.createServerSocket();
InetSocketAddress socketAddress = new InetSocketAddress("localhost" , port);
while(true)
{
SSLSocket socket = (SSLSocket)sslsocket.accept();
System.out.println("Client: " + socket.getInetAddress().getHostName() + " Conected");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class ClientControl implements ActionListener, Runnable
{
private DataInputStream dataInput;
private DataOutputStream dataOutput;
private ClientPanel panel;
public ClientControl (Socket s, ClientPanel panel)
{
this.panel = panel;
try
{
dataInput = new DataInputStream(s.getInputStream());
dataOutput = new DataOutputStream(s.getOutputStream());
panel.addActionListener(this);
Thread t = new Thread(this);
t.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void actionPerformed (ActionEvent event)
{
try
{
dataOutput.writeUTF(panel.getTexto());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
try
{
String text = dataInput.readUTF();
panel.addTexto(text);
panel.addTexto("\n");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
从理论上讲,客户端线程可以使客户端作为线程运行并实现其功能:
public class ClientThread implements Runnable, ListDataListener
{
private DefaultListModel conversation;
private Socket s;
private DataInputStream dataInput;
private DataOutputStream dataOutput;
public ClientThread (DefaultListModel conversation, Socket s)
{
this.conversation = conversation;
this.s = s;
try
{
dataInput = new DataInputStream(s.getInputStream());
dataOutput = new DataOutputStream(s.getOutputStream());
conversation.addListDataListener(this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void run()
{
try
{
while (true)
{
String text = dataInput.readUTF();
System.out.println(text);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void intervalAdded(ListDataEvent e)
{
String text = (String) conversation.getElementAt(e.getIndex0());
try
{
dataOutput.writeUTF(text);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
@Override
public void contentsChanged(ListDataEvent arg0)
{
}
@Override
public void intervalRemoved(ListDataEvent arg0)
{
}
}
下面的“客户端面板”基本上是使用JSwing做一个简单的设计,以创建和连接您可以看到对话的界面,您可以编写所需的任何内容:
public class ClientPanel
{
private JScrollPane scroll;
private JTextArea textArea;
private JTextField textField;
private JButton button;
public ClientPanel(Container cont)
{
cont.setLayout(new BorderLayout());
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
JPanel panel = new JPanel(new FlowLayout());
textField = new JTextField(50);
button = new JButton("Send");
panel.add(textField);
panel.add(button);
cont.add(scroll, BorderLayout.CENTER);
cont.add(panel, BorderLayout.SOUTH);
}
public void addActionListener (ActionListener action)
{
textField.addActionListener(action);
button.addActionListener(action);
}
public void addTexto (String text)
{
textArea.append(text);
}
public String getTexto()
{
String text = textField.getText();
textField.setText(text);
return text;
}
}
如何将数据库添加到“登录用户”?如何在其中添加Md5以保护密码?我如何才能使所有这些一起工作?
那是我的问题
您有服务器和客户端,并且想要编写聊天记录。因此,服务器是中心,并保持与存储所有持久性数据的数据库的连接。密码不是以纯文本形式存储的,只有它的md5哈希存储在数据库中。此外,只有服务器拥有与数据库的连接。
这回答了使用位置MD5
以及数据库管理员是谁。
您已经创建了一个SeverChat
。那家伙负责听新客户进行连接。如果新客户端要连接,则ServerChat
必须产生一个新客户端ClientController
。
你ClientControl
看起来不像我的意思。AClientControll
负责从与其连接的特定客户端获取请求,处理该请求并将答案发送给该客户端。
这意味着您需要某种protokol。您可以使用ObjectStreams将对象从客户端发送到服务器,反之亦然。这使定义protokol更容易。
了解以下内容ClientController
:
class ClientController extends Thread {
private final ObjectInputStream dataInput;
private final ObjectOutputStream dataOutput;
private boolean loggedIn = false;
ClientController(ObjectInputStream dataInput, ObjectOutputStream dataOutput) {
this.dataInput = dataInput;
this.dataOutput = dataOutput;
}
@Override
public void run() {
try {
boolean stayConnected = true;
while (stayConnected) {
Object data = dataInput.readObject();
if (data instanceof LoginAction) {
// check data an do login
this.loggedIn = true;
dataOutput.write(new LoginResponse(/* response data */));
}
if (data instanceof RequestChatDataAction) {
if (this.loggedIn) {
dataOutput.write(new NotLoggedInResponse());
} else {
dataOutput.write(new ChatDataResponse(/* chat data.. */));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
的LoginAction
,LoginResponse
ASO对象定义protokol。您需要使更多的对象实现所有功能,而这些对象也必须实现java.io.Serializable
。否则,您将无法通过电线发送它们。
您还必须编写计数器部分-客户。
客户端启动,连接到服务器并尝试登录。如果登录成功,客户端将等待新的聊天数据并显示它。如果用户输入内容,该数据将发送到服务器,并将被添加到“全球”聊天数据中。
我建议不要在完成客户端与服务器之间的通信之前添加gui元素。您可以使用System.out
和System.in
来与用户互动。
因此,希望对您有所帮助。
此外,SO并非针对以下问题:做我的作业。我知道你已经参加了这次旅行。
阅读如何创建最小,完整和可验证的示例将不胜感激。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句