马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 不会起名字的我 于 2021-7-22 15:00 编辑
我写了一个TCP程序,但是报空指针异常,客户端我发了消息还是一样,代码如下
服务器代码import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import java.net.*;
public class Start extends JFrame {
boolean isStart = false;
JTextArea outtext;
JTextField port;
ServerSocket server;
Socket client;
public Start() {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 300);
setTitle("服务器");
setLayout(new BorderLayout());
Container container = getContentPane();
JPanel panel1 = new JPanel();
JLabel serverport = new JLabel("端口");
JTextField ip = new JTextField(10);
port = new JTextField(10);
JToolBar tool = new JToolBar();
JButton start = new JButton("启动服务器");
JButton close = new JButton("关闭服务器");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (isStart) {
print("正在关闭服务器···");
server.close();
isStart = false;
print("服务器已经关闭。");
} else {
print("警告:服务器已关闭。");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new open().start();
}
});
tool.add(start);
tool.addSeparator();
tool.add(close);
tool.setFloatable(false);
container.add(tool, BorderLayout.NORTH);
panel1.setLayout(new BorderLayout());
JPanel panel2 = new JPanel();
panel2.add(serverport);
panel2.add(port);
panel1.add(panel2, BorderLayout.NORTH);
outtext = new JTextArea();
outtext.setEditable(false);
JScrollPane js = new JScrollPane(outtext);
panel1.add(js, BorderLayout.CENTER);
container.add(panel1, BorderLayout.CENTER);
validate();
}
class open extends Thread {
@Override
public void run() {
print("正在启动服务器···");
int port1 = new Integer(port.getText());
try {
server = new ServerSocket(port1);
isStart = true;
new input().start();
print("服务器已经启动。");
} catch (IOException e) {
e.printStackTrace();
}
}
}
class connect extends Thread {
public void run() {
while (true) {
try {
client = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class input extends Thread {
@Override
public void run() {
while (isStart) {
try {
InputStream fis = client.getInputStream();// 这里出错了
byte[] info = new byte[409600];
int count ;
while((count = fis.read(info))!=-1) {
String str = new String(info, 0, count);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
void print(String message) {
outtext.append(message + "\n");
}
public static void main(String[] args) {
new Start();
}
}
客户端代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.*;
import java.net.*;
public class Start extends JFrame {
JTextArea area;
JTextField inputip;
JTextField inputport;
Socket client;
boolean lianjie1 = false;
public Start() {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setTitle("Java Chat");
setLayout(new BorderLayout());
Container container = getContentPane();
JLabel ip = new JLabel("服务器IP");
JLabel port = new JLabel(" 端口");
inputip = new JTextField(10);
inputport = new JTextField(10);
JButton lianjie = new JButton("连接");
lianjie.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new lianjieServer().start();
}
});
JPanel panel1 = new JPanel();
panel1.add(ip);
panel1.add(inputip);
panel1.add(port);
panel1.add(inputport);
panel1.add(lianjie);
container.add(panel1, BorderLayout.NORTH);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
area = new JTextArea();
JScrollPane js = new JScrollPane(area);
panel2.add(js, BorderLayout.CENTER);
container.add(panel2, BorderLayout.CENTER);
JLabel send1 = new JLabel("发送:");
JButton Send = new JButton("发送");
JTextField send = new JTextField(30);
JPanel panel3 = new JPanel();
panel3.add(send1);
panel3.add(send);
panel3.add(Send);
Send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
print(lianjie1);
if(lianjie1) {
try {
OutputStream fot = client.getOutputStream();
byte a[] = send.getText().getBytes();
fot.write(a);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
area.setEditable(false);
container.add(panel3, BorderLayout.SOUTH);
validate();
}
class lianjieServer extends Thread {
public void run() {
print("正在尝试连接···");
try {
if (!(inputip.getText() == "")) {
int PORT = Integer.valueOf(inputport.getText());
client = new Socket(inputip.getText(), PORT);
lianjie1 = true;
print("连接成功!");
} else {
print("无效的端口。");
}
} catch (Exception e) {
print("连接失败!");
}
}
}
void print(Object message) {
area.append(message + "\n");
}
public static void main(String[] args) {
new Start();
}
}
请问哪里出了问题? |