求助DEBUG~~!JAVA的网络编程
- 提问者网友:蓝莓格格巫
- 2021-08-11 08:26
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class GameServer extends JFrame
{
private JTextArea jta = new JTextArea();
public static void main(String[] args)
{
new GameServer();
}
public GameServer()
{
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("SeverSide");
jta.setEditable(false);
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try
{ ServerSocket serverSocket = new ServerSocket(6666);
jta.append("Server started at " + new Date() + '\n');
Socket socket = serverSocket.accept();
DataInputStream fromClient = new DataInputStream(socket.getInputStream());
DataOutputStream toClient = new DataOutputStream(socket.getOutputStream());
toClient.writeUTF("\nfrom server");
toClient.writeInt('\n' + fromClient.readInt());
// while(true)
// {
// String status = fromClient.readUTF();
// jta.append(status);
// }
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}
这个是游戏服务器端的class
package client;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.*;
public class GameClient extends JFrame
{
private ClientThreads ct;
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
public JTextField getJtf()
{
return jtf;
}
public JTextArea getJta()
{
return jta;
}
public ClientThreads getCt()
{
return ct;
}
private DataInputStream fromServer2;
private DataOutputStream toServer2;
public static void main(String[] args) throws UnknownHostException, IOException
{
new GameClient();
}
public GameClient() throws UnknownHostException, IOException
{
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter a num"), BorderLayout.WEST);
p.add(jtf,BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.LEFT);
setLayout(new BorderLayout());
add(p, BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.CENTER);
jtf.addActionListener(new TextFieldListener());
setTitle("Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//ct = new ClientThreads("localhost", 6666, this);
Socket socket = new Socket("localhost", 6666);
ct = new ClientThreads(socket,this);
//toServer2 = new DataOutputStream(socket.getOutputStream());
//fromServer2 = new DataInputStream(socket.getInputStream());
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
ct.start();
String text = jtf.getText().trim() + '\n';
//ct.getToServer().writeUTF(text);
ct.sendMessages(text);
//toServer2.writeUTF(text);
//toServer2.flush();
ct.getToServer().flush();
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}
}
客户端的CLASS
package client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientThreads extends Thread
{
private Socket socket;
private GameClient game;
//private int portnumber;
//private String servername;
private DataOutputStream toServer;
private DataInputStream fromServer;
public Socket getSocket()
{
return socket;
}
public DataInputStream getFromServer()
{
return fromServer;
}
// public ClientThreads(String servername, int portnumber, GameClient game)
// {
// this.servername = servername;
// this.portnumber = portnumber;
// this.game = game;
// }
public ClientThreads(Socket socket, GameClient game)
{
this.socket = socket;
this.game = game;
}
public void run()
{
try
{
//socket = new Socket("localhost", 6666);
fromServer = new DataInputStream(socket.getInputStream());
toServer = new DataOutputStream(socket.getOutputStream());
String msg;
while(true)
{
msg = fromServer.readUTF();
game.getJta().append(msg + '\n');
//game.getJta().append("pass number back succesfully\n");
}
}
catch (IOException e)
{
System.err.println(e);
}
}
public DataOutputStream getToServer()
{
return toServer;
}
public void sendMessages(String m) throws IOException
{
if(socket.isClosed()==false)
{
toServer.writeUTF(m);
toServer.flush();
}
}
}
客户端线程的CLASS
大概内容是这样,ClientThreads创建一个客户端的线程,然后在GameClient里边创建一个叫ct的object, 然后在界面输入一点儿信息,会发送到服务器端,服务器端的界面会有显示,然后如果收到的话,服务器会发送一个String "from server" 回到客户端。
(运行时要先运行服务器,在运行客户,否则显示连接不到)
现在的问题是,当客户端发送信息时,服务器端收不到,但是客户端能接受到服务器发回的信息,也就是说连接是正常没有问题的,但是客户端发送的部分有问题,我是创建了ct的ClientThreads的object,然后调用sendMessages(m)这个method来发送信息,但是很失败~~~~~
理论上是没有问题,事实上是实现不了的,耗了我一天一夜了已经,求解~!
- 五星知识达人网友:醉吻情书
- 2021-08-11 09:14
嘿嘿 这位朋友,一看就知道是高手 ...
其实你这个问题不是什么问题 ,你瞧瞧你的这段话:
public void sendMessages(String m) throws IOException
{
if(socket.isClosed()==false)
{
toServer.writeUTF(m);
toServer.flush();
}
}
toServer 会报告一个NullPointer的exception ,那为什么呢会这样呢?我接着往上看 ,发现你把
toServer = new DataOutputStream(socket.getOutputStream());
写在了run()方法里面,那我就想是不是这句话没有执行你就调用了sendMessages()方法呢?
果然,你的文本域控件的消息响应函数是这么写的:
ct.start();
String text = jtf.getText().trim() + '\n';
ct.sendMessages(text);
ct.getToServer().flush();
那你想一想,是不是你这个ct.start()调用之后,ct的run方法没来得及运行,toServer没来的及初始化,接下来的ct.sendMessages()就已经运行了呢?
我想答案是肯定的,你在ct.start();后面添上一句Thread.sleep(1000);发现toServer已经被正常初始化了 ,这样这一点就被证明了....
呵呵 java我忘得差不多了,看你的程序给我很清新的感觉...
- 1楼网友:痴妹与他
- 2021-08-11 10:30
晕,TextField 不能触发你定义的那个ActionListener,你最好定义多一个按钮,用来触他。
你看不管什么聊天工具都会有一个发送按钮,再或者你得在TextField定义键盘事件,才可能会发送信息到服务器。